JavaMail介紹及發送一封簡單郵件

  本文來自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,轉載請註明。
       JavaMail是SUN提供給開發人員在應用程序中實現郵件發送和接收功能而提供的一套標準開發類庫,支持常用的郵件協議,如SMTP、POP3、IMAP,開發人員使用JavaMail編寫郵件程序時,無需考慮底層的通信細節(Socket),JavaMail也提供了能夠創建出各種複雜MIME格式的郵件內容的API。使用JavaMail,我們可以實現類似OutLook、FoxMail的軟件。雖然JavaMail(僅支持JDK4及以上)也是Java的API之一,但是卻沒有直接加入到JDK中,所以我們需要另行下載。另外,JavaMail依賴JAF(JavaBeans Activation Framework),JAF在Java6之後已經合併到JDK中,而JDK5之前需要另外下載JAF的類庫。下載地址如下:

       JavaMail:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-JPR

       JavaMail spec:http://www.oracle.com/technetwork/java/javamail-1-149769.pdf

       JAF:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jaf-1.1.1-fcs-oth-JPR

       JavaMail下載好後,我們來看一下其主要內容:

  1. README.txt:整體介紹JavaMail,需要看一下  
  2. docs/javadocs:The JavaMail API javadocs,需要看一下  
  3. mail.jar:包括JavaMail API和所有service providers,大部分用戶只需要該jar包  
  4. lib/mailapi.jar :只有JavaMail API  
  5. lib/imap.jar:The IMAP service provider  
  6. lib/smtp.jar:The SMTP service provider  
  7. lib/pop3.jar:The POP3 service provider  
  8. lib/dsn.jar:multipart/report DSN message support  
  9. demo:demo示例,簡單瞭解,有需要再看  
       JavaMail包含兩部分內容,一部分是JavaMail API,定義了一組平臺無關、獨立於通訊協議的郵件程序框架,該部分稱爲應用級接口,也就是供我們調用的部分,另一部分是service provider,該部分使用特定的協議語言來實現第一部分定義的抽象類和接口,這些協議包括:SMTP、NNTP、POP3、IMAP,如果讓JavaMail與郵件服務器通信,就需要相應的協議支持,該部分稱爲服務提供者接口,也就是JavaMail自身需要的協議支持。在使用JavaMail時,通常我們只需將mail.jar放在classpath下使用,它包含了JavaMail API部分和SUN自己實現的service provider部分。可能也有特殊的時候,我們應用程序中需要自己實現service provider部分,那我們只需要mailapi.jar。下面通過幾個類來簡單認識下JavaMail API:
  1. javax.mail.Session:上下文環境信息,如服務器的主機名、端口號、協議名稱等  
  2. javax.mail.Message:郵件模型,發送郵件和接收郵件的媒介,封裝了郵件的信息,如發件人、收件人、郵件標題、郵件內容等  
  3. javax.mail.Transport:連接郵件SMTP服務器,發送郵件  
  4. javax.mail.Store:連接郵件POP3、IMAP服務器,收取郵件  
       通過這些類,最終就可以實現收發郵件,一個發送郵件的簡單示例:
  1. public class JavaMailTest1 {  
  2.     public static void main(String[] args) throws MessagingException {  
  3.         Properties props = new Properties();  
  4.         // 開啓debug調試  
  5.         props.setProperty("mail.debug""true");  
  6.         // 發送服務器需要身份驗證  
  7.         props.setProperty("mail.smtp.auth""true");  
  8.         // 設置郵件服務器主機名  
  9.         props.setProperty("mail.host""smtp.163.com");  
  10.         // 發送郵件協議名稱  
  11.         props.setProperty("mail.transport.protocol""smtp");  
  12.           
  13.         // 設置環境信息  
  14.         Session session = Session.getInstance(props);  
  15.           
  16.         // 創建郵件對象  
  17.         Message msg = new MimeMessage(session);  
  18.         msg.setSubject("JavaMail測試");  
  19.         // 設置郵件內容  
  20.         msg.setText("這是一封由JavaMail發送的郵件!");  
  21.         // 設置發件人  
  22.         msg.setFrom(new InternetAddress("[email protected]"));  
  23.           
  24.         Transport transport = session.getTransport();  
  25.         // 連接郵件服務器  
  26.         transport.connect("java_mail_001""javamail");  
  27.         // 發送郵件  
  28.         transport.sendMessage(msg, new Address[] {new InternetAddress("[email protected]")});  
  29.         // 關閉連接  
  30.         transport.close();  
  31.     }  
  32. }  
       最終運行後,郵件發送成功。由於我們開啓了debug調試,在控制檯可以看到JavaMail和服務器之間的交互信息記錄,可以發現,和Java Mail(一):telnet實現發送收取郵件中telnet下的命令及服務器反饋信息基本一致。
       創建Session對象時可能需要的屬性詳細信息如下:
Name Type Description
mail.debug boolean The initial debug mode. Default is false.
mail.from String The return email address of the current user, used by the InternetAddressmethodgetLocalAddress.
mail.mime.address.strict boolean The MimeMessage class uses the InternetAddress method parseHeader to parse headers in messages. This property controls the strict flag passed to theparseHeader method. The default is true.
mail.host String The default host name of the mail server for both Stores and Transports. Used if themail.protocol.host property isn't set.
mail.store.protocol String Specifies the default message access protocol. The Session methodgetStore() returns a Store object that implements this protocol. By default the first Store provider in the configuration files is returned.
mail.transport.protocol String Specifies the default message transport protocol. The Session methodgetTransport() returns a Transport object that implements this protocol. By default the first Transport provider in the configuration files is returned.
mail.user String The default user name to use when connecting to the mail server. Used if the mail.protocol.userproperty isn't set.
mail.protocol.class String Specifies the fully qualified class name of the provider for the specified protocol. Used in cases where more than one provider for a given protocol exists; this property can be used to specify which provider to use by default. The provider must still be listed in a configuration file.
mail.protocol.host String The host name of the mail server for the specified protocol. Overrides the mail.host property.
mail.protocol.port int The port number of the mail server for the specified protocol. If not specified the protocol's default port number is used.
mail.protocol.user String The user name to use when connecting to mail servers using the specified protocol. Overrides themail.user property. 

       更新於2014.01.06
       文中示例以及以後的示例中所用的郵箱賬戶均爲在163申請的測試賬戶,分別爲java_mail_001至java_mail_004,密碼均爲javamail。
       本文來自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,轉載請註明。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章