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 = "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());
                }
}}

}

4

1 回答 1

0

此 JavaMail FAQ 条目可能会有所帮助:

于 2013-05-09T23:29:10.313 回答