程式最火紅的一個範例了~~~
HELLO WORD!!
HELLO WORD!!
/*
Java Mail 簡易版
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class JavaMail {
public static void main(String[] args)
{
try{
// 設定傳送基本資訊
String host = "Your host";
String from = "From mail Address";
String to = "To mail Address";
Properties props = System.getProperties();
//設定SMTP server
props.put("mail.smtp.host",host);
//依據Properties建立一個Session
Session mailSession = Session.getInstance(props,null);
//從Session建立一個Message
Message mailMessage = new MimeMessage(mailSession);
//Set from email address
mailMessage.setFrom(new InternetAddress(from));
//Set to mail address
mailMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
//設定標題
mailMessage.setSubject("Hello JavaMail");
//設定Mail內容
mailMessage.setText("Wellcome to JavaMail...加油!!");
//傳送
Transport.send(mailMessage);
System.out.println("\n Mail was sent successfully.");
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
文章標籤
全站熱搜

請問,以此例子看來,Javamail直接以localhost就可以發信? 不需要透過其它mail server?
你問的太好了,讓我發現這範例有點問題。 props.put("mail.smtp.host",host); 以上這段應該將前面使用的host由此帶入才對,當然不是寫這段程式就讓自己本機成為mail server,這程式還是得透過mail server,寫成localhost是如果你程式就放在mail server本機的話。 這程式不是自動變成MTA(http://en.wikipedia.org/wiki/Mail_transfer_agent),相關mail的架構know-how你可能要再察看一下。
怎麼都是寄信的範例 ><" 找不到收信的範例! ><"