0

I have checked the program, the code is working on one system but when I migrate this file to my other system I get the below error, I want to know why this is happening and how could I troubleshoot this issue? My credentials are correct

package javaapplication6;  

import java.util.Properties;    
import javax.mail.*;
import javax.mail.internet.*;    
class Mailer{  
    public static void send(String from,String password,String to,String sub,String msg){  
          //Get properties object    

          Properties props = new Properties();    
          props.put("mail.smtp.host", "smtp.gmail.com");    
          props.put("mail.smtp.socketFactory.port", "465"); 
          props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.auth", "true");    
          props.put("mail.smtp.port", "465");
          //get Session
          Session session = Session.getDefaultInstance(props,    
           new javax.mail.Authenticator() {    
           protected PasswordAuthentication getPasswordAuthentication() {    
           return new PasswordAuthentication("[email protected]","XXXXXXXXX");  
           }    
          });    
          //compose message    
          try {    
           MimeMessage message = new MimeMessage(session);    
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));    
           message.setSubject(sub);    
           message.setText(msg);    
           //send message  
           Transport.send(message);    
           System.out.println("message sent successfully");    
          } catch (MessagingException e) {throw new RuntimeException(e);}    

    }  
}  
public class SendEmail{    
 public static void main(String[] args) {    
     //from,password,to,subject,message  
     Mailer.send("[email protected]","XXXXXXXXX","[email protected]","hello javatpoint","How r u?");  
     //change from, password and to  
 }    
}

The exception I am getting:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;

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 javaapplication6.Mailer.send(SendEmail.java:32) at javaapplication6.SendEmail.main(SendEmail.java:39)

Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; 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

1

1 Answer 1

0

It was due to the Avast Antivirus that was blocking the ports so i uninstalled it and the program worked fine !! .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.