javax.mail.AuthenticationFailedException:535 5.0.0 身份验证失败
我不明白为什么我会得到这个例外。 这是尝试发送电子邮件的代码。
public void sendAsHotmail() {
final String username = jTextField14.getText();
final String password = jPasswordField4.getText();
String subject = jTextField16.getText();
String Cc = jTextField17.getText();
String Bcc = jTextField18.getText();
String recipient = jTextArea5.getText();
Properties props = new Properties();
props.put( "mail.smtp.host" , "smtp.live.com");
props.put( "mail.smtp.user" , username );
// Use TLS
props.put("mail.smtp.auth" , "true" );
props.put( "mail.smtp.starttls.enable" , "true" );
props.put( "mail.smtp.password" , password );
Session session = Session.getDefaultInstance( props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if( username == null | password == null )
JOptionPane.showMessageDialog( new JFrame() , "username or password incorrect");
return new PasswordAuthentication( username , password );
}
});
String to = recipient;
String from = username + "@hotmail.com";
String emailMessage = jTextArea2.getText();
MimeMessage message = new MimeMessage(session);
MimeBodyPart attachment = new MimeBodyPart();
MimeBodyPart messagePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource( fileName );
try {
message.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
message.setFrom( new InternetAddress(from) );
message.setSubject(subject);
message.setText( emailMessage );
attachment.setDataHandler( new DataHandler( fds ) );
attachment.setFileName( fileName );
messagePart.setText( emailMessage );
Multipart hotmailMP = new MimeMultipart();
hotmailMP.addBodyPart(attachment);
hotmailMP.addBodyPart( messagePart );
message.setContent( hotmailMP );
Transport transport = session.getTransport("smtp");
transport.send(message);
JOptionPane.showMessageDialog(new JFrame() , "mail sent !");
} catch(Exception exc) {
System.out.println(exc);
}
}
为什么我会得到这个异常?如果代码有任何问题,请告诉问题是什么。
I don't understand why i am getting this exception.
This is the code that attempts to send email message.
public void sendAsHotmail() {
final String username = jTextField14.getText();
final String password = jPasswordField4.getText();
String subject = jTextField16.getText();
String Cc = jTextField17.getText();
String Bcc = jTextField18.getText();
String recipient = jTextArea5.getText();
Properties props = new Properties();
props.put( "mail.smtp.host" , "smtp.live.com");
props.put( "mail.smtp.user" , username );
// Use TLS
props.put("mail.smtp.auth" , "true" );
props.put( "mail.smtp.starttls.enable" , "true" );
props.put( "mail.smtp.password" , password );
Session session = Session.getDefaultInstance( props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if( username == null | password == null )
JOptionPane.showMessageDialog( new JFrame() , "username or password incorrect");
return new PasswordAuthentication( username , password );
}
});
String to = recipient;
String from = username + "@hotmail.com";
String emailMessage = jTextArea2.getText();
MimeMessage message = new MimeMessage(session);
MimeBodyPart attachment = new MimeBodyPart();
MimeBodyPart messagePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource( fileName );
try {
message.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
message.setFrom( new InternetAddress(from) );
message.setSubject(subject);
message.setText( emailMessage );
attachment.setDataHandler( new DataHandler( fds ) );
attachment.setFileName( fileName );
messagePart.setText( emailMessage );
Multipart hotmailMP = new MimeMultipart();
hotmailMP.addBodyPart(attachment);
hotmailMP.addBodyPart( messagePart );
message.setContent( hotmailMP );
Transport transport = session.getTransport("smtp");
transport.send(message);
JOptionPane.showMessageDialog(new JFrame() , "mail sent !");
} catch(Exception exc) {
System.out.println(exc);
}
}
Why do i get this exception ? If there is anything wrong with the code please tell what the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
535 表示错误的用户名或密码:请参阅 SMTP 回复代码
您可能需要检查 SMTP 服务器的手册以了解 5.0.0 是什么方法。
535 means bad username or password: see SMTP reply codes
You might need to check your SMTP server's manual to see what 5.0.0 means.
我同意@Mi Mee 的观点。
在您的用户名中,您似乎使用了不完整的用户名(这就是身份验证失败的原因)。对于 Hotmail,您必须输入您的 Windows Live Id,可能是
[电子邮件受保护] , [电子邮件受保护]
等等输入正确的用户名。并从
from
变量中删除@hotmail.com
。其余代码都很好。I agree with @ Mi Mee .
In your username it seems you are taking the incomplete username (That is the reason Authentication failed). For Hotmail you have to enter your Windows Live Id that could be
[email protected] , [email protected]
etc.So take in the correct username. And remove
@hotmail.com
from thefrom
variable. Rest of the code is fine.