赞
踩
第一次做发邮件功能,
遇到一个javamail的问题:
javax.mail.SendFailedException: Invalid Addresses
是在收件地址不对或不存在的情况下出现的,并且那些对的地址也不能发送出去;解决方案如下,捕获处理并重发
try{
//发送代码
}catch (MailSendException se) {
Exception[] messageExceptions = se.getMessageExceptions();
SendFailedException sendFail;
try {
sendFail = (SendFailedException) messageExceptions[0];
} catch (ClassCastException e) {
LOGGER.error("class cast exception:[{}]", e.getMessage());
return;
}
LOGGER.error("send mail eerror,the invalid mail address:{}", sendFail.getInvalidAddresses());
Address[] address = sendFail.getValidUnsentAddresses();
if (null == address) {
return;
}
String[] validMails = new String[address.length];
for (int i = 0; i < address.length; i++) {
validMails[i] = String.valueOf(address[i]);
}
try {
message.setTo(validMails);
mailSender.send(message); //重新发送
} catch (MessagingException e) {
LOGGER.error("create MimeMessage exception.", e);
throw new ServerLayerException("-1");
}
} catch (MessagingException e) {
LOGGER.error("create MimeMessage exception.", e);
throw new ServerLayerException("-2");
}
参考资料:
java STMP协议群发邮件时有无效地址导致发送邮件失败_smtp invalid addresses_人称小马哥的博客-CSDN博客
javax.mail.SendFailedException: Invalid Addresses解决办法_雨中伫立_新浪博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。