Ver Fonte

20190515

suochencheng há 6 anos atrás
pai
commit
c7b1294b37

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

@@ -0,0 +1,93 @@
+package com.izouma.awesomeadmin.dto;
+
+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;
+    }
+
+}
+

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

@@ -0,0 +1,77 @@
+package com.izouma.awesomeadmin.util;
+
+import com.izouma.awesomeadmin.dto.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 = "一米世界留言板";
+    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();
+        }
+
+    }
+
+
+}

+ 58 - 0
src/main/java/com/izouma/awesomeadmin/web/MailSenderController.java

@@ -0,0 +1,58 @@
+package com.izouma.awesomeadmin.web;
+
+import com.izouma.awesomeadmin.dto.MailInfo;
+import com.izouma.awesomeadmin.dto.Result;
+import com.izouma.awesomeadmin.util.MailUtil;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <p>页面控制。</p>
+ *
+ * @author 姓名 <br />
+ * 更新履历 <br />
+ * 日期 : 姓名: 更新内容<br />
+ * @version 1.0
+ */
+@Controller
+@RequestMapping("/mail")
+public class MailSenderController {
+
+    @RequestMapping(value = "/send", method = RequestMethod.GET)
+    @ResponseBody
+    public Result send(@RequestParam(required = true, value = "mobile") String mobile,
+                       @RequestParam(required = true, value = "address") String address,
+                       @RequestParam(required = true, value = "message") String message,
+                       @RequestParam(required = true, value = "mail") String mail) {
+
+        try {
+            MailInfo mailInfo = new MailInfo();
+            List<String> toList = new ArrayList<String>();
+            toList.add(mail);
+
+
+            mailInfo.setToAddress(toList);//收件人
+
+            mailInfo.setSubject("一米世界留言版");
+            String content = "<h1>手机:" + mobile + " </h1>"
+                    + "<h1>地址:" + address + "</h1>"
+                    + "<h1>留言:" + message + "</h1>";
+            mailInfo.setContent(content);
+
+            MailUtil.sendEmail(mailInfo);
+
+            return new Result(true, "发送成功");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return new Result(false, "发送失败,请稍后重试");
+    }
+
+}