1. mail配置
## mail
mail.smtp.host=smtp.XXXXXX.com
mail.smtp.user=username@XXXXXX.com
mail.smtp.password=111111
mail.smtp.port=465
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.ssl.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.fallback=false
mail.recipient.cc=ccusername@XXXXXX.com
2.MailUtil.java
@Component
public class MailUtil implements InitializingBean {
@Autowired
private AppConfig appConfig;
private static final String SMTP = "smtp";
private static final String CONTENT_TYPE = "text/html;charset=utf-8";
private static final List<String> CC = new ArrayList<String>();
private static final Logger logger = Logger.getLogger(MailUtil.class);
public void sendMail(List<String> toList, List<String> cc, String subject, String body)
throws AddressException, MessagingException {
Properties props = System.getProperties();
props.put("mail.smtp.ssl.enable", appConfig.getMailSsl());
props.put("mail.smtp.host", appConfig.getMailHost());
props.put("mail.smtp.user", appConfig.getMailUser());
props.put("mail.smtp.password", appConfig.getMailPassword());
props.put("mail.smtp.port", appConfig.getMailPort());
props.put("mail.smtp.auth", appConfig.getMailAuth());
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(appConfig.getMailUser()));
logger.info("mail subject : " + subject);
List<String> to = new ArrayList<String>();
for (int i = 0; i < toList.size(); i++) {//去重
String tempMail = toList.get(i);
logger.info("toList mail i = " + i + " : " + tempMail);
if(!to.contains(tempMail)){
to.add(tempMail);
}
}
logger.info("after removing the repeat mail, to list size: " + to.size());
//20160202 in product 暂时注释掉 将收件人替换为开发者自己 正式环境下去掉注释
for (int i = 0; i < to.size(); i++) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.get(i)));
}
if (cc != null) {
for (int i = 0; i < cc.size(); i++) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
}
}
//没什么用 注释掉
/*for (int i = 0; i < CC.size(); i++) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(CC.get(i)));
}*/
//20160202 in develop 暂时加上将收件人替换为开发者自己 正式环境下注释掉
//message.addRecipient(Message.RecipientType.TO, new InternetAddress("testusername@XXXXXX.com"));
//message.addRecipient(Message.RecipientType.TO, new InternetAddress(Constants.ADMIN_EMAIL));
message.setSubject(subject);
message.setContent(body, CONTENT_TYPE);
Transport transport = session.getTransport(SMTP);
transport.connect(appConfig.getMailHost(), appConfig.getMailUser(), appConfig.getMailPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
if (!Strings.isNullOrEmpty(appConfig.getMailCc())) {
String []cc = appConfig.getMailCc().split(";");
for (int i = 0; i < cc.length; i++) {
CC.add(cc[i].trim());
}
}
}
}
3.AppConfig.java(将properties中的属性映射到bean中)
@Component
public class AppConfig {
public static final String CURRENT_WORKING_DIR = System
.getProperty("user.dir");
public static final String FILE_SEPARATOR = System
.getProperty("file.separator");
static final String DEV_MODE = "dev";
static final String PROD_MODE = "prod";
// app ---------------------------------------------------------------------
@Value("${app.runMode}")
private String runMode;
@Value("${app.workDir}")
private String workDirPath;
// http --------------------------------------------------------------------
@Value("${app.server.port}")
private int httpPort;
@Value("${app.server.sessionTimeout}")
private int httpSessionTimeout;
// mail
@Value("${mail.smtp.host}")
private String mailHost;
@Value("${mail.smtp.user}")
private String mailUser;
@Value("${mail.smtp.password}")
private String mailPassword;
@Value("${mail.smtp.port}")
private String mailPort;
@Value("${mail.smtp.auth}")
private String mailAuth;
@Value("${mail.smtp.timeout}")
private String mailTimeout;
@Value("${mail.smtp.ssl.enable}")
private String mailSsl;
@Value("${mail.recipient.cc}")
private String mailCc;
public String getMailHost() {
return mailHost;
}
public void setMailHost(String mailHost) {
this.mailHost = mailHost;
}
public String getMailUser() {
return mailUser;
}
public void setMailUser(String mailUser) {
this.mailUser = mailUser;
}
public String getMailPassword() {
return mailPassword;
}
public void setMailPassword(String mailPassword) {
this.mailPassword = mailPassword;
}
public String getMailPort() {
return mailPort;
}
public void setMailPort(String mailPort) {
this.mailPort = mailPort;
}
public String getMailAuth() {
return mailAuth;
}
public void setMailAuth(String mailAuth) {
this.mailAuth = mailAuth;
}
public String getMailTimeout() {
return mailTimeout;
}
public void setMailTimeout(String mailTimeout) {
this.mailTimeout = mailTimeout;
}
public String getMailSsl() {
return mailSsl;
}
public void setMailSsl(String mailSsl) {
this.mailSsl = mailSsl;
}
public String getMailCc() {
return mailCc;
}
public void setMailCc(String mailCc) {
this.mailCc = mailCc;
}
//判断是在开发模式还是生产模式
public boolean isInDevelopMode() {
if (runMode.compareTo(DEV_MODE) == 0) {
return true;
}
return false;
}
public boolean isInProductionMode() {
if (runMode.compareTo(PROD_MODE) == 0) {
return true;
}
return false;
}
public String getRunMode() {
return runMode;
}
public String getWorkDirPath() {
return workDirPath;
}
public int getHttpPort() {
return httpPort;
}
public int getHttpSessionTimeout() {
return httpSessionTimeout;
}
}