/**
* 促销邮件发送后同时给网站管理员发一份游客信息
*
* @param bo
* @return
*/
@PostMapping("/sendEmailForManager")
public Boolean sendEmailForMessage(@RequestBody SendSimpleMessageBo bo) {
boolean success;
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
// 连接协议
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.smtp.host", "smtp.163.com");
// 指定验证为true
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.timeout", "10000");
props.setProperty("mail.smtp.port", "25");
//ssl配置,本地用不了
//props.setProperty("mail.smtp.port", "465");
//MailSSLSocketFactory sf = null;
//try {
// sf = new MailSSLSocketFactory();
// sf.setTrustAllHosts(true);
//} catch (GeneralSecurityException e) {
// e.printStackTrace();
//}
//props.put("mail.smtp.ssl.enable", true);
//props.put("mail.smtp.ssl.socketFactory", sf);
// 验证账号及密码,密码需要是第三方授权码
Authenticator auth = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("****@163.com", "授权吗");
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
MimeMessage message = new MimeMessage(session);
try {
//防止成为垃圾邮件,披上outlook的马甲(发送到谷歌邮箱容易被标记成垃圾邮件)
message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
// 设置发送者
message.setFrom(new InternetAddress("****@163.com"));
BackendConfigBo configBo = new BackendConfigBo();
configBo.setConfigName("ManagerEmail");
List<BackendConfigVo> list = configService.queryList(configBo);
String toEmail = list.get(0).getRemark();
// 设置发送方式与接收者
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
// 设置主题
message.setSubject(bo.getSubject());
//一个Multipart对象包含一个或多个bodypart对象,组成邮件正文
MimeMultipart multipart = new MimeMultipart();
BodyPart text = new MimeBodyPart();
text.setContent(bo.getText(), "text/html; charset=utf-8");
multipart.addBodyPart(text);
//读取本地图片,将图片数据添加到"节点"
if (!CollUtil.isEmpty(bo.getPicList())) {
for (String f : bo.getPicList()) {
MimeBodyPart imagePart = new MimeBodyPart();
String replace = f.replace(NtqConfig.getDomainUrl(), "");
DataSource fds = new FileDataSource(NtqConfig.getProfile() + replace);
imagePart.setDataHandler(new DataHandler(fds));
imagePart.setContentID(f);
multipart.addBodyPart(imagePart);
}
}
multipart.setSubType("related");
message.setContent(multipart);
// 3.创建 Transport用于将邮件发送
Transport.send(message);
success = true;
System.out.println("给 " + bo.getTo() + " 发送邮件成功");
} catch (Exception e) {
System.err.println("给 " + bo.getTo() + " 发送邮件失败");
//System.out.println(e.getMessage());
e.printStackTrace();
success = false;
}
return success;
}
04-09
411
02-22
937