当前位置: 移动技术网 > IT编程>开发语言>Java > 使用Spring的JAVA Mail支持简化邮件发送功能

使用Spring的JAVA Mail支持简化邮件发送功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

闲来无事,翻看《spring in action》,发现spring集成了对java mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多。

spring的邮件发送的核心是mailsender接口,在spring3.0中提供了一个实现类javamailsenderimpl,这个类是发送邮件的核心类。可以通过在配置文件中配置使用,当然也可以自己硬编码到代码中(方便起见,下面的演示代码都是硬编码到代码中,省得配置麻烦)。

spring提供的邮件发送不仅支持简单邮件的发送、添加附件,而且还可以使用velocity模板控制页面样式(应该也支持freemarker)。

首先对加入相应spring jar包和java mail 的jar包。

我们首先得声明一个mailsender对象,因为mailsender对象只有两个重载的send(...)方法,显得有些简陋,我们建议选用javamailsender接口,或者干脆直接使用实现类,javamailsenderimpl。笔者是使用的javamailsenderimpl对象,功能丰富。

声明javamailsenderimpl对象,并在构造函数中初始化(当然也可以使用ioc容器初始化):

public class springmailsender
 {
 
 //
 spring的邮件工具类,实现了mailsender和javamailsender接口
 private javamailsenderimpl
 mailsender;
  
 public springmailsender()
 {
 //
 初始化javamailsenderimpl,当然推荐在spring配置文件中配置,这里是为了简单
 mailsender
 = new javamailsenderimpl();
 //
 设置参数
 mailsender.sethost("smtp.qq.com");
 mailsender.setusername("mosaic@qq.com");
 mailsender.setpassword("asterisks");
 ...

得到了mailsender对象之后,就可以发送邮件了,下面是示例代码,没有封装,仅供参考。

1、发送简单邮件

/**
 *
 简单邮件发送
 *
 */
public void simplesend()
 {
 //
 构建简单邮件对象,见名知意
 simplemailmessage
 smm = new simplemailmessage();
 //
 设定邮件参数
 smm.setfrom(mailsender.getusername());
 smm.setto("mosaic@126.com");
 smm.setsubject("hello
 world");
 smm.settext("hello
 world via spring mail sender");
 //
 发送邮件
 mailsender.send(smm);
}

2、发送带附件的邮件

/**
 *
 带附件的邮件发送
 *
 *
 @throws messagingexception
 */
public void attachedsend()
throws messagingexception
 {
 //使用javamail的mimemessage,支付更加复杂的邮件格式和内容
 mimemessage
 msg = mailsender.createmimemessage();
 //创建mimemessagehelper对象,处理mimemessage的辅助类
 mimemessagehelper
 helper = new mimemessagehelper(msg,
true);
 //使用辅助类mimemessage设定参数
 helper.setfrom(mailsender.getusername());
 helper.setto("mosaic@126.com");
 helper.setsubject("hello
 attachment");
 helper.settext("this
 is a mail with attachment");
 //加载文件资源,作为附件
 classpathresource
 file = new classpathresource(
   "chrysanthemum.jpg");
 //加入附件
 helper.addattachment("attachment.jpg",
 file);
 //发送邮件
 mailsender.send(msg);
}

3、发送富文本邮件

/**发送富文本邮件
 *
 @throws messagingexception
 */
public void richcontentsend()
throws messagingexception
 {
 mimemessage
 msg = mailsender.createmimemessage();
 
 mimemessagehelper
 helper = new mimemessagehelper(msg,
true);
 
 helper.setfrom(mailsender.getusername());
 helper.setto("mosaic@126.com");
 helper.setsubject("rich
 content mail");
 //第二个参数true,表示text的内容为html,然后注意<img/>标签,src='cid:file','cid'是contentid的缩写,'file'是一个标记,需要在后面的代码中调用mimemessagehelper的addinline方法替代成文件
 helper.settext(
   "<body><p>hello
 html email</p><img src='cid:file'/></body>",
   true);
 
 filesystemresource
 file = new filesystemresource(
   "c:\\users\\public\\pictures\\sample
 pictures\\chrysanthemum.jpg");
 helper.addinline("file",
 file);
 
 mailsender.send(msg);
}

 4、使用velocity模板确定邮件风格

使用velocity模板,需要velocity的jar包,可以在官方网站下载,并加入classpath,然后需要声明一个velocityengine对象,具体的参考下面代码,这是笔者第一次使用velocity,不甚了解,言多有失,望见谅。

声明一个velocityengine对象,并在构造函数中初始化(ioc is optional)

...
private velocityengine
 velocityengine;
 
public springmailsender()
 {
  ...
 //
 velocity的参数,通过velocityenginefactorybean创建velocityengine,也是推荐在配置文件中配置的
 properties
 props = system.getproperties();
 props.put("resource.loader",
"class");
 props
   .put("class.resource.loader.class",
     "org.apache.velocity.runtime.resource.loader.classpathresourceloader");
 velocityenginefactorybean
 v = new velocityenginefactorybean();
 v.setvelocityproperties(props);
 try {
  velocityengine
 = v.createvelocityengine();
 }
catch (velocityexception
 e) {
  e.printstacktrace();
 }
catch (ioexception
 e) {
  e.printstacktrace();
 }
 
}

简单的velocity模板文件(index.vm):

<html>
<head>
<style type="text/css">
h4{
 color:red;
 background:#efefef;
}
</style>
</head>
<body>
<h4>${user}
 </h4>
<p><p>
<i>${content}</i>
</body>
</html>

开起来貌似很容易理解,只是普通的html文件,使用了一些${placeholder}作为占位符。

java要做的,就是加载模板,并将相应的值插入到占位符当中。

/**
 *
 使用velocity模板发送邮件
 *
 *
 @throws messagingexception
 */
public
 void templatesend() throws messagingexception {
 //
 声明map对象,并填入用来填充模板文件的键值对
 map<string,
 string> model = new hashmap<string,
 string>();
 model.put("user",
 "mzule");
 model.put("content",
 "hello");
 //
 spring提供的velocityengineutils将模板进行数据填充,并转换成普通的string对象
 string
 emailtext = velocityengineutils.mergetemplateintostring(
   velocityengine,
 "index.vm", model);
 //
 和上面一样的发送邮件的工作
 mimemessage
 msg = mailsender.createmimemessage();
 mimemessagehelper
 helper = new mimemessagehelper(msg, true);
 helper.setfrom(mailsender.getusername());
 helper.setto("mosaic@126.com");
 helper.setsubject("rich
 content mail");
 helper.settext(emailtext,
 true);
 
 mailsender.send(msg);
}

spring可谓是大大简化了邮件的发送步骤,虽然我们自己封装可能实现起来并不复杂,但是,有现成的有何必要重新造轮子呢?(当然造轮子可以学到很多)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网