有人可以告诉我为什么我们会得到以下例外

时间:2013-05-09 07:02:45

标签: javamail

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 = "mymail@gmail.com";//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());
                }
}}

}