Properties props = new Properties();
props.put("mail.smtp.host", host); //SMTP Host
props.put("mail.smtp.port", "465"); //SMTP Port
props.setProperty("mail.pop3.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.startssl.enable", "true");
props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userId, password);
}
};
Session session = Session.getDefaultInstance(props, auth); try {
Message message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(userId));
message.setText(emailMessage);
Transport.send(message);