Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 why 465 is not working

Viewed 10659

i want create email template , in that i want append html code to create a template ,so i tried below code where in this port 465 number is not working can any one helps me?

package com.indoabus2.mail;

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;

public class SendHTMLEmail {

public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "vpenchalaprasad2@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "vpenchalaprasad2@gmail.com";
      final String username = "vpenchalaprasad2";//change accordingly
      final String password = "100509732041";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "smtp.gmail.com";

      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "465");

      // Get the Session object.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
            }
    });

      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("Testing Subject");

       // Send the actual HTML message, as big as you like
       message.setContent(
              "<h1>This is actual message embedded in HTML tags</h1>",
             "text/html");

       // Send message
       Transport.send(message);

       System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
       e.printStackTrace();
       throw new RuntimeException(e);
      }
   }



}

nut the code is not getting executed exception is

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

i am not able to trace it why 465 is not working , and what is response: -1can any one suggest me a solution

2 Answers

The security file in my jre 1.8 was not allowing it to connect I used the corretto jre 1.8 version and it worked. This answer is coming after 2 days of visiting this and possible clones of this answer. The file in question here for me was in the path $JAVA_HOME/lib/security/java.security EDIT: there was a java 1.8 security release on april 20th 2021 this solution is only valid for people who are having issues after that

Related