From c2caa2b7bbdfb9ddc4a652ade9ae96e96ea92929 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 21 三月 2024 15:24:38 +0800
Subject: [PATCH] 添加邮件发送工具

---
 src/main/resources/application-dev.yml       |   12 ++++
 src/main/resources/application-prod.yml      |   12 ++++
 说明.txt                                       |    7 -
 src/main/resources/application-test.yml      |   12 ++++
 pom.xml                                      |    6 ++
 src/main/java/com/yssh/utils/EmailUtils.java |   98 ++++++++++++++++++++++++++++++++
 6 files changed, 142 insertions(+), 5 deletions(-)

diff --git a/pom.xml b/pom.xml
index 56ab726..2e72745 100644
--- a/pom.xml
+++ b/pom.xml
@@ -294,6 +294,12 @@
             <artifactId>easyexcel</artifactId>
             <version>2.2.10</version>
         </dependency>
+        <!--email-->
+        <dependency>
+            <groupId>com.sun.mail</groupId>
+            <artifactId>javax.mail</artifactId>
+            <version>1.6.2</version>
+        </dependency>
     </dependencies>
 
     <profiles>
diff --git a/src/main/java/com/yssh/utils/EmailUtils.java b/src/main/java/com/yssh/utils/EmailUtils.java
new file mode 100644
index 0000000..5cf9018
--- /dev/null
+++ b/src/main/java/com/yssh/utils/EmailUtils.java
@@ -0,0 +1,98 @@
+package com.yssh.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+
+import javax.mail.*;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import java.util.Properties;
+
+/**
+ * 閭欢宸ュ叿绫�
+ *
+ * @author www
+ * @date 2024-03-21
+ */
+public class EmailUtils {
+    @Value("${email.userName}")
+    private String userName;
+
+    @Value("${email.password}")
+    private String password;
+
+    @Value("${email.smtpHost}")
+    private String smtpHost;
+
+    @Value("${email.smtpPort}")
+    private String smtpPort;
+
+    @Value("${email.smtpAuth}")
+    private String smtpAuth;
+
+    @Value("${email.smtpTls}")
+    private String smtpTls;
+
+    @Value("${email.from}")
+    private String from;
+
+    @Value("${email.to}")
+    private String to;
+
+    @Value("${email.cc}")
+    private String cc;
+
+    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    public Session createSession() {
+        // 鍒涘缓涓�涓厤缃枃浠讹紝骞朵繚瀛�
+        Properties props = new Properties();
+
+        // SMTP鏈嶅姟鍣ㄨ繛鎺ヤ俊鎭細126鈥攕mtp.126.com锛�163鈥攕mtp.163.com锛宷q-qqsmtp.qq.com"
+        props.put("mail.smtp.host", smtpHost); // SMTP涓绘満鍚�
+        props.put("mail.smtp.port", smtpPort); // 涓绘満绔彛鍙凤細126鈥�25锛�163鈥�645
+        props.put("mail.smtp.auth", smtpAuth); // 鏄惁闇�瑕佺敤鎴疯璇�
+        props.put("mail.smtp.starttls.enale", smtpTls); // 鍚敤TlS鍔犲瘑
+
+        Session session = Session.getInstance(props, new Authenticator() {
+            @Override
+            protected PasswordAuthentication getPasswordAuthentication() {
+                return new PasswordAuthentication(userName, password);
+            }
+        });
+
+        // 鎺у埗鍙版墦鍗拌皟璇曚俊鎭�
+        session.setDebug(true);
+
+        return session;
+    }
+
+    public void send(String title, String text) {
+        try {
+            // 鍒涘缓Session浼氳瘽
+            Session session = createSession();
+
+            // 鍒涘缓閭欢瀵硅薄
+            MimeMessage message = new MimeMessage(session);
+            message.setSubject(title);
+            message.setText(text);
+            message.setFrom(new InternetAddress(from));
+            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
+            //message.setRecipients(Message.RecipientType.CC, new InternetAddress[] {new  InternetAddress("鎶勯�佷汉閭")});
+            if (!StringUtils.isEmpty(cc)) {
+                String[] strs = cc.split(",");
+                InternetAddress[] ias = new InternetAddress[strs.length];
+                for (int i = 0, c = strs.length; i < c; i++) {
+                    ias[i] = new InternetAddress(strs[i]);
+                }
+                message.setRecipients(Message.RecipientType.CC, ias);
+            }
+
+            // 鍙戦��
+            Transport.send(message);
+        } catch (Exception ex) {
+            logger.error(ex.getMessage(), ex);
+        }
+    }
+}
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index 77a7203..1961427 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -1,3 +1,15 @@
+# 鐢靛瓙閭欢璁剧疆
+email:
+    userName: 252740454
+    password:
+    smtpHost: qqsmtp.qq.com
+    smtpPort: 25
+    smtpAuth: true
+    smtpTls: true
+    from: 252740454@qq.com
+    to: wuweiwei@terra-it.cn
+    cc: zhengliusuo@smartearth.cn
+
 # 鏁版嵁婧愰厤缃�
 spring:
     datasource:
diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml
index 08353ae..4346f34 100644
--- a/src/main/resources/application-prod.yml
+++ b/src/main/resources/application-prod.yml
@@ -1,3 +1,15 @@
+# 鐢靛瓙閭欢璁剧疆
+email:
+    userName: 252740454
+    password:
+    smtpHost: qqsmtp.qq.com
+    smtpPort: 25
+    smtpAuth: true
+    smtpTls: true
+    from: 252740454@qq.com
+    to: wuweiwei@terra-it.cn
+    cc: zhengliusuo@smartearth.cn
+
 # 鏁版嵁婧愰厤缃�
 spring:
     datasource:
diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml
index 1726939..6add89e 100644
--- a/src/main/resources/application-test.yml
+++ b/src/main/resources/application-test.yml
@@ -1,3 +1,15 @@
+# 鐢靛瓙閭欢璁剧疆
+email:
+    userName: 252740454
+    password:
+    smtpHost: qqsmtp.qq.com
+    smtpPort: 25
+    smtpAuth: true
+    smtpTls: true
+    from: 252740454@qq.com
+    to: wuweiwei@terra-it.cn
+    cc: zhengliusuo@smartearth.cn
+
 # 鏁版嵁婧愰厤缃�
 spring:
     datasource:
diff --git "a/\350\257\264\346\230\216.txt" "b/\350\257\264\346\230\216.txt"
index 08d7c36..72de3ec 100644
--- "a/\350\257\264\346\230\216.txt"
+++ "b/\350\257\264\346\230\216.txt"
@@ -19,17 +19,14 @@
 http://192.168.20.228:8082/yssh/suYuan/selectSuYuan46ById?id=42_474_0&date=2023-08-08+06:00:00
 
 -------------------------------------------------------------------------------
-"id": 508506,"name": "AI-10","lon": 115.9187,"lat": 39.7352,"value": 2.18,"time": "2023080900"
-
------------------------------------------------------
 1.MySQL鏃ュ織锛�
 show binary logs;
 reset master;
 
 2.澶栫綉浜戠櫥褰曟柟寮忥細
->1.鐧诲綍澶栫綉浜戯紝鐢ㄦ埛鍚嶆槸maxy628 123.114.233.55锛岃緭鍏ラ獙璇佺爜锛宲assd123!@#..
+>1.鐧诲綍澶栫綉浜戯紝鐢ㄦ埛鍚嶆槸maxy628锛�123.114.233.55锛岃緭鍏ラ獙璇佺爜锛寉ssh123456.
 >2.杩涘叆浜戞闈� 锛屾祻瑙堝櫒璁块棶10.101.3.183 杩涘叆鍫″瀿鏈虹郴缁�
-     鐢ㄦ埛璐﹀彿锛歮axy628.yssh  瀵嗙爜锛歽ssh1234@#..锛寉ssh1234@#628.
+     鐢ㄦ埛璐﹀彿锛歮axy628.yssh  瀵嗙爜锛歽ssh1234@#628.
 >3. 鍏ㄥ眬鎼滅储  98銆�101銆�102銆�103锛寉ssh123!@#..
 
 3.瀹㈡埛绔湴鍧�锛�

--
Gitblit v1.9.3