国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 10:55:07
字體:
來源:轉載
供稿:網友

 本文來自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,#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下載好后,我們來看一下其主要內容:

[plain] view plain copy print?在CODE上查看代碼片README.txt:整體介紹JavaMail,需要看一下  docs/javadocs:The JavaMail API javadocs,需要看一下  mail.jar:包括JavaMail API和所有service providers,大部分用戶只需要該jar包  lib/mailapi.jar :只有JavaMail API  lib/imap.jar:The IMAP service provider  lib/smtp.jar:The SMTP service provider  lib/pop3.jar:The POP3 service provider  lib/dsn.jar:multjavax.mail.session:上下文環境信息,如服務器的主機名、端口號、協議名稱等  javax.mail.Message:郵件模型,發送郵件和接收郵件的媒介,封裝了郵件的信息,如發件人、收件人、郵件標題、郵件內容等  javax.mail.Transport:連接郵件SMTP服務器,發送郵件  javax.mail.Store:連接郵件POP3、IMAP服務器,收取郵件   %20 %20 %20 通過這些類,最終就可以實現收發郵件,一個發送郵件的簡單示例:[java] view%20plain copy print?派生到我的代碼片public class JavaMailTest1 {      public static void main(String[] args) throws MessagingException {          Properties props = new Properties();          // 開啟debug調試          props.setProperty("mail.debug", "true");          // 發送服務器需要身份驗證          props.setProperty("mail.smtp.auth", "true");          // 設置郵件服務器主機名          props.setProperty("mail.host", "smtp.163.com");          // 發送郵件協議名稱          props.setProperty("mail.transport.protocol", "smtp");                    // 設置環境信息          Session session = Session.getInstance(props);                    // 創建郵件對象          Message msg = new MimeMessage(session);          msg.setSubject("JavaMail測試");          // 設置郵件內容          msg.setText("這是一封由JavaMail發送的郵件!");          // 設置發件人          msg.setFrom(new InternetAddress("java_mail_001@163.com"));                    Transport transport = session.getTransport();          // 連接郵件服務器          transport.connect("java_mail_001", "javamail");          // 發送郵件          transport.sendMessage(msg, new Address[] {new InternetAddress("java_mail_002@163.com")});          // 關閉連接          transport.close();      }  }         最終運行后,郵件發送成功。由于我們開啟了debug調試,在控制臺可以看到JavaMail和服務器之間的交互信息記錄,可以發現,和Java Mail(一):telnet實現發送收取郵件中telnet下的命令及服務器反饋信息基本一致。       創建Session對象時可能需要的屬性詳細信息如下:
NameTypeDescription
mail.debugbooleanThe initial debug mode. Default is false.
mail.fromStringThe return email address of the current user, used by the InternetAddressmethodgetLocalAddress.
mail.mime.address.strictbooleanThe 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.hostStringThe default host name of the mail server for both Stores and Transports. Used if themail.protocol.host property isn't set.
mail.store.protocolStringSpecifies the default message access protocol. The SessionmethodgetStore() returns a Store object that implements this protocol. By default the first Store provider in the configuration files is returned.
mail.transport.protocolStringSpecifies the default message transport protocol. The SessionmethodgetTransport() returns a Transport object that implements this protocol. By default the first Transport provider in the configuration files is returned.
mail.userStringThe default user name to use when connecting to the mail server. Used if the mail.protocol.user property isn't set.
mail.protocol.classStringSpecifies 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.hostStringThe host name of the mail server for the specified protocol. Overrides the mail.host property.
mail.protocol.portintThe port number of the mail server for the specified protocol. If not specified the protocol's default port number is used.
mail.protocol.userStringThe 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,轉載請注明。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 信阳市| 孟连| 河北省| 二手房| 大港区| 防城港市| 美姑县| 晋城| 曲沃县| 河北省| 龙泉市| 乐业县| 宣威市| 清流县| 沂水县| 胶州市| 周口市| 林甸县| 无极县| 衡阳县| 江西省| 昆明市| 霞浦县| 宜君县| 荣成市| 尼勒克县| 潮州市| 河北省| 白沙| 济宁市| 冕宁县| 武冈市| 全椒县| 襄垣县| 中宁县| 枣阳市| 西宁市| 黎城县| 和静县| 庄河市| 汤原县|