前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java Mail(三):Session、Message详解

Java Mail(三):Session、Message详解

作者头像
高爽
发布2018-01-15 14:42:06
8990
发布2018-01-15 14:42:06
举报
文章被收录于专栏:高爽的专栏高爽的专栏

上篇文章介绍了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.providersjavamail.default.providers的文件中,该文件放在mail.jar/smtp.jar/pop3.jar/imap.jar中的META-INF下,文件内容格式如:

代码语言:javascript
复制
# 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,新建一封邮件,如下图所示:

       这就是一封完整的邮件包含的所有信息,默认情况下是没有暗送和回复设置的,可以通过菜单栏-->查看-->暗送地址/回复地址来显示出来,回复地址默认情况下为发件人,暗送是比较猥琐的发邮件方式,暗送邮件除了被暗送者,没有人能知道暗送给谁了,邮件头信息中也不会记录。下面来看下Message中设置邮件信息的一些方法。

发件人

abstract  void

setFrom()          Set the "From" attribute in this Message.

abstract  void

setFrom(Address address)          Set the "From" attribute in this Message.

收件人/抄送人/暗送人

void

setRecipient(Message.RecipientType type,Address address)          Set the recipient address.

abstract  void

setRecipients(Message.RecipientType type,Address[] addresses)          Set the recipient addresses.

回复人

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".

示例

       下面来看一个稍复杂点的示例:

代码语言:javascript
复制
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);
	}

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-01-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Session
  • Message
    • 发件人
      • 收件人/抄送人/暗送人
        • 回复人
          • 标题
            • 内容
            • 示例
            相关产品与服务
            多因子身份认证
            多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档