首页 > 代码库 > SpringMVC 邮件发送
SpringMVC 邮件发送
<!--邮件发送实现类-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="http://www.mamicode.com/smtp.qq.com"/>
<property name="port" value="http://www.mamicode.com/25"/>
<property name="username" value="http://www.mamicode.com/你的邮箱"/>
<property name="password" value="http://www.mamicode.com/你的邮箱密码"/>
<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="http://www.mamicode.com/发件人" />
<property name="subject" value="http://www.mamicode.com/邮件主题" />
<!--
<property name="text" value="http://www.mamicode.com/邮件内容" />
-->
</bean>
<!--线程池异步发送邮件-->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="http://www.mamicode.com/5" />
<property name="maxPoolSize" value="http://www.mamicode.com/10" />
<property name="queueCapacity" value="http://www.mamicode.com/25" />
</bean>
<!--FreeMarker 模板-->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="http://www.mamicode.com/classpath:template" />
<!--<property name="templateLoaderPath" value="http://www.mamicode.com/template" />-->
<property name="freemarkerSettings">
<props>
<prop key="locale">zh_CN</prop>
<prop key="default_encoding">UTF-8</prop>
</props>
</property>
</bean>
发送邮件的工具类:(采用注解形式注入管理邮件发送的类)
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();
}
}
SpringMVC 邮件发送