本文來自:高爽|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 spec:http://www.oracle.com/technetwork/java/javamail-1-149769.pdf
JavaMail下載好後,我們來看一下其主要內容:
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: 通過這些類,最終就可以實現收發郵件,一個發送郵件的簡單示例: 最終運行後,郵件發送成功。由於我們開啓了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 InternetAddress methodgetLocalAddress . |
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.user property 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,轉載請註明。