I am trying to send email via JavaMain API but getting read timeout exceptio.
Configurations I did ::
I have imported below dependencies in my gradle project ::
implementation("javax.mail:javax.mail-api:1.6.2")
implementation("com.sun.mail:javax.mail:1.6.2")
implementation("org.apache.commons:commons-email:1.5")
And Java code ::
System.setProperty("mail.debug", "true");
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.connectiontimeout", "50000"); // Set socket connection timeout to 5 seconds
properties.setProperty("mail.smtp.timeout", "50000"); // Set socket I/O timeout to 5 seconds
// Email authentication credentials
String username = "myUsername@....";
String password = "mypassword";
try {
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
// Create MimeMessage object
MimeMessage message = new MimeMessage(session);
// Set sender
message.setFrom(new InternetAddress("[email protected]"));
// Set recipient
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// Set subject
message.setSubject("Test Email");
// Set message body
message.setText("This is a test email sent using JavaMail API.");
// Create Transport object and send message
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} Catch (Exception e){
}
Error getting ::
Caused by: java.net.SocketTimeoutException: Read timed out
Can someone please help if I am missing anything in above code ?