title: Java Mailing Logic: Could not convert socket to TLS
body: <p>In one application, I implemented mail sending logic using java. I used <code>smtp.gmail.com</code> over <code>587 port</code> 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 <code>smtp.xyz.in</code> over <code>port 25</code> with a valid email id and password on that domain.</p>
  
  <p>When I continue with SSL enable with following code:</p>
  
  <p>I am getting an error </p>
  
  <p><code>Could not convert socket to TLS</code> </p>
  
  <p><code>SunCertPathBuilderException: Unable To Find Valid Certification Path To Requested Target</code></p>
  
  <p>=======================================================</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>When I am removing EnableSSL and trying by adding the following code:</p>
  
  <p><code>(getting javax.mail.AuthenticationFailedException:535 5.7.3 Authentication unsuccessful)</code></p>
  
  <p>==========================================================================</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>By googling enough in last 3 days, I understand that I need to configure for trusted certificate like given <a href="http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/" rel="noreferrer">here</a>.</p>
  
  <p>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.</p>