I have this code to send mail using SSL and company mail server.
public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
System.setProperty("java.net.useSystemProxies", "true");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.starttls.enable", "true");
props.setProperty("mail.smtps.host", "mail.company.au");
props.setProperty("mail.smtps.user", username);
props.setProperty("mail.smtps.password", password);
props.setProperty("mail.smtps.port", "587");
props.setProperty("mail.smtps.auth", "true");
props.setProperty("mail.smtps.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtps.timeout", "5000");
props.setProperty("mail.smtps.connectiontimeout", "5000");
props.setProperty("mail.smtps.writetimeout", "5000");
Session session = Session.getInstance(props,null);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("mail.company.au", username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
But I get this error can someone help?
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?