title: How to fix Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
body: <p>I am trying to send email using Java Mail API from my application. The code I am using is as follows:</p>
  
  <pre><code>   private void sendMail(Properties props, String fromEmail, String password, String toEmail, String messageSubject, String messageBody)
      {
          final String username = fromEmail;
          final String pasword = password;
          //props.put("mail.smtp.starttls.enable", "true");
          Session session = Session.getInstance(props, new javax.mail.Authenticator() 
          {
              protected PasswordAuthentication getPasswordAuthentication()
              {
                  return new PasswordAuthentication(username,pasword);
              }
          });
      try 
      {
          Message message = new MimeMessage(session);
          message.setFrom(new InternetAddress(username));
          message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
          message.setSubject(messageSubject);
          message.setText(messageBody);
          Transport.send(message);
      } 
      catch (MessagingException e)
      {
          e.printStackTrace();
          throw new RuntimeException(e);
      }
      }
  </code></pre>
  
  <p>And I have the following properties:</p>
  
  <pre><code>{mail.smtp.port=587, mail.smtp.socketFactory.port=465, mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory, mail.smtp.auth=true, mail.smtp.host=smtp.office365.com}
  </code></pre>
  
  <p>I am getting the following exception:</p>
  
  <pre><code>Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
  </code></pre>
  
  <p>The same code was working for <code>smtp.gmail.com</code> and I was able to send mails from gmail based emails. Is there any solution for this issue.</p>