获取PKIX路径构建失败

时间:2018-04-24 12:03:38

标签: java javamail

我试图通过gmail使用java发送邮件 但我得到一些例外 守则如下:

(SELECT count(*) as the-counts,(date(timestamp)) as the-timestamps FROM `user_data` WHERE 1 group BY the-timestamps)

例外情况如下:

 public static void send_java_mail() throws IOException
{
    System.out.println("\n\n\n\nIn send_java_mail");
    String[] recipents=new String[10];
    recipents[0]=email_id;//email_id;       
    //put your smtp details in the properties file.Following given are for gmail account

    Properties smtp_properties =null;
    smtp_properties = new Properties();
    InputStream iss = new FileInputStream("D:\\Heat\\Web\\smtpconfig.properties");
    smtp_properties.load(iss);


    String e_mailbody="Hi\n\tYour Order has been Placed.It will deliever in 2 Days.\n\n\tYour Name :"+CustomerName+"\n\tYour Email Address :"+email_id+"\n\tYour Shipping Address :"+Addr+"\n\tYour Contact Number:"+Phone+"\n\tYour Total amount to be Pay is 3610.\nThanks.";
    String host = smtp_properties.getProperty("host");
    String mail_transport_protocol = smtp_properties.getProperty("mail.transport.protocol");
    String mail_smtp_auth =smtp_properties.getProperty("mail.smtp.auth");
    String mail_smtp_port = smtp_properties.getProperty("mail.smtp.port");
    String mail_debug =smtp_properties.getProperty("mail.debug");
    String mail_smtp_socketFactory_port =smtp_properties.getProperty("mail.smtp.socketFactory.port");
    String mail_smtp_socketFactory_class =smtp_properties.getProperty("mail.smtp.socketFactory.class");
    String mail_smtp_socketFactory_fallback = smtp_properties.getProperty("mail.smtp.socketFactory.fallback");

    Properties props = System.getProperties();
    System.out.println("mail transport protocol :"+mail_transport_protocol);
    props.setProperty("mail.transport.protocol",mail_transport_protocol);     
    props.setProperty("mail.host", host);  
    props.put("mail.smtp.auth", mail_smtp_auth);  
    props.put("mail.smtp.port", mail_smtp_port);  
    props.put("mail.debug", mail_debug);
    props.setProperty("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", mail_smtp_socketFactory_port);  
    props.put("mail.smtp.socketFactory.class",mail_smtp_socketFactory_class);  
    props.put("mail.smtp.socketFactory.fallback", mail_smtp_socketFactory_fallback);  

    final String from="dhanshreeherballife@gmail.com";
    final String pass="D@ny1996";

    Session session = Session.getDefaultInstance(props,  new javax.mail.Authenticator() {    
        protected PasswordAuthentication getPasswordAuthentication() {    
            return new PasswordAuthentication(from,pass);  
            }    
           });  
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[recipents.length];

            toAddress[0] = new InternetAddress(recipents[0]);

            message.addRecipient(Message.RecipientType.TO, toAddress[0]);


            message.setSubject("Order");
        message.setText(e_mailbody);
        Transport transport = session.getTransport("smtp");

        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }               
}

1 个答案:

答案 0 :(得分:0)

首先修复所有这些common JavaMail mistakes

JavaMail FAQ有more information on cert path errors。如果您在连接到Gmail时遇到此错误,则最有可能的是您有防火墙或防病毒产品拦截连接并提供自己的证书而不是Gmail证书。在使用上述链接中的任何技术禁用安全检查之前,您应该仔细考虑允许该软件监视连接的安全隐患。

您还应该检查您的JDK安装是否配置错误或损坏。也许您的应用程序服务器会覆盖JDK附带的cacerts文件(信任库)。