javax.mail.AuthenticationFailedException: 535 5.0.0 Authentication Failed

ぃ、小莉子 提交于 2019-12-22 04:25:16

问题


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.


回答1:


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.

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




回答2:


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.



来源:https://stackoverflow.com/questions/6800561/javax-mail-authenticationfailedexception-535-5-0-0-authentication-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!