javax.mail.AuthenticationFailedException:535 5.0.0 身份验证失败

发布于 2024-11-26 01:03:41 字数 2292 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2024-12-03 01:03:41

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.

-小熊_ 2024-12-03 01:03:41

我同意@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 the from variable. Rest of the code is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文