0

I have written a code for sending email from java swing using javamail api .

    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");

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

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("melbino1992@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("melbino1992@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Hello");

        MimeBodyPart messageBodyPart;
        Multipart multipart = new MimeMultipart();
                    messageBodyPart = new MimeBodyPart();

                multipart.addBodyPart(messageBodyPart);
                    message.setContent(multipart);

        Transport.send(message);

        System.out.println("sent mail");

    } catch (Exception e) {
        //System.out.println(e);
        e.printStackTrace();
    }

         System.out.println("hello!!!!!!!!");
}

But it shows an exception as javax.mail.MessagingException: Could not convert socket to TLS;

The stack trace is as follows

javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at workregistration.Send.main(Send.java:59)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1902)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1338)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:154)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1032)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    ... 7 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1320)
    ... 17 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Melbin Mathew
  • 69
  • 1
  • 3
  • 11
  • How is it related to swing? – StanislavL Nov 14 '14 at 06:34
  • duplicate of http://stackoverflow.com/questions/12743846/unable-to-send-an-email-using-smtp-getting-javax-mail-messagingexception-could – jmn Nov 14 '14 at 06:42
  • Sorry that's my mistake – Melbin Mathew Nov 14 '14 at 06:42
  • I used the method-commenting props.put("mail.smtp.starttls.enable", "true"); But it still generate exception javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587, response: 421 – Melbin Mathew Nov 14 '14 at 06:50
  • @MelbinMathew That's not what you original question shows. Are you sure you're setting the SMTP host to smtp.gmail.com, i.e. props.put("mail.smtp.host", "smtp.gmail.com"); – jmn Nov 14 '14 at 09:45
  • If I use props.put("mail.smtp.host", "smtp.gmail.com"); The it will show exception javax.mail.MessagingException: Could not convert socket to TLS; If I alter the code such as props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); then it shows the exception javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587, response: 421. So could you tell me what is the real problem – Melbin Mathew Nov 14 '14 at 10:18
  • @MelbinMathew Put in both, not one or the other. – jmn Nov 14 '14 at 10:45

2 Answers2

1

do this

 props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

if that fails check your firewall settings

smushi
  • 701
  • 6
  • 17
  • It generate an exception: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587, response: 421 – Melbin Mathew Nov 14 '14 at 06:41
0

Please check firewall and disable antivirus shield and check again...

Anil Olakkal
  • 3,734
  • 2
  • 17
  • 17