final int port = 587;
String host = "mail.website.com";
final String user = "abc@website.com";
final String password = "password";
String to = "abc@yourmail.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("demo");
message.setText("Hello");
Transport.send(message);
System.out.println("done");
} catch (MessagingException e) {
e.printStackTrace();
}
Error:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: