|
1 | 1 | package com.danbai.ys.utils; |
2 | 2 |
|
3 | | -import org.springframework.mail.SimpleMailMessage; |
4 | | -import org.springframework.mail.javamail.JavaMailSenderImpl; |
5 | | -import org.springframework.scheduling.annotation.Async; |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.beans.factory.annotation.Value; |
| 5 | +import org.springframework.mail.javamail.JavaMailSender; |
| 6 | +import org.springframework.mail.javamail.MimeMessageHelper; |
6 | 7 | import org.springframework.stereotype.Component; |
7 | | -import org.springframework.stereotype.Service; |
8 | | - |
9 | | -import javax.mail.internet.InternetAddress; |
10 | | -import java.io.UnsupportedEncodingException; |
11 | | -import java.util.Properties; |
| 8 | +import org.thymeleaf.TemplateEngine; |
| 9 | +import org.thymeleaf.context.Context; |
| 10 | +import javax.mail.internet.MimeMessage; |
| 11 | +import java.util.Map; |
12 | 12 |
|
13 | 13 | /** |
14 | 14 | * @author danbai |
15 | 15 | * @date 2019-10-29 16:26 |
16 | 16 | */ |
17 | 17 | @Component |
18 | | -@Service |
19 | 18 | public class EmailUtil { |
20 | | - private static final String HOST = "smtpdm.aliyun.com"; |
21 | | - private static final String PROTOCOL = "smtp"; |
22 | | - private static final int PORT = 465; |
23 | | - private static final String FROM = "danbaiyingshi@p00q.cn"; |
24 | | - private static final String PWD = "123"; |
25 | | - |
26 | | - private static JavaMailSenderImpl javaMailSender; |
27 | | - |
28 | | - static { |
29 | | - javaMailSender = new JavaMailSenderImpl(); |
30 | | - javaMailSender.setProtocol(PROTOCOL); |
31 | | - javaMailSender.setHost(HOST); |
32 | | - //链接服务器 |
33 | | - javaMailSender.setPort(PORT); |
34 | | - //默认使用25端口发送 |
35 | | - javaMailSender.setUsername(FROM); |
36 | | - //账号 |
37 | | - javaMailSender.setPassword(PWD); |
38 | | - //密码 |
39 | | - javaMailSender.setDefaultEncoding("UTF-8"); |
40 | | - Properties properties = new Properties(); |
41 | | - properties.setProperty("mail.smtp.auth", "true"); |
42 | | - //开启认证 |
43 | | - properties.setProperty("mail.smtp.socketFactory.port", "465"); |
44 | | - //设置ssl端口 |
45 | | - properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
46 | | - javaMailSender.setJavaMailProperties(properties); |
47 | | - } |
| 19 | + @Value("${dbys.mail-sender}") |
| 20 | + private String sender; |
| 21 | + @Autowired |
| 22 | + private JavaMailSender javaMailSender; |
| 23 | + @Autowired |
| 24 | + private TemplateEngine templateEngine; |
48 | 25 |
|
49 | | - @Async |
50 | | - public void sendEmail(final String title, final String content, final String toMail) { |
51 | | - try { |
52 | | - SimpleMailMessage mailMessage = new SimpleMailMessage(); |
53 | | - String nick=javax.mail.internet.MimeUtility.encodeText("淡白影视"); |
54 | | - mailMessage.setFrom(String.valueOf(new InternetAddress(nick+" <"+FROM+">"))); |
55 | | - mailMessage.setSubject(title); |
56 | | - mailMessage.setText(content); |
57 | | - String[] toAddress = toMail.split(","); |
58 | | - mailMessage.setTo(toAddress); |
59 | | - //发送邮件 |
60 | | - javaMailSender.send(mailMessage); |
61 | | - }catch(Exception e){ |
62 | | - System.out.println(e); |
| 26 | + /** |
| 27 | + * 发信模版 |
| 28 | + * @param receiver |
| 29 | + * @param subject |
| 30 | + * @param emailTemplate |
| 31 | + * @param dataMap |
| 32 | + * @throws Exception |
| 33 | + */ |
| 34 | + public void sendTemplateMail(String receiver, String subject, String emailTemplate, Map<String, Object> dataMap) throws Exception { |
| 35 | + Context context = new Context(); |
| 36 | + for (Map.Entry<String, Object> entry : dataMap.entrySet()) { |
| 37 | + context.setVariable(entry.getKey(), entry.getValue()); |
63 | 38 | } |
| 39 | + String templateContent = templateEngine.process(emailTemplate, context); |
| 40 | + MimeMessage message = javaMailSender.createMimeMessage(); |
| 41 | + MimeMessageHelper helper = new MimeMessageHelper(message, true); |
| 42 | + helper.setFrom(sender); |
| 43 | + helper.setTo(receiver); |
| 44 | + helper.setSubject(subject); |
| 45 | + helper.setText(templateContent, true); |
| 46 | + javaMailSender.send(message); |
64 | 47 | } |
65 | 48 | } |
0 commit comments