Переглянути джерело

fix:“优化报警邮件推送功能"

soobin 6 місяців тому
батько
коміт
75cb189986

+ 72 - 0
src/main/java/com/szwl/model/utils/SampleMail.java

@@ -96,6 +96,78 @@ public class SampleMail {
 
     }
 
+    public void sendAuthCode(String toEmail, String content, String title) {
+        // 配置发送邮件的环境属性
+        final Properties props = new Properties();
+
+        // 表示SMTP发送邮件,需要进行身份验证
+        props.put("mail.smtp.auth", "true");
+        props.put("mail.smtp.host", "smtpdm.aliyun.com"); //华东1:smtpdm.aliyun.com,悉尼:smtpdm-ap-southeast-2.aliyun.com
+        //设置端口:
+        props.put("mail.smtp.port", "80");//或"25"
+        props.put("mail.smtp.from", "aftersale@magiccandy.cn");    //mailfrom 参数
+        props.put("mail.user", "aftersale@magiccandy.cn");// 发件人的账号(在控制台创建的发信地址)
+        props.put("mail.password", "DirectMail123");// 发信地址的smtp密码(在控制台选择发信地址进行设置)
+        System.setProperty("mail.mime.splitlongparameters", "false");//用于解决附件名过长导致的显示异常
+
+        // 构建授权信息,用于进行SMTP进行身份验证
+        Authenticator authenticator = new Authenticator() {
+            @Override
+            protected PasswordAuthentication getPasswordAuthentication() {
+                // 用户名、密码
+                String userName = props.getProperty("mail.user");
+                String password = props.getProperty("mail.password");
+                return new PasswordAuthentication(userName, password);
+            }
+        };
+        //使用环境属性和授权信息,创建邮件会话
+        Session mailSession = Session.getInstance(props, authenticator);
+        //mailSession.setDebug(true);//开启debug模式
+
+
+        final String messageIDValue = genMessageID(props.getProperty("mail.user"));
+        //创建邮件消息
+        MimeMessage message = new MimeMessage(mailSession) {
+            @Override
+            protected void updateMessageID() throws MessagingException {
+                //设置自定义Message-ID值
+                setHeader("Message-ID", messageIDValue);//创建Message-ID
+            }
+        };
+
+        try {
+            // 设置发件人邮件地址和名称。填写控制台配置的发信地址。和上面的mail.user保持一致。名称用户可以自定义填写。
+            InternetAddress from = new InternetAddress("aftersale@magiccandy.cn", "AfterSaleInfo");//from 参数,可实现代发,注意:代发容易被收信方拒信或进入垃圾箱。
+            message.setFrom(from);
+
+            //可选。设置回信地址
+            Address[] a = new Address[1];
+            a[0] = new InternetAddress(toEmail);
+            message.setReplyTo(a);
+
+            // 设置收件人邮件地址
+            InternetAddress to = new InternetAddress(toEmail);
+            message.setRecipient(MimeMessage.RecipientType.TO, to);
+
+
+            message.setSentDate(new Date()); //设置时间
+
+            //设置邮件标题
+            message.setSubject(title);
+            message.setContent(content, "text/html;charset=UTF-8");//html超文本;// "text/plain;charset=UTF-8" //纯文本。
+            // 发送邮件
+            Transport.send(message);
+
+        } catch (MessagingException e) {
+            String err = e.getMessage();
+            // 在这里处理message内容, 格式是固定的
+            System.out.println(err);
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+    }
+
 
     public void sendAuthCodePortalmcc(String toEmail, String content) {
         // 配置发送邮件的环境属性

+ 7 - 4
src/main/java/com/szwl/util/WechatSendUtil.java

@@ -1,6 +1,7 @@
 package com.szwl.util;
 
 import com.szwl.model.utils.MailUtil;
+import com.szwl.model.utils.SampleMail;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 
@@ -95,8 +96,9 @@ public class WechatSendUtil {
                 "Best Regards.<br>" +
                 "Cotton Candy Service Team";
         content.append(str1).append(name).append(str2).append(alarmContent).append(str3).append(timeByZoneID).append(str4).append(str5).append(str6);
-        MailUtil mailUtil = new MailUtil();
-        mailUtil.send(email, subject, content.toString());
+//        MailUtil mailUtil = new MailUtil();
+//        mailUtil.send(email, subject, content.toString());
+        new SampleMail().sendAuthCode(email, content.toString(), subject);
         System.out.println("邮件发送成功");
     }
 
@@ -147,8 +149,9 @@ public class WechatSendUtil {
                 "Best Regards.<br>" +
                 "<b>Cotton Candy Service Team</b>";
         content.append(str1).append(name).append(str3).append(timeByZoneId).append(str4).append(str5).append(str6);
-        MailUtil mailUtil = new MailUtil();
-        mailUtil.send(email, subject, content.toString());
+//        MailUtil mailUtil = new MailUtil();
+//        mailUtil.send(email, subject, content.toString());
+        new SampleMail().sendAuthCode(email, content.toString(), subject);
         System.out.println("邮件发送成功");
     }