From eac127c9b61771a09b150f5ab577403adda3e935 Mon Sep 17 00:00:00 2001 From: "zhoufan@dafycredit.com" Date: Thu, 24 Mar 2016 16:17:32 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=B7=BB=E5=8A=A0]=20=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E7=BB=84=E4=BB=B6=EF=BC=8C=E9=9C=80=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=85=8D=E7=BD=AEhostname=20=E5=B9=B6=E5=BC=80?= =?UTF-8?q?=E5=90=AFdisabled=EF=BC=9B=20see=20EmailComponentImpl,=20see=20?= =?UTF-8?q?[Commons=20Email](http://commons.apache.org/proper/commons-emai?= =?UTF-8?q?l/)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cachecloud-open-common/pom.xml | 5 ++ .../web/component/EmailComponentImpl.java | 62 ++++++++++++++++--- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/cachecloud-open-common/pom.xml b/cachecloud-open-common/pom.xml index e3e27c28..4194c04d 100644 --- a/cachecloud-open-common/pom.xml +++ b/cachecloud-open-common/pom.xml @@ -16,6 +16,11 @@ commons-lang commons-lang + + + commons-email + commons-email + 1.1 \ No newline at end of file diff --git a/cachecloud-open-common/src/main/java/com/sohu/cache/web/component/EmailComponentImpl.java b/cachecloud-open-common/src/main/java/com/sohu/cache/web/component/EmailComponentImpl.java index 744f561c..380c62eb 100644 --- a/cachecloud-open-common/src/main/java/com/sohu/cache/web/component/EmailComponentImpl.java +++ b/cachecloud-open-common/src/main/java/com/sohu/cache/web/component/EmailComponentImpl.java @@ -1,11 +1,17 @@ package com.sohu.cache.web.component; +import org.apache.commons.mail.EmailException; +import org.apache.commons.mail.SimpleEmail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; + /** * 邮件服务 * @@ -16,10 +22,13 @@ public class EmailComponentImpl implements EmailComponent { private final Logger logger = LoggerFactory.getLogger(EmailComponentImpl.class); - /** - * 管理员 - */ - private String adminEmail; + /** + * your company send email codes + */ + private boolean disabled = true; // 关闭 true | 开启 false + private String adminName = "Admin"; + private String adminEmail = "CacheCloud@dafycredit.com"; + private String hostName = "cas1.dafycredit.com"; @Override public boolean sendMailToAdmin(String title, String content) { @@ -33,10 +42,47 @@ public boolean sendMail(String title, String content, List emailList) { @Override public boolean sendMail(String title, String content, List emailList, List ccList) { - /** - * your company send email codes - */ - return true; + if(disabled){ + return Boolean.FALSE; + } + + if(emailList==null || emailList.size()==0){ + logger.error("send email fail, nobody."); + return Boolean.FALSE; + } + + try { + SimpleEmail email = new SimpleEmail(); + email.setCharset("UTF-8"); + + // email.setAuthentication("username", "password"); // 登陆邮件服务器的用户名和密码 (内网,可省略) + + List clist = new ArrayList (); + for(String e : emailList){ + clist.add(new InternetAddress(e)); // 接收人 + } + email.setTo(clist); + +// List cclist = new ArrayList (); +// for(String e : ccList){ +// cclist.add(new InternetAddress(e)); // 抄送人 +// } +// email.setCc(cclist); + + email.setHostName(hostName); // smtp host + email.setFrom(adminEmail, adminName); // 发送人 + email.setSubject(title); // 标题 + email.setMsg(content); // 邮件内容 + email.send(); + + logger.info("Send email successful!"); + } catch (EmailException | AddressException e) { + logger.error("send email error, subject: {}, emailList: {}, ccList: {}", title, emailList, ccList); + logger.error(e.getMessage(), e); + return Boolean.FALSE; + } + + return Boolean.TRUE; } public void setAdminEmail(String adminEmail) {