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) {