Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook Email Setup for JavaMail API

I having headache in setting up the outlook mail to JavaMail, I had followed the instructions from here and here

Properties props = new Properties();
          props.put("mail.smtp.user", d_email);
          props.put("mail.smtp.host", "smtp-mail.outlook.com");
          props.put("mail.smtp.port", "587");
          props.put("mail.smtp.starttls.enable","true");
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.socketFactory.port", "587");
          props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.socketFactory.fallback", "true");

          try
          {
          Authenticator auth = new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(d_email, d_password);
              }
            };

          Session session = Session.getInstance(props, auth);

          MimeMessage msg = new MimeMessage(session);
          msg.setText("Hey, this is the testing email.");
          msg.setSubject("Testing");
          msg.setFrom(new InternetAddress(d_email));
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
          Transport.send(msg);

          }catch (MessagingException mex) {
             mex.printStackTrace();
          }

And I keep getting the errors:

javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)

I had searching around google and stackoverflow, I guess it is a MUST to use SSL instead of TLS because outlook doesn't accept TLS or else the connection will be close by remote host, but the solutions I found from Internet didn't work out. What am I missed in order to send my testing mail successfully?

I am total new to mail API, any advises or helps will be much appreciated. Thanks.

EDIT: I have a email account from microsoft I registered similar to '[email protected]'

like image 611
Lance Zhao Zai Avatar asked Jul 01 '16 13:07

Lance Zhao Zai


People also ask

Is JavaMail deprecated?

The App Engine Mail API (which also supports JavaMail) has already been deprecated. Instead, GCP recommends to use a third-party mail provider such as: SendGrid. Mailgun.

How do I send an email to multiple recipients using JavaMail?

addRecipient(Message.RecipientType.CC, "[email protected],[email protected],[email protected]"); Or message. addRecipient(Message.RecipientType.CC, "[email protected];[email protected];[email protected]"); I can send a message using alternate methods too, but I want to know the purpose of the above method.


1 Answers

Delete the SocketFactory parameters like this, and enjoy sending emails.

props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true"); 

I didn't find this parameters necessary on the documentation.

https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

like image 193
Angel Cuenca Avatar answered Sep 23 '22 01:09

Angel Cuenca