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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

JavaMail介紹及發(fā)送一封簡單郵件

2019-11-14 10:54:24
字體:
供稿:網(wǎng)友

 本文來自:高爽|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下載好后,我們來看一下其主要內(nèi)容:

[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:上下文環(huán)境信息,如服務(wù)器的主機(jī)名、端口號(hào)、協(xié)議名稱等  javax.mail.Message:郵件模型,發(fā)送郵件和接收郵件的媒介,封裝了郵件的信息,如發(fā)件人、收件人、郵件標(biāo)題、郵件內(nèi)容等  javax.mail.Transport:連接郵件SMTP服務(wù)器,發(fā)送郵件  javax.mail.Store:連接郵件POP3、IMAP服務(wù)器,收取郵件   %20 %20 %20 通過這些類,最終就可以實(shí)現(xiàn)收發(fā)郵件,一個(gè)發(fā)送郵件的簡單示例:[java] view%20plain copy print?派生到我的代碼片public class JavaMailTest1 {      public static void main(String[] args) throws MessagingException {          Properties props = new Properties();          // 開啟debug調(diào)試          props.setProperty("mail.debug", "true");          // 發(fā)送服務(wù)器需要身份驗(yàn)證          props.setProperty("mail.smtp.auth", "true");          // 設(shè)置郵件服務(wù)器主機(jī)名          props.setProperty("mail.host", "smtp.163.com");          // 發(fā)送郵件協(xié)議名稱          props.setProperty("mail.transport.protocol", "smtp");                    // 設(shè)置環(huán)境信息          Session session = Session.getInstance(props);                    // 創(chuàng)建郵件對(duì)象          Message msg = new MimeMessage(session);          msg.setSubject("JavaMail測(cè)試");          // 設(shè)置郵件內(nèi)容          msg.setText("這是一封由JavaMail發(fā)送的郵件!");          // 設(shè)置發(fā)件人          msg.setFrom(new InternetAddress("java_mail_001@163.com"));                    Transport transport = session.getTransport();          // 連接郵件服務(wù)器          transport.connect("java_mail_001", "javamail");          // 發(fā)送郵件          transport.sendMessage(msg, new Address[] {new InternetAddress("java_mail_002@163.com")});          // 關(guān)閉連接          transport.close();      }  }         最終運(yùn)行后,郵件發(fā)送成功。由于我們開啟了debug調(diào)試,在控制臺(tái)可以看到JavaMail和服務(wù)器之間的交互信息記錄,可以發(fā)現(xiàn),和Java Mail(一):telnet實(shí)現(xiàn)發(fā)送收取郵件中telnet下的命令及服務(wù)器反饋信息基本一致。       創(chuàng)建Session對(duì)象時(shí)可能需要的屬性詳細(xì)信息如下:
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申請(qǐng)的測(cè)試賬戶,分別為java_mail_001至java_mail_004,密碼均為javamail。       本文來自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,轉(zhuǎn)載請(qǐng)注明。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉屏| 于都县| 黄龙县| 康马县| 无锡市| 拜城县| 兴安县| 永年县| 深泽县| 自贡市| 岗巴县| 咸阳市| 鹿邑县| 榕江县| 兴义市| 昔阳县| 关岭| 乌拉特后旗| 垫江县| 沂南县| 平顺县| 航空| 休宁县| 天津市| 镇远县| 金塔县| 乌鲁木齐县| 沈丘县| 普兰店市| 九江市| 余干县| 龙泉市| 乌拉特前旗| 亳州市| 青神县| 五大连池市| 应城市| 庆元县| 星子县| 兴宁市| 施甸县|