Mohit Singla
Mohit Singla

Reputation: 101

Could Not convert socket to TLS

I am trying to send email using java servlets in eclipse IDE. This is my Code.

    final String username = "******@gmail.com";
    final String password = "******";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.ssl.trust", "smtpserver");

    Session session1 = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try 
    {
        if(result)
        {

            Message message = new MimeMessage(session1);
            message.setFrom(new InternetAddress("******[email protected]"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));

            message.setSubject("Welcome To Our Bank");
            message.setText("Dear "+custname+","
                    +"\n\n Your Account has been Created Successfully."
                    +"\n\n Your Account Details Are:"
                    +"\n   User Id : "+userid+""
                    +"\n   Account Number : "+accno+""
                    +"\n   Login Password : "+passwd+""
                    +"\n   Transaction Password : "+t_passwd+"");

            Transport.send(message);
            out.println("mail sent");
        }
    } 
    catch(MessagingException e) 
    {
        out.println("Exception Caught : "+e);
}

This is Exception That occur after Execution.

Exception Caught : javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.io.IOException: Server is not trusted: smtp.gmail.com

Upvotes: 4

Views: 10343

Answers (2)

Rizwan_Mohammed
Rizwan_Mohammed

Reputation: 313

If your context is android application , then make sure your android device time is set to current date and time

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29971

By setting "mail.smtp.ssl.trust" to "smtpserver" you've said you only trust the server named "smtpserver". "smtp.gmail.com" is not named "smtpserver".

Upvotes: 5

Related Questions