使用spring 并加载模板发送Email 发邮件 java 模板
以下例子是使用spring发送email,然后加载到固定的模板,挺好的,大家可以试试
需要使用到spring-context 包 和 com.springsource.org.apache.velocity-1.6.2.jar 其他都是常用包,自选,包括commons 系列包
代码如下
package asia.wildfire.mail; import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import javax.mail.internet.MimeMessage; import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils; /**
* Created with IntelliJ IDEA.
* User: liuxiaochen
* Date: 13-9-22
* Time: 下午6:50
* 修改描述
*/
public class ActsocialMailSender {
//从配置文件中读取相应的邮件配置属性
private static final String emailHost = "smtp.gmail.com";
private static final String userName = "****@gmail.com";
private static final String password = "********";
private static final String mailAuth = "true";
private static Map<String, Object> proMap = null;
private static JavaMailSenderImpl instance = null;
private static VelocityEngine velocityEngine = null; static {
proMap = new HashMap<String, Object>();
proMap.put("resource.loader", "class");
proMap.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); } public static JavaMailSender getInstance() {
if (null == instance) {
synchronized (JavaMailSenderImpl.class) {
if (null == instance) {
instance = new JavaMailSenderImpl();
instance.setHost(emailHost);
instance.setUsername(userName);
instance.setPassword(password);
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", mailAuth);
//使用gmail发送邮件是必须设置如下参数的 主要是port不一样
if (emailHost.indexOf("smtp.gmail.com")>=0) {
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.port", "465");
properties.setProperty("mail.smtp.socketFactory.port", "465");
}
instance.setJavaMailProperties(properties);
}
}
} return instance;
} public static VelocityEngine getVelocityEngineInstance() {
if (null == velocityEngine) {
synchronized (VelocityEngine.class) {
if (null == velocityEngine) {
velocityEngine = new VelocityEngine();
for (Map.Entry<String, Object> entry : proMap.entrySet()) {
velocityEngine.setProperty(entry.getKey(), entry.getValue());
}
}
}
}
return velocityEngine;
} public static void sendEmail(final Map<String,Object> model,final String subject,final String vmfile,final String[] mailTo,final String [] files)
{
MimeMessagePreparator preparator = new MimeMessagePreparator() {
//注意MimeMessagePreparator接口只有这一个回调函数
public void prepare(MimeMessage mimeMessage) throws Exception
{
MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true,"GBK");
//这是一个生成Mime邮件简单工具,如果不使用GBK这个,中文会出现乱码
//如果您使用的都是英文,那么可以使用MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(mailTo);//设置接收方的email地址
message.setSubject(subject);//设置邮件主题
message.setFrom(userName);//设置发送方地址
String text = VelocityEngineUtils.mergeTemplateIntoString(
ActsocialMailSender.getVelocityEngineInstance(), vmfile, "UTF-8", model);
//从模板中加载要发送的内容,vmfile就是模板文件的名字
//注意模板中有中文要加GBK,model中存放的是要替换模板中字段的值
message.setText(text, true);
//将发送的内容赋值给MimeMessageHelper,后面的true表示内容解析成html
//如果您不想解析文本内容,可以使用false或者不添加这项
FileSystemResource file;
for(String s:files)//添加附件
{
file = new FileSystemResource(new File(s));//读取附件
message.addAttachment(s, file);//向email中添加附件
}
}
};
ActsocialMailSender.getInstance().send(preparator);//发送邮件
} public static void sendAlertEmail(final Map<String,Object> model,final String[] mailTo){
sendEmail(model, "", "", mailTo, new String[]{});
}
}
以上代码就是发送邮件所需要的代码,可以看下
测试类
public class ActsocialEmailTest {
public static void main(String[] args) {
Map<String,Object> model = new HashMap<String,Object>();
model.put("userName","xiaochen.liu");
model.put("emailAddress", "xiaochen.liu@xingxinghuo.com");
ActsocialMailSender.sendEmail(model,"欢迎加入", "welcome.vm",new String[]{"****@gmail.com"},new String[]{});
}
}
模板文件如下
welcome.vm
<html>
<body>
<h3>您好 $!{userName}, 欢迎您加入actsocial!</h3> <div>
您的email地址是<a href="mailto:${emailAddress}">$!{emailAddress}</a>.
本条信息是系统自动发送,请勿回复!
</div>
</body> </html>
好了就这么多
使用spring 并加载模板发送Email 发邮件 java 模板的更多相关文章
- 廖雪峰Java13网络编程-2Email编程-1发送email
1.邮件发送 1.1传统邮件发送: 传统的邮件是通过邮局投递,从一个邮局到另一个邮局,最终到达用户的邮箱. 1.2电子邮件发送: 与传统邮件类似,它是从用户电脑的邮件软件(如outlook)发送到邮件 ...
- iOS 打电话、发短信、发邮件功能
打电话 方法1 最简单最直接的方式:直接跳到拨号界面 NSURL *url = [NSURL URLWithString:@"tel://10010"]; [[UIApplicat ...
- iOS中打电话、打开网址、发邮件、发短信等
常用小功能 小功能简介 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等 打电话-方法1 最简单最直接的方式:直接跳到拨号界面 NSURL *url = [ ...
- AJ学IOS(45)之常用的小功能比如打电话、打开网址、发邮件、发短信打开其他应用。
AJ分享,必须精品 简介 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信.打开其他应用等. 打电话 方法1 最简单最直接的方式:直接跳到拨号界面 NSURL ...
- Python【yagmail】模块发邮件
#步骤一:import yagmail #步骤二:实例化一个发邮件的对象username = '553637138@qq.com' #邮箱账号pwd='sa2008' #授权码mail = yagma ...
- 使用Spring发送Email
配置Spring发送邮件 Spring发送邮件底层还是使用JavaMail,我在http://www.cnblogs.com/lz2017/p/6882925.html 中记录过关于JavaMail的 ...
- 第19章-使用Spring发送Email
1 配置Spring发送邮件 Spring Email抽象的核心是MailSender接口.顾名思义,MailSender的实现能够通过连接Email服务器实现邮件发送的功能,如图19.1所示. 图1 ...
- Spring 发送 Email
本文转自:http://zl198751.iteye.com/blog/757617 看到了本文,收获颇丰,感谢之至! 首先介绍下Email的发送流程: 需要选中smtp邮件服务器,Yahoo不提供免 ...
- spring boot 学习(十)SpringBoot配置发送Email
SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...
随机推荐
- 把Go程序变小的办法
把Go程序变小的办法是: go build -ldflags “-s -w” (go install类似) -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了, 这 ...
- iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用
iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...
- CSS的优先级
样式的优先级: (内联样式表[嵌入式样式])>(内部样式表)>(外部样式表) 经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆 ...
- Struts2中有关struts-default.xml,struts.xml,struts.properties文件详解
1) struts-default.xml 这个文件是struts2框架默认加载的配置文件.它定义struts2一些核心的bean和拦截器. <?xml version="1.0&qu ...
- (原)编译caffe时提示未定义的引用(undefined reference to)
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5864715.html 参考网址: https://github.com/BVLC/caffe/issu ...
- (原)Ubuntu16中卸载并重新安装google的Protocol Buffers
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5782992.html 目前最新的是1.6.1 1. 卸载掉老版本的Protocol: sudo apt ...
- (原)C++中测试代码执行时间
转载请注明出处(不过这个用法网上到处都是): http://www.cnblogs.com/darkknightzh/p/4987738.html LARGE_INTEGER nFreq, nBegi ...
- ajax 传值 中文乱码问题
使用encodeURI编码内容 var Path = encodeURI("中文.xls"); url: "ashx/Data.ashx?Path =" + P ...
- 做了一个js的拉动遮罩层,两个图片分别显示的效果
想做成车修好了和没修好的对比,所以需要两个图片.需要用到的知识点, 1.定位 2.mouse 的事件(代码中体现) 3.鼠标指针的移动距离算法 4.css中,cursor的应用 好了,废话不多说 ,直 ...
- URL锚点定位
我们都知道<a>标签中的url属性有三种值: 绝对 URL - 指向另一个站点(比如 href="http://www.example.com/index.htm") ...