Pārlūkot izejas kodu

调整发邮件功能

AE86 2 gadi atpakaļ
vecāks
revīzija
a805bb1f81

+ 6 - 0
dbsyncer-plugin/pom.xml

@@ -25,5 +25,11 @@
             <version>${project.parent.version}</version>
         </dependency>
 
+        <!-- Mail -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-mail</artifactId>
+        </dependency>
+
     </dependencies>
 </project>

+ 0 - 102
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/notify/EmailNotifyService.java

@@ -1,102 +0,0 @@
-package org.dbsyncer.plugin.notify;
-
-import org.dbsyncer.common.model.NotifyMessage;
-import org.dbsyncer.common.spi.NotifyService;
-import org.dbsyncer.common.util.StringUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.mail.SimpleMailMessage;
-import org.springframework.mail.javamail.JavaMailSender;
-import org.springframework.mail.javamail.JavaMailSenderImpl;
-import org.springframework.stereotype.Component;
-import org.springframework.util.Assert;
-
-import javax.annotation.PostConstruct;
-import java.util.Date;
-import java.util.Properties;
-
-/**
- * 邮件通知服务实现
- *
- * @author AE86
- * @version 1.0.0
- * @date 2022/11/13 22:20
- */
-@Component
-@ConditionalOnProperty(value = "dbsyncer.plugin.notify.email.enabled", havingValue = "true")
-@ConfigurationProperties(prefix = "dbsyncer.plugin.notify.email")
-public class EmailNotifyService implements NotifyService {
-
-    private final Logger logger = LoggerFactory.getLogger(getClass());
-
-    /**
-     * 邮箱账户
-     */
-    private String username;
-
-    /**
-     * 授权码
-     */
-    private String password;
-
-    /**
-     * 邮件发送
-     */
-    private JavaMailSender mailSender;
-
-    @PostConstruct
-    private void init() {
-        logger.info("username:{}", username);
-
-        JavaMailSenderImpl sender = new JavaMailSenderImpl();
-        //服务器
-        sender.setHost("smtp.qq.com");
-        //协议
-        sender.setProtocol("smtps");
-        //端口号
-        sender.setPort(465);
-        //邮箱账号
-        sender.setUsername(username);
-        //邮箱授权码
-        sender.setPassword(password);
-        //编码
-        sender.setDefaultEncoding("Utf-8");
-        Properties p = new Properties();
-        p.setProperty("mail.smtp.ssl.enable", "true");
-        sender.setJavaMailProperties(p);
-
-        this.mailSender = sender;
-    }
-
-    @Override
-    public void sendMessage(NotifyMessage notifyMessage) {
-        checkMail(notifyMessage);
-
-        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
-        // 发送人
-        simpleMailMessage.setFrom(username);
-        // 标题
-        simpleMailMessage.setSubject(notifyMessage.getTitle());
-        // 内容
-        simpleMailMessage.setText(notifyMessage.getContent());
-        // 接收人
-        simpleMailMessage.setTo(StringUtil.split(notifyMessage.getReceiver(), ";"));
-        simpleMailMessage.setSentDate(new Date());
-
-        try {
-            mailSender.send(simpleMailMessage);
-            logger.info("simple mail send success");
-        } catch (Exception e) {
-            logger.error("simple mail send error!", e);
-        }
-    }
-
-    private void checkMail(NotifyMessage notifyMessage) {
-        Assert.notNull(notifyMessage, "通知请求不能为空");
-        Assert.notNull(notifyMessage.getTitle(), "邮件主题不能为空");
-        Assert.notNull(notifyMessage.getContent(), "邮件内容不能为空");
-        Assert.notNull(notifyMessage.getReceiver(), "邮件收件人不能为空");
-    }
-}

+ 114 - 0
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/notify/MailNotifyService.java

@@ -0,0 +1,114 @@
+package org.dbsyncer.plugin.notify;
+
+import org.dbsyncer.common.model.NotifyMessage;
+import org.dbsyncer.common.spi.NotifyService;
+import org.dbsyncer.common.util.StringUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+import org.springframework.util.Assert;
+
+import javax.annotation.PostConstruct;
+import javax.mail.*;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import java.util.Properties;
+
+/**
+ * 邮件通知服务实现
+ *
+ * @author AE86
+ * @version 1.0.0
+ * @date 2022/11/13 22:20
+ */
+@Component
+@ConditionalOnProperty(value = "dbsyncer.plugin.notify.mail.enabled", havingValue = "true")
+@ConfigurationProperties(prefix = "dbsyncer.plugin.notify.mail")
+public class MailNotifyService implements NotifyService {
+
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    /**
+     * 邮箱账户
+     */
+    private String username;
+
+    /**
+     * 授权码
+     */
+    private String password;
+
+    /**
+     * 邮箱会话
+     */
+    private Session session;
+
+    @PostConstruct
+    private void init() {
+        final Properties props = new Properties();
+        props.put("mail.smtp.auth", "true");
+        props.put("mail.smtp.host", "smtp.qq.com");
+        props.put("mail.user", username);
+        props.put("mail.password", password);
+
+        // 构建授权信息,用于进行SMTP进行身份验证
+        session = Session.getInstance(props, new Authenticator() {
+            @Override
+            protected PasswordAuthentication getPasswordAuthentication() {
+                return new PasswordAuthentication(username, password);
+            }
+        });
+    }
+
+    @Override
+    public void sendMessage(NotifyMessage notifyMessage) {
+        try {
+            checkMail(notifyMessage);
+            String to = notifyMessage.getReceiver();
+            String title = notifyMessage.getTitle();
+            String content = notifyMessage.getContent();
+
+            // 创建邮件消息
+            MimeMessage message = new MimeMessage(session);
+            // 设置发件人
+            message.setFrom(new InternetAddress(username));
+
+            // 接收人
+            String[] receiverArray = StringUtil.split(to, ";");
+            int length = receiverArray.length;
+            InternetAddress[] addresses = new InternetAddress[length];
+            for (int i = 0; i < length; i++) {
+                addresses[i] = new InternetAddress(receiverArray[i]);
+            }
+            message.setRecipients(Message.RecipientType.TO, addresses);
+
+            // 设置邮件标题
+            message.setSubject(title);
+
+            // 设置邮件的内容体
+            message.setContent(content, "text/html;charset=UTF-8");
+            // 发送邮件
+            Transport.send(message);
+            logger.info("simple mail send success");
+        } catch (Exception e) {
+            logger.error("simple mail send error!", e);
+        }
+    }
+
+    private void checkMail(NotifyMessage notifyMessage) {
+        Assert.notNull(notifyMessage, "通知请求不能为空");
+        Assert.notNull(notifyMessage.getTitle(), "邮件主题不能为空");
+        Assert.notNull(notifyMessage.getContent(), "邮件内容不能为空");
+        Assert.notNull(notifyMessage.getReceiver(), "邮件收件人不能为空");
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+}

+ 4 - 5
dbsyncer-web/src/main/resources/application.properties

@@ -22,9 +22,9 @@ dbsyncer.parser.flush.buffer.actuator.queue-capacity=50000
 dbsyncer.parser.flush.buffer.actuator.period-millisecond=300
 
 #plugin
-#dbsyncer.plugin.notify.email.enabled=false
-#dbsyncer.plugin.notify.email.username=your email username
-#dbsyncer.plugin.notify.email.password=your email authorization code
+dbsyncer.plugin.notify.mail.enabled=false
+dbsyncer.plugin.notify.mail.username=your mail username
+dbsyncer.plugin.notify.mail.password=your mail authorization code
 
 #storage
 dbsyncer.storage.binlog.recorder.batch-count=1000
@@ -55,5 +55,4 @@ spring.thymeleaf.mode=HTML
 spring.thymeleaf.encoding=UTF-8
 spring.thymeleaf.cache=false
 spring.resources.chain.strategy.content.enabled=true
-spring.resources.chain.strategy.content.paths=/**
-
+spring.resources.chain.strategy.content.paths=/**