问题描述
- javamail 多线程发送邮件出现邮件错乱的问题
-
大家好,使用javamail 进行多线程发送邮件,发生了本来是发送给B的内容,却发送给了A,而且概率不小,代码大概如下,请大家帮忙分析一下,以下这个方法就是多线程调用的,邮件的一些信息是使用MailSend bean封装进来,在调用这个方法之前,有日志记录,都显示是正确的,但是就发生了错乱,高手们帮看看,再不解决就丢饭碗了,先谢谢了!
/**- @Description: 以HTML格式发送邮件
- @param MailSend mailSend 待发送的邮件对象信息
- @return boolean true:发送成功 false:发送失败
*/
public boolean sendHtmlMail(MailSend mailSend) {
long stime = System.currentTimeMillis();
logger.debug("MailSendServer.sendHtmlMail(mailSend) entry: mailSend={}",new Object[] { mailSend.toString() });
Session sendMailSession = null;
Transport transport = null;
try {
// 判断是否需要身份认证, 根据邮件会话属性和密码验证器构造一个发送邮件的session
Properties properties = mailSend.getProperties();
if (mailSend.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
EmailAuthenticator authenticator = new EmailAuthenticator(
mailSend.getUserName(), mailSend.getPassword());
sendMailSession = Session.getInstance(properties, authenticator);
} else {
sendMailSession = Session.getInstance(properties);
}
// 是否开启 Mail Session Debug
if(mail_session_debug == 1) {
sendMailSession.setDebug(true);
}
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailSend.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailSend.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailSend.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailSend.getContent(), contentType);
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
// Transport.send(mailMessage);
// email send
mailMessage.saveChanges();
transport = sendMailSession.getTransport("smtp");
transport.connect(mailSend.getMailServerHost(),
Integer.parseInt(mailSend.getMailServerPort()),
mailSend.getUserName(), mailSend.getPassword());
logger.warn(
"mail transport: toAddress={}, title={}",
new Object[] {mailSend.getToAddress(),mailSend.getSubject()});
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
logger.debug("MailSendServer.sendHtmlMail(mailSend) success: costTime={}ms",
new Object[] { System.currentTimeMillis() - stime });
return true;
} catch (MessagingException e) {
logger.error("MailSendServer.sendHtmlMail(mailSend) fail: errorMessage={}",
new Object[] { e.getMessage() });
return false;
} catch (Exception e) {
logger.error("MailSendServer.sendHtmlMail(mailSend) fail: errorMessage={}",
new Object[] { e.getMessage() });
throw new ServiceException(e);
// return false;
} finally {
if(null != transport) {
try {
transport.close();
} catch (MessagingException e) {
logger.error("MailSendServer.sendHtmlMail(mailSend) fail when close transport : errorMessage={}",
new Object[] { e.getMessage() });
}
}
}
}
时间: 2024-10-21 15:07:04