1.邮件异步发送
/**
* 异步方式发送邮件
* @param id
* @param other
* @param title
* @param context
*/
public void sendMailByAsynchronous(final String id, final String other,
final String title, final String context) {
new Thread(new Runnable() {
public void run() {
try {
mailSendService.sendMail(id,
null, title, context);
} catch (Exception ex) {
}
}
}).start();
}
2.邮件信息处理
public String sendMail(String ids, String others, String title, String context) {
String msg = "0";
String host = SysConfig.getInstance().getProperty("mail", "mail.server");
String from = SysConfig.getInstance().getProperty("mail", "mail.name");
String pwd = SysConfig.getInstance().getProperty("mail", "mail.password");
Mail ms = new Mail(host, from, pwd, title, context);
if (StringUtil.isEmpty(host) || StringUtil.isEmpty(from) || StringUtil.isEmpty(pwd)) {
msg = "邮件服务器未配置,请联系管理人员!";
return msg;
}
// 收件人
List<String> targets = new ArrayList<>();
if (ids != null && ids.length() < 1 && others!= null && others.length() < 1) {
msg = "收件人为空!";
return msg;
}
if (ids != null && ids.length() > 0) {
String[] idArray = ids.split(",");
for (String id : idArray) {
User user = userService.getUserByid(Integer.parseInt(id));
if (user != null) {
String email = user.getEmail();
if (!StringUtil.isEmpty(email) && !targets.contains(email)) {
targets.add(email);
} else {
log.warn(user.getName() + "未配置邮件,请为该用户配置邮件后重试!");
}
}
}
}
if (others!= null && others.length() > 0) {
String[] otherArray = others.split(";");
for (String other : otherArray) {
if (!targets.contains(other)) {
targets.add(other);
}
}
}
for (String target : targets) {
try {
ms.addTarget(target);
} catch (Exception e) {
msg = "添加邮件接收人失败!";
this.log.error("添加邮件接收人失败: " + target + "\n" + e.getStackTrace());
e.printStackTrace();
}
}
try {
mailSend.sendmail(ms);
// SysConfig.getInstance().addMailSend(ms);
} catch (Exception e) {
msg = "发送邮件失败!";
this.log.error("发送邮件失败: " + "\n" + e.getStackTrace());
e.printStackTrace();
}
return msg;
}