Java Mailing Logic: Failed to Convert Socket to TLS

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

When I continue SSL permission with the following code:

I get 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 remove EnableSSL and try to add 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); 

It seems that over the past 3 days, I realized that I need to configure a trusted certificate, for example, here .

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

+4
source share
4 answers

Whether SSL / TLS is required or not is controlled by your mail server. If required, you should 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 Frequently Asked Questions .

+10
source

Usually you need to specify the host, port of your SMTP server and whether your SMTP server should request authentication when sending letters or not, and if communication with the SMTP server should be secured via SSL / TLS. You must correctly specify the appropriate properties, and this is necessary.

But the above parameters are dictated by the SMTP server. The server decides whether it should accept only secure connections; and whether it should use SSL or more secure TLS, and it will be either one of them, or both. In addition, some SMTP servers require sender authentication when sending emails, and some do not. So, you need to check the documentation of the SMTP server, which you must use in order to configure the settings correctly. Contact here for a working example that uses Gmail to send emails

0
source

If you use

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

Remove this snippet to avoid SSL configuration from the SMTP server.

0
source

Hey. Deactivate your antivirus, it works for me.

I deactivate only outgoing SMTP in antivirus

0
source

Source: https://habr.com/ru/post/1488055/


All Articles