用Spring让Java Mail支持简化邮件发送

今天我们谈到spring提供的邮件发送不仅支持简单邮件的发送、添加附件,而且还可以使用velocity模板控制页面样式。

闲来无事,翻看《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("786553789@qq.com");

mailsender.setpassword("556wuli779");

得到了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,然后注意标签,src='cid:file',

'cid'是contentid的缩写,'file'是一个标记,

需要在后面的代码中调用mimemessagehelper的addinline方法替代成文件

helper.settext(

"

hello html email

",

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):

${user}

${content}

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

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

/**

* 使用velocity模板发送邮件

*

* @throws messagingexception

*/

public void templatesend() throws messagingexception {

// 声明map对象,并填入用来填充模板文件的键值对

map model = new hashmap();

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

绿色通道:好文要顶关注我收藏该文与我联系


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值