我们的web项目使用javamail发送邮件。页面可修改发送邮件的服务器、发件人信息,但是现在在页面修改发件人邮箱地址和密码之后,发送邮件失败。重起Tomcat服务器之后,又可以正常发送邮件,各位大神,有谁知道为什么呢?
public boolean sendMail(String subject, String body){
Properties props = new Properties();
props.put("mail.smtp.host", server);
props.put("mail.smtp.auth", needAuth);
props.put("mail.smtp.sender", sender);
props.put("mail.smtp.password", password);
// 判断是否需要身份认证
MailAuthenticator authenticator = null;
if (needAuth) {
authenticator = new MailAuthenticator(sender, password);
}
try {
Session session = Session.getDefaultInstance(props, authenticator);
MimeMessage message = new MimeMessage(session);
InternetAddress from = null;
if(null != sender && !"".equals(sender)){
from = new InternetAddress(sender);
}
message.setFrom(from);
String[] str = to.split(",");
InternetAddress[] address = new InternetAddress[str.length];
for (int i = 0; i < str.length; i++) {
address[i] = new InternetAddress(str[i]);
}
message.setRecipients(Message.RecipientType.TO,address);
message.setSubject(subject, "UTF-8");
message.setSentDate(new Date());
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(body, "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp);
message.setContent(multipart);
Transport.send(message);
return true;
} catch (Exception e) {
return false;
}
修改发件人之后,报如下异常:
com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User 684966a9-37c9-4b09-ba32-ea282cf46b58
;
nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 Invalid User 684966a9-37c9-4b09-ba32-ea282cf46b58
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:146)