javax.mail.AuthenticationFailedException:535 5.0.0身份验证失败 [英] javax.mail.AuthenticationFailedException: 535 5.0.0 Authentication Failed

查看:882
本文介绍了javax.mail.AuthenticationFailedException:535 5.0.0身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我会收到这个例外. 这是尝试发送电子邮件的代码.

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.

推荐答案

我同意@ Mi Mee. 在您的用户名中,您似乎使用了不完整的用户名(这是身份验证失败的原因).对于Hotmail,您必须输入 Windows Live ID ,该ID可以为xyz@hotmail.com , qrs@gmail.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 xyz@hotmail.com , qrs@gmail.com etc.

因此请输入正确的用户名.并从from变量中删除@hotmail.com.其余的代码很好.

So take in the correct username. And remove @hotmail.com from the from variable. Rest of the code is fine.

这篇关于javax.mail.AuthenticationFailedException:535 5.0.0身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆