Background: There is no problem in the local test after the email sending code is written on the local machine. However, after the server is deployed, it is found that the email sending will always report errors and the email cannot be sent. The exception is javax.mail. Mail. MessagingException:, whichever comes first

And the reason for that is

  • Because you are using the Session. GetDefaultInstance. Therefore, by default, no other server is authorized, and authorization is required when sending mail
  • When the mail server is called, SSL is required to make the call, so this exception is raised.

The solution:

  • My source
Properties props = System.getProperties(); // set up the connection to the mailbox server."mail.smtp.host", EMAIL_HOST);
            props.put("mail.smtp.port", PORT);
            props.put("mail.smtp.auth"."true");
            props.put("mail.smtp.starttls.enable"."true");
            MailAuthenticatorEntity auth = new MailAuthenticatorEntity(EMAIL_FROM, AUTH_CODE);
            Session session = Session.getDefaultInstance(props, auth);

            MimeMessage message = new MimeMessage(session);
Copy the code

Add a line in the code props. The put (” mail. The SMTP. Starttls. Enable “, “true”);

  • Reference links: javax.mail. Mail. MessagingException, whichever comes first