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

首頁 > 學院 > 開發(fā)設計 > 正文

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

2019-11-14 10:50:22
字體:
供稿:網(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)境信息,如服務器的主機名、端口號、協(xié)議名稱等  javax.mail.Message:郵件模型,發(fā)送郵件和接收郵件的媒介,封裝了郵件的信息,如發(fā)件人、收件人、郵件標題、郵件內(nèi)容等  javax.mail.Transport:連接郵件SMTP服務器,發(fā)送郵件  javax.mail.Store:連接郵件POP3、IMAP服務器,收取郵件   %20 %20 %20 通過這些類,最終就可以實現(xiàn)收發(fā)郵件,一個發(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ā)送服務器需要身份驗證          props.setProperty("mail.smtp.auth", "true");          // 設置郵件服務器主機名          props.setProperty("mail.host", "smtp.163.com");          // 發(fā)送郵件協(xié)議名稱          props.setProperty("mail.transport.protocol", "smtp");                    // 設置環(huán)境信息          Session session = Session.getInstance(props);                    // 創(chuàng)建郵件對象          Message msg = new MimeMessage(session);          msg.setSubject("JavaMail測試");          // 設置郵件內(nèi)容          msg.setText("這是一封由JavaMail發(fā)送的郵件!");          // 設置發(fā)件人          msg.setFrom(new InternetAddress("java_mail_001@163.com"));                    Transport transport = session.getTransport();          // 連接郵件服務器          transport.connect("java_mail_001", "javamail");          // 發(fā)送郵件          transport.sendMessage(msg, new Address[] {new InternetAddress("java_mail_002@163.com")});          // 關(guān)閉連接          transport.close();      }  }         最終運行后,郵件發(fā)送成功。由于我們開啟了debug調(diào)試,在控制臺可以看到JavaMail和服務器之間的交互信息記錄,可以發(fā)現(xiàn),和Java Mail(一):telnet實現(xiàn)發(fā)送收取郵件中telnet下的命令及服務器反饋信息基本一致。       創(chuàng)建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,轉(zhuǎn)載請注明。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 措美县| 余庆县| 十堰市| 夏河县| 读书| 宽甸| 江孜县| 崇文区| 和静县| 宽城| 渑池县| 建阳市| 涞源县| 滦平县| 称多县| 巫溪县| 通山县| 塘沽区| 兰溪市| 雷波县| 黄骅市| 辽中县| 怀化市| 天全县| 和硕县| 布拖县| 岑巩县| 鹰潭市| 古田县| 嘉义市| 陇南市| 广南县| 海伦市| 娄烦县| 汾阳市| 宜都市| 田东县| 桓仁| 宝兴县| 随州市| 勃利县|