0

This is the code:

Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "mail.mydomain.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.user", "myemail@mydomain.com");
properties.put("mail.smtp.password", "mypassword");

Session sendSession = Session.getInstance(properties);

MimeMessage message = new MimeMessage(sendSession);
message.setFrom(new InternetAddress("myemail@mydomain.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(content);

Transport transport = sendSession.getTransport("smtp");
transport.connect("mail.mydomain.com", 465, "mydomain.com@mydomain.com", "mypassword");
transport.sendMessage(message, message.getAllRecipients());
transport.close();

These are the SSL/TLS setting:

Outgoing Server: mail.mydomain.com
SMTP Port: 465 

Executing the above code, it get stuck at:

transport.connect("mail.mydomain.com", 465, "mydomain.com@mydomain.com", "mypassword");

and nothing happen.

Why it get stuck at connect and how to fix it?

KunLun
  • 3,109
  • 3
  • 18
  • 65

2 Answers2

1

Try changing the mail.transport.protocol value from smtps to smtp.

You are trying to use port 465 which is for smtp . So you have to change the protocol to smtp . You are trying to start TLs with secure mode but you are using simple smtp port.

Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtp.starttls.enable", "true");
.....

Try changing it and send mail.

Umeshwaran
  • 627
  • 6
  • 12
0

I found the problem.

At Transport transport = sendSession.getTransport("smtp");

Instead of smtp should be smtps

Thanks to everybody who tried to help me.

KunLun
  • 3,109
  • 3
  • 18
  • 65