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

前端 未结 14 2055
广开言路
广开言路 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 11:13

    I've solved this issue adding user and password in Transport.send call:

    Transport.send(msg, "user", "password");
    

    According to this signature of the send function in javax.mail (from version 1.5):

    public static void send(Message msg, String user, String password)

    Also, if you use this signature it's not necessary to set up any Authenticator, and to set user and password in the Properties (only the host is needed). So your code could be:

    private void sendMail(){
      try{
          Properties prop = System.getProperties();
          prop.put("mail.smtp.host", "yourHost");
          Session session = Session.getInstance(prop);
          Message msg = #createYourMsg(session, from, to, subject, mailer, yatta yatta...)#;
          Transport.send(msg, "user", "password");
      }catch(Exception exc) {
          // Deal with it! :)
      }
    }
    

提交回复
热议问题