1

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?

1 Answers1

0

Here is a snippet i found some where some months ago.(I do not remember where exactly).

public static void sendMail(String recipient, String subject, String content) {
// config
// Sender's email ID 
String from = "YOUR SENDER ADDRESS HERE";

// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtps.host", "PLACE YOUR SERVER DOMAIN e.g. smtps.example.com");
properties.setProperty("mail.smtps.port", "465");
properties.setProperty("mail.smtps.auth", "true");
properties.setProperty("mail.user", "YOUR USERNAME");
properties.setProperty("mail.password", "YOUR SMTP PASSWORD");
//properties.put("mail.debug", "true");

// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

try{
  MimeMessage message = new MimeMessage(session);

  // Set From: header field of the header.
  message.setFrom(new InternetAddress(from));

  // Set To: header field of the header.
  message.addRecipient(Message.RecipientType.TO,
      new InternetAddress(recipient));

  message.setSubject(subject);
  message.setText(content);

  Transport trnsport;
  trnsport = session.getTransport("smtps");
  trnsport.connect(null, properties.getProperty("mail.password"));
  message.saveChanges();
  trnsport.sendMessage(message, message.getAllRecipients());
  trnsport.close();
  System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
  mex.printStackTrace();
}

}

Eritrean
  • 15,851
  • 3
  • 22
  • 28