Java Mail(三):Session、Message详解
http://blog.csdn.net/ghsau/article/details/17909093
*************************************
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17909093,转载请注明。
上篇文章介绍了JavaMail并实现了一个发送邮件的简单示例,JavaMail
API使用上非常灵活,比如,服务器信息可以设置到Session中,也可以设置到Transport中,收件人可以设置到Message中,也可以设置到Transport中,如何使用,取决于我们应用程序中的实际情况。本文详细的介绍一下这三个类的主要方法。
Session
static Session |
getDefaultInstance(Properties props)Get the default Session object. |
static Session |
getDefaultInstance(Properties props,Authenticator authenticator)Get the default Session object. |
static Session |
getInstance(Properties props)Get a new Session object. |
static Session |
getInstance(Properties props,Authenticator authenticator)Get a new Session object. |
getDefaultInstance得到的始终是该方法初次创建的缺省的对象,而getInstance得到的始终是新的对象,Authenticator的使用后面会说到。通过Session可以创建Transport(用于发送邮件)和Store(用于接收邮件),Transport和Store是JavaMail
API中定义好的接口,通过上文我们知道JavaMail分为API和service
provider两部分,API定义了相关接口(eg.:Transport and Store),service
provider中实现了这些接口,这些实现类配置在名为javamail.providers或javamail.default.providers的文件中,该文件放在mail.jar/smtp.jar/pop3.jar/imap.jar中的META-INF下,文件内容格式如:
# JavaMail IMAP provider Sun Microsystems, Inc
protocol=imap; type=store; class=com.sun.mail.imap.IMAPStore; vendor=Sun Microsystems, Inc;
protocol=imaps; type=store; class=com.sun.mail.imap.IMAPSSLStore; vendor=Sun Microsystems, Inc;
# JavaMail SMTP provider Sun Microsystems, Inc
protocol=smtp; type=transport; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc;
protocol=smtps; type=transport; class=com.sun.mail.smtp.SMTPSSLTransport; vendor=Sun Microsystems, Inc;
# JavaMail POP3 provider Sun Microsystems, Inc
protocol=pop3; type=store; class=com.sun.mail.pop3.POP3Store; vendor=Sun Microsystems, Inc;
protocol=pop3s; type=store; class=com.sun.mail.pop3.POP3SSLStore; vendor=Sun Microsystems, Inc;
每一行声明了协议名称、类型、实现类、供应商、版本等信息,如果需要自己实现相应的协议,必须按照该格式配置好,Java Mail API中才能正确的调用到。Session中提供的创建Trasnsport和Store的方法如下:
Store |
getStore()Get a Store object that implements this user's desired Store protocol. |
Store |
getStore(Provider provider)Get an instance of the store specified by Provider. |
Store |
getStore(String protocol)Get a Store object that implements the specified protocol. |
Store |
getStore(URLName url)Get a Store object for the given URLName. |
Transport |
getTransport()Get a Transport object that implements this user's desired Transport protcol. |
Transport |
getTransport(Address address)Get a Transport object that can transport a Message of the specified address type. |
Transport |
getTransport(Provider provider)Get an instance of the transport specified in the Provider. |
Transport |
getTransport(String protocol)Get a Transport object that implements the specified protocol. |
Transport |
getTransport(URLName url)Get a Transport object for the given URLName. |
可以看到,重载了很多,这些方法最终都会去解析上文提到的配置文件,找到对应配置信息。
Message
Message是邮件的载体,用于封装邮件的所有信息,Message是一个抽象类,已知的实现类有MimeMessage。一封完整的邮件都有哪些信息呢?我们打开一个邮件客户端,我用的是FoxMail,新建一封邮件,如下图所示:

发件人
abstract void |
setFrom()Set the "From" attribute in this Message. |
abstract void |
setFrom(Address address)Set the "From" attribute in this Message. |
现在大多数SMTP服务器要求发件人与连接账户必须一致,否则会抛出验证失败的异常,这样是防止用户伪装成其它人的账户恶意发送邮件。
收件人/抄送人/暗送人
void |
setRecipient(Message.RecipientType type,Address address)Set the recipient address. |
abstract void |
setRecipients(Message.RecipientType type,Address[] addresses)Set the recipient addresses. |
第一个方法设置单个人,第二个方法设置多个人。方法中第一个参数涉及到另一个类RecipientType,该类是Message的静态内部类,期内有三个常量值:
static Message.RecipientType |
BCCThe "Bcc" (blind carbon copy) recipients. |
static Message.RecipientType |
CCThe "Cc" (carbon copy) recipients. |
static Message.RecipientType |
TOThe "To" (primary) recipients. |
TO - 收件人;CC - 抄送人;BCC - 暗送人。
回复人
void |
setReplyTo(Address[] addresses)Set the addresses to which replies should be directed. |
设置收件人收到邮件后的回复地址。
标题
abstract void |
setSubject(String subject)Set the subject of this message. |
设置邮件标题/主题。
内容
void |
setContent(Multipart mp)This method sets the given Multipart object as this message's content. |
void |
setContent(Object obj,String type)A convenience method for setting this part's content. |
void |
setText(String text)A convenience method that sets the given String as this part's content with a MIME type of "text/plain". |
设置邮件文本内容、HTML内容、附件内容。
示例
下面来看一个稍复杂点的示例:
public class JavaMailTest2 {
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, new Authenticator() {
// 在session中设置账户信息,Transport发送邮件时会使用
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("java_mail_001", "javamail");
}
});
// 创建邮件对象
Message msg = new MimeMessage(session);
// 发件人
msg.setFrom(new InternetAddress("java_mail_001@163.com"));
// 多个收件人
msg.setRecipients(RecipientType.TO, InternetAddress.parse("java_mail_002@163.com,java_mail_003@163.com"));
// 抄送人
msg.setRecipient(RecipientType.CC, new InternetAddress("java_mail_001@163.com"));
// 暗送人
msg.setRecipient(RecipientType.BCC, new InternetAddress("java_mail_004@163.com"));
// 主题
msg.setSubject("中文主题");
// HTML内容
msg.setContent("<div align=\"center\">你好啊</div>", "text/html;charset=utf-8");
// 连接邮件服务器、发送邮件、关闭连接,全干了
Transport.send(msg);
}
}
Java Mail(三):Session、Message详解的更多相关文章
- 【转】Cookie/Session机制详解
Cookie/Session机制详解 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息 ...
- Java网络编程和NIO详解开篇:Java网络编程基础
Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...
- java.util.logging.Logger使用详解 (转)
http://lavasoft.blog.51cto.com/62575/184492/ ************************************************* java. ...
- Java网络编程和NIO详解9:基于NIO的网络编程框架Netty
Java网络编程和NIO详解9:基于NIO的网络编程框架Netty 转自https://sylvanassun.github.io/2017/11/30/2017-11-30-netty_introd ...
- Java网络编程和NIO详解5:Java 非阻塞 IO 和异步 IO
Java网络编程和NIO详解5:Java 非阻塞 IO 和异步 IO Java 非阻塞 IO 和异步 IO 转自https://www.javadoop.com/post/nio-and-aio 本系 ...
- java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET
java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了! 社区福利快来领取免费参加MDCC大会机会哦 Tag功能介绍—我们 ...
- Java中的main()方法详解
在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...
- [译]Java Thread join示例与详解
Java Thread join示例与详解 Java Thread join方法用来暂停当前线程直到join操作上的线程结束.java中有三个重载的join方法: public final void ...
- Java虚拟机之垃圾回收详解一
Java虚拟机之垃圾回收详解一 Java技术和JVM(Java虚拟机) 一.Java技术概述: Java是一门编程语言,是一种计算平台,是SUN公司于1995年首次发布.它是Java程序的技术基础,这 ...
- Hibernate Session & Transaction详解
Hibernate Session & Transaction详解 HIbernate中的Session Session是JAVA应用程序和Hibernate进行交互时使用的主要接口,它也是持 ...
随机推荐
- Item 5:那些被C++默默地声明和调用的函数 Effective C++笔记
Item 5: Know what functions C++ silently writes and calls 在C++中,编译器会自己主动生成一些你没有显式定义的函数,它们包含:构造函数.析构函 ...
- DWG/DGN格式导入Arcgis;转化为shp格式;更改地理坐标;导入Google Earth【转】
其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK8 ...
- ECharts学习总结(五):echarts的Option概览
注:下面内容摘自echarts官网,原文地址:http://echarts.baidu.com/doc/doc.html#%E9%80%89%E9%A1%B9 option 图表选项,包含图表实例任何 ...
- TensorFlow 入门 下(自用)
下文会出现以下知识点:神经网络的计算流程. TensorFlow游乐场: 网址:http://playground.tensorflow.org. 神经网络简介: 在机器学习中,所有描述一个实体的数字 ...
- 转:RSA算法原理说明
转:http://www.joenchen.com/archives/979 RSA算法可以说在我们使用计算机的每一方面都在发挥着作用, EXE文件的签名算法用的是SHA1 + RSA. 我们每天登陆 ...
- 依据硬件设备配置高性能的Nginx
Nginx的高级配置会涉及硬件,假设配置不好,会直接让性能下降好多好多. 我这里总结一下,怎样依据server的硬件设备来配置Nginx. 见下图: 低訪问量的网络,能够这样配置. 标准的网络訪问量, ...
- PHP 正则表达式(PCRE)
PHP 正则表达式(PCRE) 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串 ...
- OpenERP登录页面调整
在OpenERP的登录页面中,有针对数据库管理的链接,为了安全起见,一般都会通过修改原始的XML来实现隐藏的目的.但这样每次重新安装以后,都要重新修改,很不方便,所以我们可以通过建立一个新模块的方式来 ...
- 多对一关系表 java类描述
少的一方把它查询出来,多的一方看需求把它查出来 涉及java对象涉及到多个对象相互引用,要尽量避免使用一对多,或多对多关系,而应使用多对一描述对象之间的关系(或使用延迟加载的方式). 下个例子empl ...
- Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们
转载自:http://www.open-open.com/lib/view/open1436360508098.html