javax.mail.AuthenticationFailedException: failed to connect, no password specified?

前端 未结 14 1949
广开言路
广开言路 2020-12-17 10:46

This program attempts to send e-mail but throws a run time exception:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?
         


        
14条回答
  •  [愿得一人]
    2020-12-17 10:59

    Try to create an javax.mail.Authenticator Object, and send that in with the properties object to the Session object.

    Authenticator edit:

    You can modify this to accept a username and password and you can store them there, or where ever you want.

    public class SmtpAuthenticator extends Authenticator {
    public SmtpAuthenticator() {
    
        super();
    }
    
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
     String username = "user";
     String password = "password";
        if ((username != null) && (username.length() > 0) && (password != null) 
          && (password.length   () > 0)) {
    
            return new PasswordAuthentication(username, password);
        }
    
        return null;
    }
    

    In your class where you send the email:

    SmtpAuthenticator authentication = new SmtpAuthenticator();
    javax.mail.Message msg = new MimeMessage(Session
                        .getDefaultInstance(emailProperties, authenticator));
    

提交回复
热议问题