【问题标题】:Could some one please let me know why could we get the following exception有人可以让我知道为什么我们会得到以下异常
【发布时间】:2013-05-03 14:27:27
【问题描述】:

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

}

【问题讨论】:

    标签: jakarta-mail


    【解决方案1】:

    【讨论】:

    • 查看您的代码,我发现您正在连接到 Gmail,因此不应发生该异常。可能您有一些防病毒、防火墙或代理在您与 Gmail 的通信中进行干预并导致此问题。您可能需要检查您获得的证书并查看它的来源。
    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多