2013-05-09 114 views
0

javax.mail.MessagingException:无法将套接字转换为TLS; 嵌套的例外是: javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径建设失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求的目标的有效证书路径有人请告诉我为什么我们可以得到以下异常

我的代码:

public class SendMail implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e1) 
    { 
       String TO = textTo.getText(); 
     Component Mail=sendMail; 
      if((TO.trim().length() < 1)) 
      { 
       JOptionPane.showMessageDialog(Mail, 
       "Please enter some address to send mail", 
       "No Address to send mail", JOptionPane.ERROR_MESSAGE); 
      } 

其他{

   String host = "smtp.gmail.com";//host name 
     String from = "[email protected]";//sender id 
     String to = textTo.getText();//reciever id 
     String pass = "password";//sender's password 
     String fileAttachment = textAttFile.getText();//file name for attachment 
     //system properties 
     Properties prop = System.getProperties(); 
     // Setup mail server properties 
     prop.put("mail.smtp.gmail", host); 
     prop.put("mail.smtp.starttls.enable", "true"); 
     prop.put("mail.smtp.host", host); 
     prop.put("mail.smtp.user", from); 
     prop.put("mail.smtp.password", pass); 
     prop.put("mail.smtp.port", "587"); 
     prop.put("mail.smtp.auth", "true"); 
     //session 
     Session session = Session.getInstance(prop, null); 
     // Define message 
     MimeMessage message = new MimeMessage(session); 

       try 
       { 
        message.setFrom(new InternetAddress(from)); 
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
        message.setSubject(textSub.getText()); 
        // create the message part 
        MimeBodyPart messageBodyPart = new MimeBodyPart(); 
        //message body 
        messageBodyPart.setText(body.getText()); 
        Multipart multipart = new MimeMultipart(); 
        multipart.addBodyPart(messageBodyPart); 
        //attachment 
        messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(fileAttachment); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(fileAttachment); 
        multipart.addBodyPart(messageBodyPart); 
        message.setContent(multipart); 
        //send message to reciever 
        Transport transport = session.getTransport("smtp"); 
        transport.connect(host, from, pass); 
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close(); 
        JOptionPane.showMessageDialog(sendMail, 
      "Mail Sent Successfully.", 
      "Mail Sent.", JOptionPane.INFORMATION_MESSAGE); 
    } 
       catch(Exception e) 
       { 
        System.out.println(e.toString()); 
       } 
}} 

}

回答

相关问题