Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[添加] 邮件发送组件,需修改配置hostname 并开启disabled; see EmailComponentImpl, see [C… #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cachecloud-open-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>commons-email</groupId>
<artifactId>commons-email</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;

/**
* 邮件服务
*
Expand All @@ -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 = "[email protected]";
private String hostName = "cas1.dafycredit.com";

@Override
public boolean sendMailToAdmin(String title, String content) {
Expand All @@ -33,10 +42,47 @@ public boolean sendMail(String title, String content, List<String> emailList) {

@Override
public boolean sendMail(String title, String content, List<String> emailList, List<String> 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 <InternetAddress> clist = new ArrayList <InternetAddress>();
for(String e : emailList){
clist.add(new InternetAddress(e)); // 接收人
}
email.setTo(clist);

// List <InternetAddress> cclist = new ArrayList <InternetAddress>();
// 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) {
Expand Down