尝试从Java使用smtp服务器发送邮件时遇到此错误!如何解决这个问题

因此,我尝试使用Gmail smtp端口465通过邮件发送pdf,但它会不断抛出此错误, 用谷歌搜索,但无法解决此问题。

我不明白怎么了?帮助我吗?

提前致谢

尝试过:

1.更换端口

2.试图纠正证书上的某些问题无效

错误:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Exception reading response;
  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.sample.pdf.PDFMailing.pdfMail(PDFMailing.java:72)
    at com.sample.pdf.GeneratePdf.addDataToPdf(GeneratePdf.java:42)
    at com.sample.pdf.Report.main(Report.java:47)

PDFMailing.java

public static void pdfMail(String file){
        //Sender email-ID and Password.
                final String senderEmail="xxxxxx";//Sender Mail ID
                final String password="xxxxx";//Sender Mail ID Password.                
                //setting the Properties.
                Properties props=new Properties();
                props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
                props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
                props.put("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
                props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
                props.put("mail.smtp.port", "465"); //SMTP Port

                //Authenticating the mailID of the sender.
                    Authenticator auth = new Authenticator() {
                        //override the getPasswordAuthentication method
                        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                            return new javax.mail.PasswordAuthentication(senderEmail, password);
                        }
                    };

                    //Creating and getting the Session Object.
                 Session session=Session.getInstance(props, auth);

                 //Setting the From, To, Subject, MessageBody. 
                 try{
                     Message message=new MimeMessage(session);
                     message.setFrom(new InternetAddress(senderEmail));//Sender Mail ID
                     message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("harshapirate@gmail.com"));//Receiver Mail ID
                     message.setSubject("Sample ECO PDF file");
                     message.setText("This a Sample ECO PDF file.");

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

                      bodyPart.setText("This is multipart Text.");


                      //Attachments for any file.
                    MimeBodyPart pdfAttachment=new MimeBodyPart();
                    pdfAttachment.attachFile(file);

                    //Attach the Body part to the Multipart.
                    multipart.addBodyPart(bodyPart);
                    multipart.addBodyPart(pdfAttachment);

                    //Associate multipart to the message.
                    message.setContent(multipart);

                      System.out.println("Sending mail is in process.......");

                      //sending the message to-address mail.
                     Transport.send(message);
                     System.out.println("Mail has been sent sucessfully.");
                 }
                 catch(Exception e){
                     throw new RuntimeException(e);

                 }
    }
评论