-3

Code:

import java.util.*;  
import java.util.Date;
import java.util.Properties; 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail  
{  
public static void main(String [] args)
{
    String host="chnmail.hcl.com";
    final String user="allwinjayson.m@hcl.com";
    final String password="*********";
    String to="allwinjayson.m@hcl.com";

    //Get the session object  
    Properties props = new Properties();  
    props.put("mail.smtp.host",host);  
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");


    //new
    Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() 
    {  
            protected PasswordAuthentication getPasswordAuthentication() 
            {  
                return new PasswordAuthentication(user,password);  
            }  
    });  


     //Compose the message  
    try 
    {  
        MimeMessage message = new MimeMessage(session);  
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
        message.setSubject("Testing");
        message.setText("This is simple program of sending email using JavaMail API");

        //send the message  
         Transport.send(message);  
         System.out.println("message sent successfully...");  
    } 
    catch (MessagingException e) 
    {
        e.printStackTrace();
    }  
}
}  

Error:

javax.mail.MessagingException: Could not connect to SMTP host: chnmail.hcl.com, port: 25;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at SendingClass.SendEmail.main(SendEmail.java:55)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:694)
    at sun.security.ssl.InputRecord.read(InputRecord.java:527)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 7 more
fabian
  • 80,457
  • 12
  • 86
  • 114
Jays
  • 13
  • 3

1 Answers1

0

Don't specify the socket factory and it will work. SMTP over port 25 will initially be plaintext, and the starttls will then negotiate TLS for encryption.

Note that you will need JavaMail 1.4.5 or higher for this (current latest is 1.5.6).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197