java SpringMVC 邮件发送配置及Code
来源:互联网 发布:淘宝没有代付选项 编辑:程序博客网 时间:2024/04/30 04:15
首先是spring中的配置:(我里面使用了FreeMarker模板引擎)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!--邮件发送实现类-->
<
bean
id
=
"javaMailSender"
class
=
"org.springframework.mail.javamail.JavaMailSenderImpl"
>
<
property
name
=
"host"
value
=
"smtp.qq.com"
/>
<
property
name
=
"port"
value
=
"25"
/>
<
property
name
=
"username"
value
=
"你的邮箱"
/>
<
property
name
=
"password"
value
=
"你的邮箱密码"
/>
<
property
name
=
"javaMailProperties"
>
<
props
>
<
prop
key
=
"mail.smtp.auth"
>true</
prop
>
</
props
>
</
property
>
</
bean
>
<
bean
id
=
"simpleMailMessage"
class
=
"org.springframework.mail.SimpleMailMessage"
>
<
property
name
=
"from"
value
=
"发件人"
/>
<
property
name
=
"subject"
value
=
"邮件主题"
/>
<!--
<property name="text" value="邮件内容" />
-->
</
bean
>
<!--线程池异步发送邮件-->
<
bean
id
=
"taskExecutor"
class
=
"org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
>
<
property
name
=
"corePoolSize"
value
=
"5"
/>
<
property
name
=
"maxPoolSize"
value
=
"10"
/>
<
property
name
=
"queueCapacity"
value
=
"25"
/>
</
bean
>
<!--FreeMarker模板-->
<
bean
id
=
"freeMarkerConfigurer"
class
=
"org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"
>
<
property
name
=
"templateLoaderPath"
value
=
"classpath:template"
/>
<!--<property name="templateLoaderPath" value="/template" />-->
<
property
name
=
"freemarkerSettings"
>
<
props
>
<
prop
key
=
"locale"
>zh_CN</
prop
>
<
prop
key
=
"default_encoding"
>UTF-8</
prop
>
</
props
>
</
property
>
</
bean
>
发送邮件的工具类:(采用注解形式注入管理邮件发送的类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public
class
MailSender {
@Inject
private
JavaMailSender javaMailSender;
@Inject
private
SimpleMailMessage simpleMailMessage;
@Inject
private
FreeMarkerConfigurer freeMarkerConfigurer;
@Inject
private
TaskExecutor taskExecutor;
/**
* 构建邮件内容,发送邮件。
* @param user 用户
* @param url 链接
*/
public
void
send(User user, String url,String email) {
String nickname = user.getNickname();
String to = email;
String text =
""
;
Map<String, String> map =
new
HashMap<String, String>(
1
);
map.put(
"url"
, url);
try
{
// 从FreeMarker模板生成邮件内容
Template template = freeMarkerConfigurer.getConfiguration().getTemplate(
"register_mail.ftl"
);
// 模板中用${XXX}站位,map中key为XXX的value会替换占位符内容。
text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(TemplateException e) {
e.printStackTrace();
}
this
.taskExecutor.execute(
new
SendMailThread(to,
null
,text));
}
// 内部线程类,利用线程池异步发邮件。
private
class
SendMailThread
implements
Runnable {
private
String to;
private
String subject;
private
String content;
private
SendMailThread(String to, String subject, String content) {
super
();
this
.to = to;
this
.subject = subject;
this
.content = content;
}
@Override
public
void
run() {
sendMail(to, subject, content);
}
}
/**
* 发送邮件
* @param to 收件人邮箱
* @param subject 邮件主题
* @param content 邮件内容
*/
public
void
sendMail(String to, String subject, String content) {
try
{
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper =
new
MimeMessageHelper(message,
true
,
"UTF-8"
);
messageHelper.setFrom(simpleMailMessage.getFrom());
if
(subject !=
null
) {
messageHelper.setSubject(subject);
}
else
{
messageHelper.setSubject(simpleMailMessage.getSubject());
}
messageHelper.setTo(to);
messageHelper.setText(content,
true
);
javaMailSender.send(message);
}
catch
(MessagingException e) {
e.printStackTrace();
}
}
}
服务层调用:
1、要注入发送邮件工具类
1
2
@Inject
private
MailSender mailSender;
2、调用代码
1
2
3
4
User user = (User)request.getSession().getAttribute(
"user"
);
String url = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+
request.getContextPath();
mailSender.send(user,url,email);
0 0
- java SpringMVC 邮件发送配置及Code
- java SpringMVC 邮件发送配置及Code
- JAVA发送邮件及springboot发送邮件
- redmine配置及邮件发送
- linux 发送邮件配置及使用
- springMVC中发送邮件
- springMVC实现邮件发送
- springmvc发送邮件
- Apache James邮件服务器配置及发送外网邮件
- Ubuntu Linux自动发邮件配置及邮件发送脚本
- Apache James邮件服务器配置及发送外网邮件
- Apache James邮件服务器配置及发送外网邮件
- Apache James邮件服务器配置及发送外网邮件
- Apache James邮件服务器配置及发送外网邮件
- java发送邮件及邮件附件代码实例
- Java发送邮件消息及超链接
- spring 配置 java mail 发送邮件
- Java通过Spring配置发送QQ邮件
- Tsar学习笔记----安装篇
- hadoop中使用MapReduce编程实例(转)
- Known Issues: Grid Infrastructure Redundant Interconnect and ora.cluster_interconnect.haip (Doc ID 1
- js 读取excel 文件
- iOS 开发中遇到的各种证书的详细说明以及申请讲解
- java SpringMVC 邮件发送配置及Code
- short url 算法
- 关于RAC Interconnect之HAIP
- 解决Unix-like、Windows系统之间文本换行符不同的问题
- IOS打包验证时,提示使用了私有API的问题
- Python学习03--流程控制语句
- 织梦仿站系列教程第十七讲——通用底部的制作
- Log4J详细配置
- mysql操作类