0
    Properties props = new Properties();
    props.put("mail.smtp.proxy.host","host");
    props.put("mail.smtp.proxy.port","587");
    props.put("mail.smtp.host", "smtp.office365.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.ssl.enable", "false");
//  props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.ssl.protocols", "TLSv1.2");
    props.put("mail.smtp.starttls.required", "true");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.socketFactory.fallback", "true");




    Session session = Session.getInstance(props, new javax.mail.Authenticator(){
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("username@domain.com", "password");
        }
    });
    session.setDebug(true);

    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("This is the email subject");
        message.setText("This is the email body");

        Transport.send(message);

        System.out.println("mailsent");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }

I have written the above code for sending email using javamail API through SMTP but I ended up with the following exception. Please see my code below. I have used debugging mode and below the code you can find the exception as well.

Error Details

DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message javax.mail.MessagingException: Exception reading response;

Sowmya
  • 1
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 31 '23 at 09:21
  • 587 is (optionally but usually) STARTTLS, not (ever) SMTPS. Do specify `.starttls.enable=true` (but `.ssl.enable=false`). _Don't_ specify the socket factory(s) for javamail; this has been at best useless or as here harmful for two decades. See https://stackoverflow.com/questions/60654561/java-mail-cannot-connect-to-smtp-using-tls-or-ssl https://stackoverflow.com/questions/42431229/using-javamail-with-ssl-and-tls https://javaee.github.io/javamail/docs/SSLNOTES.txt . – dave_thompson_085 Jan 31 '23 at 11:46

0 Answers0