Explorar el Código

20190513自动发送邮件

suochencheng hace 6 años
padre
commit
9746573774

+ 2 - 0
log.txt

@@ -201,3 +201,5 @@ Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
 [2018-10-15 15:12:55] [WARN ] - Bad pool size config, start 3 < min 5. Using 5 as start.
 [2018-10-15 15:18:04] [WARN ] - Bad pool size config, start 3 < min 5. Using 5 as start.
 [2018-10-26 10:48:33] [WARN ] - Bad pool size config, start 3 < min 5. Using 5 as start.
+[2019-05-13 17:37:51] [WARN ] - Bad pool size config, start 3 < min 5. Using 5 as start.
+[2019-05-13 17:38:53] [WARN ] - Bad pool size config, start 3 < min 5. Using 5 as start.

+ 7 - 0
pom.xml

@@ -663,6 +663,13 @@
             <version>1.1.1</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-email</artifactId>
+            <version>1.4</version>
+        </dependency>
+
+
         <!-- poi office -->
         <dependency>
             <groupId>org.apache.poi</groupId>

+ 93 - 0
src/main/java/com/izouma/awesomeadmin/model/MailInfo.java

@@ -0,0 +1,93 @@
+package com.izouma.awesomeadmin.model;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import org.apache.commons.mail.EmailAttachment;
+
+import java.util.List;
+
+/**
+ * depart_info 实体类
+ * Thu Apr 26 10:56:37 CST 2018  Suo Chen Cheng
+ */
+@JsonAutoDetect
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class MailInfo {
+
+    // 收件人
+    private List<String> toAddress = null;
+    // 抄送人地址
+    private List<String> ccAddress = null;
+    // 密送人
+    private List<String> bccAddress = null;
+    // 附件信息
+    private List<EmailAttachment> attachments = null;
+    // 邮件主题
+    private String subject;
+    // 邮件的文本内容
+    private String content;
+
+
+    public List<String> getToAddress() {
+        return toAddress;
+    }
+
+    public void addToAddress(String toAddress) {
+        this.toAddress.add(toAddress);
+    }
+
+    public void addToAddress(List<String> toAddress) {
+        this.toAddress.addAll(toAddress);
+    }
+
+    public void addCcAddress(List<String> ccAddress) {
+        if (null != ccAddress && ccAddress.size() > 0)
+            this.ccAddress.addAll(ccAddress);
+    }
+
+    public List<EmailAttachment> getAttachments() {
+        return attachments;
+    }
+
+    public void setAttachments(List<EmailAttachment> attachments) {
+        this.attachments = attachments;
+    }
+
+    public List<String> getBccAddress() {
+        return bccAddress;
+    }
+
+    public void setBccAddress(List<String> bccAddress) {
+        this.bccAddress = bccAddress;
+    }
+
+    public String getSubject() {
+        return subject;
+    }
+
+    public void setSubject(String subject) {
+        this.subject = subject;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public void setToAddress(List<String> toAddress) {
+        this.toAddress = toAddress;
+    }
+
+    public List<String> getCcAddress() {
+        return ccAddress;
+    }
+
+    public void setCcAddress(List<String> ccAddress) {
+        this.ccAddress = ccAddress;
+    }
+
+}
+

+ 55 - 18
src/main/java/com/izouma/awesomeadmin/service/impl/FeedbackServiceImpl.java

@@ -1,6 +1,10 @@
 package com.izouma.awesomeadmin.service.impl;
 
 import java.util.*;
+
+import com.izouma.awesomeadmin.model.MailInfo;
+import com.izouma.awesomeadmin.util.MailUtil;
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -11,10 +15,10 @@ import com.izouma.awesomeadmin.service.FeedbackService;
 import com.izouma.awesomeadmin.dao.FeedbackMapper;
 
 /**
-*  service接口实现类
-*/
+ * service接口实现类
+ */
 @Service
-public class FeedbackServiceImpl implements FeedbackService{
+public class FeedbackServiceImpl implements FeedbackService {
 
     private static Logger logger = Logger.getLogger(FeedbackServiceImpl.class);
 
@@ -27,26 +31,27 @@ public class FeedbackServiceImpl implements FeedbackService{
         logger.info("getFeedbackList");
         try {
 
-        return feedbackMapper.queryAllFeedback(record);
+            return feedbackMapper.queryAllFeedback(record);
         } catch (Exception e) {
-        logger.error("getFeedbackList", e);
+            logger.error("getFeedbackList", e);
         }
 
         return null;
     }
+
     @Override
     public List<Feedback> getFeedbackByPage(Page page, Feedback record) {
 
         logger.info("getFeedbackByPage");
         try {
 
-        Map<String, Object> parameter = new HashMap<String, Object>();
-        parameter.put("record", record);
-        parameter.put(AppConstant.PAGE, page);
+            Map<String, Object> parameter = new HashMap<String, Object>();
+            parameter.put("record", record);
+            parameter.put(AppConstant.PAGE, page);
 
-        return feedbackMapper.queryFeedbackByPage(parameter);
+            return feedbackMapper.queryFeedbackByPage(parameter);
         } catch (Exception e) {
-        logger.error("getFeedbackByPage", e);
+            logger.error("getFeedbackByPage", e);
         }
 
         return null;
@@ -60,7 +65,7 @@ public class FeedbackServiceImpl implements FeedbackService{
 
             return feedbackMapper.selectByPrimaryKey(Integer.valueOf(id));
         } catch (Exception e) {
-        logger.error("getFeedbackById", e);
+            logger.error("getFeedbackById", e);
         }
 
         return null;
@@ -74,7 +79,7 @@ public class FeedbackServiceImpl implements FeedbackService{
 
             return feedbackMapper.queryFeedback(record);
         } catch (Exception e) {
-        logger.error("getFeedback", e);
+            logger.error("getFeedback", e);
         }
 
         return null;
@@ -89,7 +94,39 @@ public class FeedbackServiceImpl implements FeedbackService{
             int updates = feedbackMapper.insertSelective(record);
 
             if (updates > 0) {
-                 return true;
+
+                if (StringUtils.isNotEmpty(record.getEmail())) {
+                    try {
+
+                        MailInfo mailInfo = new MailInfo();
+                        List<String> toList = new ArrayList<String>();
+                        toList.add(record.getEmail());
+
+//                        List<String> ccList = new ArrayList<String>();
+//                        ccList.add("124134423@qq.com");
+
+                        List<String> bccList = new ArrayList<String>();
+                        bccList.add("124134423@qq.com");
+
+                        mailInfo.setToAddress(toList);//收件人
+                        // mailInfo.setCcAddress(ccList);//抄送人
+                        mailInfo.setBccAddress(bccList);//密送人
+
+                        mailInfo.setSubject("走马溯源测试地址");
+                        String content = "你好," + record.getUserName() + "! 下面是溯源系统后台管理测试:"
+                                + "<h1>地址:http://118.178.226.110:8086/admin/login</h1>"
+                                + "<h1>用户密码:root 123456</h1>";
+                        mailInfo.setContent(content);
+
+                        MailUtil.sendEmail(mailInfo);
+
+                    } catch (Exception e) {
+                        logger.error("sendEmail_createFeedback", e);
+                    }
+
+                }
+
+                return true;
             }
         } catch (Exception e) {
             logger.error("createFeedback", e);
@@ -104,13 +141,13 @@ public class FeedbackServiceImpl implements FeedbackService{
         logger.info("deleteFeedback");
         try {
 
-             int updates = feedbackMapper.delete(record);
+            int updates = feedbackMapper.delete(record);
 
             if (updates > 0) {
-                 return true;
+                return true;
             }
         } catch (Exception e) {
-             logger.error("deleteFeedback", e);
+            logger.error("deleteFeedback", e);
         }
 
         return false;
@@ -125,10 +162,10 @@ public class FeedbackServiceImpl implements FeedbackService{
             int updates = feedbackMapper.updateByPrimaryKeySelective(record);
 
             if (updates > 0) {
-                 return true;
+                return true;
             }
         } catch (Exception e) {
-             logger.error("updateFeedback", e);
+            logger.error("updateFeedback", e);
         }
 
         return false;

+ 77 - 0
src/main/java/com/izouma/awesomeadmin/util/MailUtil.java

@@ -0,0 +1,77 @@
+package com.izouma.awesomeadmin.util;
+
+import com.izouma.awesomeadmin.model.MailInfo;
+import org.apache.commons.mail.EmailAttachment;
+import org.apache.commons.mail.EmailException;
+import org.apache.commons.mail.HtmlEmail;
+
+import java.util.List;
+
+/**
+ * 发送邮件Util
+ */
+public class MailUtil {
+
+    //邮箱
+    private static String mailServerHost = "smtp.weiqiuwang.com";
+    private static String mailSenderAddress = "scc@weiqiuwang.com";
+    private static String mailSenderNick = "izouma";
+    private static String mailSenderUsername = "scc@weiqiuwang.com";
+    private static String mailSenderPassword = "2wsx@WSX";
+
+    /**
+     * 发送 邮件方法 (Html格式,支持附件)
+     *
+     * @return void
+     */
+    public static void sendEmail(MailInfo mailInfo) {
+
+        try {
+            HtmlEmail email = new HtmlEmail();
+            // 配置信息
+            email.setHostName(mailServerHost);
+            email.setFrom(mailSenderAddress, mailSenderNick);
+            email.setAuthentication(mailSenderUsername, mailSenderPassword);
+            email.setCharset("UTF-8");
+            email.setSubject(mailInfo.getSubject());
+            email.setHtmlMsg(mailInfo.getContent());
+
+            // 添加附件
+            List<EmailAttachment> attachments = mailInfo.getAttachments();
+            if (null != attachments && attachments.size() > 0) {
+                for (int i = 0; i < attachments.size(); i++) {
+                    email.attach(attachments.get(i));
+                }
+            }
+
+            // 收件人
+            List<String> toAddress = mailInfo.getToAddress();
+            if (null != toAddress && toAddress.size() > 0) {
+                for (int i = 0; i < toAddress.size(); i++) {
+                    email.addTo(toAddress.get(i));
+                }
+            }
+            // 抄送人
+            List<String> ccAddress = mailInfo.getCcAddress();
+            if (null != ccAddress && ccAddress.size() > 0) {
+                for (int i = 0; i < ccAddress.size(); i++) {
+                    email.addCc(ccAddress.get(i));
+                }
+            }
+            //邮件模板 密送人
+            List<String> bccAddress = mailInfo.getBccAddress();
+            if (null != bccAddress && bccAddress.size() > 0) {
+                for (int i = 0; i < bccAddress.size(); i++) {
+                    email.addBcc(bccAddress.get(i));
+                }
+            }
+            email.send();
+            System.out.println("邮件发送成功!");
+        } catch (EmailException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
+}

+ 43 - 0
src/test/java/UserServiceTest.java

@@ -1,6 +1,9 @@
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.izouma.awesomeadmin.model.MailInfo;
 import com.izouma.awesomeadmin.service.UserInfoService;
+import com.izouma.awesomeadmin.util.MailUtil;
+import org.apache.commons.mail.EmailAttachment;
 import org.json.JSONObject;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -13,6 +16,8 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
 import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
 
 @RunWith(value = SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"}, loader = GenericXmlContextLoader.class)
@@ -42,4 +47,42 @@ public class UserServiceTest {
         }
 
     }
+
+    @Test
+    public void testSendMail() {
+        try {
+
+            MailInfo mailInfo = new MailInfo();
+            List<String> toList = new ArrayList<String>();
+            toList.add("124134423@qq.com");
+
+            List<String> ccList = new ArrayList<String>();
+            ccList.add("124134423@qq.com");
+
+            List<String> bccList = new ArrayList<String>();
+            bccList.add("124134423@qq.com");
+
+            //添加附件
+//            EmailAttachment att = new EmailAttachment();
+//            att.setPath("g:\\测试.txt");
+//            att.setName("测试.txt");
+//            List<EmailAttachment> atts = new ArrayList<EmailAttachment>();
+//            atts.add(att);
+//            mailInfo.setAttachments(atts);
+
+            mailInfo.setToAddress(toList);//收件人
+            mailInfo.setCcAddress(ccList);//抄送人
+            mailInfo.setBccAddress(bccList);//密送人
+
+            mailInfo.setSubject("测试主题");
+            mailInfo.setContent("内容:<h1>test,测试</h1>");
+
+            MailUtil.sendEmail(mailInfo);
+
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
 }