package com.allianz.fileservice.service;
import com.allianz.fileservice.vo.EmailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.Set;
@Service
public class EmailService {
private static Logger logger = LoggerFactory.getLogger(EmailService.class);
@Autowired
private JavaMailSender sender;
@Autowired
private ITemplateEngine engine;
@Async
public void sendEmail(String template, EmailVO email){
// 获取参数
IContext context = getContext(email.getParams());
// 获取模板
String content = engine.process(template,context);
// 发送邮件
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
try {
helper.setTo(email.getUserEmailAddress());
helper.setSubject(email.getSubject());
helper.setFrom("su.mengxuan@allianz.com");
helper.setText(content,true);
} catch (MessagingException e) {
logger.info("Mail Delivery Failed" + email.toString());
e.printStackTrace();
}
sender.send(helper.getMimeMessage());
logger.info("Mail Delivery Success" + email.getUserEmailAddress());
}
private IContext getContext(Map<String,Object> params) {
Set<Map.Entry<String, Object>> entries = params.entrySet();
Context context = new Context();
for ( Map.Entry<String,Object> entry:entries) {
context.setVariable(entry.getKey(),entry.getValue());
}
return context;
}
}
邮件服务
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 项目地址 https://github.com/xiangyuecn/DKIM-Smtp-csharp 主要支持 ...