I have java program(Copied from google) to send emails using office365 SMTP, it is working fine as a stand java program but when I deploy this java program as jar file in web-inf/lib of a web application and calling the method from JSP it is throwing the below error:
javax.mail.SendFailedException: Sending failed;   nested exception is:
javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not
authenticated to send anonymous mail during MAIL FROM
Can someone please share their views on this issue. The java code :
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
public class SendEmailUsingSMTP {
    
    
   public static boolean sendEmail(String toAddress, String fromAddress, String userName, String userPassword,String smtpHost, String portNumber, String emailSubject,String emailBody) {
      // Recipient's email ID needs to be mentioned.
       
       Logger log = Logger.getLogger(SendEmailUsingSMTP.class);
       log.info("toAddress : "+toAddress);
       log.info("fromAddress : "+fromAddress);
       log.info("userName : "+userName);
       log.info("userPassword : "+userPassword);
       log.info("smtpHost : "+smtpHost);
       log.info("portNumber : "+portNumber);
       log.info("emailSubject : "+emailSubject);
       log.info("emailBody : "+emailBody);
       
       String to = toAddress;
      // Sender's email ID needs to be mentioned
      String from = fromAddress;//change accordingly
      final String username = userName;//change accordingly
      final String password = userPassword;//change accordingly
      // Assuming you are sending email through relay.jangosmtp.net
      String host = smtpHost;
      Properties props = new Properties();
      
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.socketFactory.port", portNumber);
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", portNumber);
      // Get the Session object.
      SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);
      props.put("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
      Session session = Session.getInstance(props, authenticator);
      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
         InternetAddress.parse(to));
         // Set Subject: header field
         message.setSubject(emailSubject);
         // Now set the actual message
         message.setText(emailBody);
         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      } catch (MessagingException e) {
            throw new RuntimeException(e);
      }
    return true;
   }
}
You can try with the following configuration, as it works for me:
"mail.smtp.starttls.enable":"true"
Also, I used host:
host = "Outlook.office365.com"
Hope this helps you or someone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With