Java Mailing Logic: Could not convert socket to TLS
Asked Answered
A

6

6

In one application, I implemented mail sending logic using java. I used smtp.gmail.com over 587 port with a valid gmail id and password. In development environment everything is working fine. But in production environment I need to use a different mailing server say smtp.xyz.in over port 25 with a valid email id and password on that domain.

When I continue with SSL enable with following code:

I am getting an error

Could not convert socket to TLS

SunCertPathBuilderException: Unable To Find Valid Certification Path To Requested Target

=======================================================

final ResourceBundle rsbd=ResourceBundle.getBundle("main/ResourceBundle/Dyna");

        // -- Attaching to default Session, or we could start a new one                                            

                    props.put("mail.smtp.host", smtpServer);

                props.put("mail.smtp.auth", "true");

                props.put("mail.debug", "true");

            props.put("mail.smtp.port", port); 

                props.put("mail.smtp.starttls.enable","true");

            props.put("mail.smtp.EnableSSL.enable","true");

  Session session =Session.getInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(admin_mail, admin_password);}});

         // -- Create a new message --
      Message msg = new MimeMessage(session);

     // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress(from));

      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));

      msg.setSubject(subject);
      msg.setText(emailContent);

      // -- Set some other header information --
      msg.setHeader("X-Mailer", "LOTONtechEmail");
      msg.setSentDate(new Date());

        // -- Send the message --
        Transport.send(msg);

When I am removing EnableSSL and trying by adding the following code:

(getting javax.mail.AuthenticationFailedException:535 5.7.3 Authentication unsuccessful)

==========================================================================

props.put("mail.smtp.socketFactory.port","25");

  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

  props.put("mail.smtp.socketFactory.fallback", "true");


  MailSSLSocketFactory sf=new MailSSLSocketFactory();

  sf.setTrustAllHosts(true);

  props.put("mail.smtp.ssl.socketFactory", sf);

By googling enough in last 3 days, I understand that I need to configure for trusted certificate like given here.

But I want to continue without encryption and without mugging to enable SSL. Is there a way to send emails by java programs through our own domain without enabling SSL. Any help will be appreciated.

Appendicectomy answered 25/6, 2013 at 11:18 Comment(5)
The error javax.mail.AuthenticationFailedException:535 5.7.3 Authentication unsuccessful means that your password/username are incorrect. This error is not relationated with SSL.Guideline
But I am able to access and send emails using the same email and password. More over the same logic is sending the email in my working System, but not working in production server(That is on a system in some intranet)Appendicectomy
Did you try adding the domain with your username? domain/usernameGuideline
Say the Domain is xyz.in, I am using mail.smtp.host: smtp.xyz.in mail.smtp.port:25 and mail address [email protected] that all things are valid as best of my knowledge. Is there any other way to send mail without enabling SSL using own domain server?Appendicectomy
I had same problem, this topic can help you: #12744346Guimar
W
11

Whether SSL/TLS is required or not is controlled by your mail server. If it requires it, you have to use it.

You can set the mail.smtp.ssl.trust property to ignore the certificate issue, or you can fix it as described in the JavaMail FAQ.

Waterway answered 25/6, 2013 at 17:20 Comment(0)
C
2

Hey Deactivate your antivirus it's working for me.

I deactivate only SMTP outbound in antivirus

Czarist answered 22/1, 2017 at 3:12 Comment(0)
N
1

If you are using

        props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");

Please remove this snippet to avoid SSL Configuration from SMTP server.

Natie answered 3/11, 2015 at 9:44 Comment(0)
K
1

I've found that we have to set the correct TLS version also, otherwise we may get the same error. Below property settings helped to solve the issue.

<props>
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
    <prop key="mail.smtp.ssl.protocols">TLSv1.2</prop>
    <prop key="mail.smtp.ssl.trust">mail.XyZ.com</prop>
</props>
Krefetz answered 13/11, 2020 at 13:5 Comment(0)
N
0

Usually, all you need to specify is the your SMTP server host, port and whether your SMTP server requires authentication when sending emails or not and if the communication with the SMTP server should be secured over SSL/TLS. You need to specify the corresponding properties correctly and thats it.

But, the above parameters are dictated by the SMTP server. The server decides whether it should accept only secured connections; and whether it should use SSL or more secure TLS and it would be either of them or both. Also, some SMTP servers do need senders authentication when sending emails while some don't. So, you need to check the documentation of the SMTP server you are to use to configure the parameters correctly. Refer here for a working example that uses Gmail to send emails

Nose answered 7/10, 2015 at 14:52 Comment(0)
G
0

This error could occur if you use an old JavaMail lib (mail.jar or javax.mail.jar). Download the newest version from here: https://javaee.github.io/javamail/

Guidotti answered 7/12, 2020 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.