title: JavaMail - Client was not authenticated to send anonymous mail during MAIL FROM
body: <p>I'm trying to use JavaMail to send emails through office365 valid account. However, I am receiving the same exception '530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM'. Following the simple code I am using for testing:</p>
  
  <pre><code>Properties props = new Properties();
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.host", "smtp.office365.com");
  props.put("mail.smtp.port", "587");
  props.put("mail.debug", "true");
  
  Session session = Session.getInstance(props, new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(USER_NAME, USER_PASS);
      }
  });
  
  try {
      final Message message = new MimeMessage(session);
      message.setRecipient(Message.RecipientType.TO, new InternetAddress(USER_NAME));
      message.setFrom(new InternetAddress(USER_NAME));
      message.setSubject(subject);
      message.setText(messageContent);
      message.setSentDate(new Date());
      Transport.send(message);
      System.out.println("Send OK");
  } catch (final MessagingException ex) {
      System.out.println(ex.getMessage());
  }
  </code></pre>
  
  <p>Following the debug output:</p>
  
  <pre><code>DEBUG: JavaMail version 1.3
  DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_171\lib\javamail.providers (The system cannot find the file specified)
  DEBUG: !anyLoaded
  DEBUG: not loading resource: /META-INF/javamail.providers
  DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
  DEBUG: Tables of loaded providers
  DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
  DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
  DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  DEBUG: !anyLoaded
  DEBUG: not loading resource: /META-INF/javamail.address.map
  DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_171\lib\javamail.address.map (The system cannot find the file specified)
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: useEhlo true, useAuth true
  
  DEBUG: SMTPTransport trying to connect to host "smtp.office365.com", port 587
  
  DEBUG SMTP RCVD: 220 xxxxx.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 6 Jun 2019 07:53:32 +0000
  
  DEBUG: SMTPTransport connected to host "smtp.office365.com", port: 587
  
  DEBUG SMTP SENT: EHLO L3343005201
  DEBUG SMTP RCVD: 250-xxxxx.outlook.office365.com Hello [92.242.173.14]
  250-SIZE 157286400
  250-PIPELINING
  250-DSN
  250-ENHANCEDSTATUSCODES
  250-STARTTLS
  250-8BITMIME
  250-BINARYMIME
  250-CHUNKING
  250 SMTPUTF8
  
  DEBUG SMTP Found extension "SIZE", arg "157286400"
  DEBUG SMTP Found extension "PIPELINING", arg ""
  DEBUG SMTP Found extension "DSN", arg ""
  DEBUG SMTP Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP Found extension "STARTTLS", arg ""
  DEBUG SMTP Found extension "8BITMIME", arg ""
  DEBUG SMTP Found extension "BINARYMIME", arg ""
  DEBUG SMTP Found extension "CHUNKING", arg ""
  DEBUG SMTP Found extension "SMTPUTF8", arg ""
  DEBUG SMTP: use8bit false
  DEBUG SMTP SENT: MAIL FROM:&lt;sender@domain&gt;
  DEBUG SMTP RCVD: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxx.yyyyy.outlook.com]
  
  DEBUG SMTP SENT: QUIT
  Sending failed;
    nested exception is:
      class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxx.yyyyy]
  </code></pre>
  
  <p>From logs it seems the authentication is not working, and the connection port goes to 25 rather than the 587 as specified in the properties.</p>
  
  <p>I tried adding other properties as well, like</p>
  
  <pre><code>props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.user", USER_NAME);
  props.put("mail.smtp.password", USER_PASS);
  props.put("mail.smtp.from", USER_NAME);
  </code></pre>
  
  <p>in any combination, but no results.
  I've also tried to use the updated version of the JavaMail library, but the exception is the same. Am I missing any configuration on the mail account? Or from code?</p>
  
  <p>Thanks in advance</p>
  
  <p>Edited: changing the port from 587 (int) to "587" (string) solved the port issue in the debug port, but the same Client authentication error occurs</p>