Below is the code I am using for testing purpose (I used the same librarires which are used in my project).
package test;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailSender {
public static void send(String from, String password, String to, String sub, String msg) {
// Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
// get Session
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
// compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setFrom(new InternetAddress("sannavdev@gmail.com"));
message.setSubject(sub);
message.setText(msg);
// send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
final String fromEmail = "sannavdev@gmail.com"; // requires valid gmail id
final String password = "sannavdev@1234"; // correct password for gmail id
final String toEmail = "vethsa.teja@broadcom.com"; // can be any email id
EmailSender.send(fromEmail, password, toEmail, "hello javatpoint", "How r u?");
}
}
Code is running fine in eclipse. But when I run the code in IPV 6 environment I am getting below error:
Failed to sent mail javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; nested exception is: java.net.SocketTimeoutException: connect timed out