0

我正在尝试使用 JavaMail 从包(在 Virgo 上运行)发送邮件,如果我在同一台机器上尝试来自简单测试 jar(不是 osgi)的相同代码,它会成功。

这是我的代码:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
        "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        "user@gmail.com", "password");
            }
        });

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("user@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("tomail@gmail.com"));
    message.setSubject("Testing 123");
    message.setText("test123");

    // create the message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();

    // fill message
    messageBodyPart.setText("testing testing testing testing");

    Multipart multipart = new MimeMultipart("mixed");
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource("/tmp/sample.pdf");
    messageBodyPart.setDataHandler(new DataHandler(source));

    messageBodyPart.setFileName("/tmp/sample.pdf");
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}

这是一个例外:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at mailsender.test.SendMail(test.java:232) ~[mailsender-bundle-2.2.1.5.jar/:na]
        at mailsender.test.createReport(test.java:152) ~[mailsender-bundle-2.2.1.5.jar/:na]
        at mailsender.test.eventjob.execute(eventjob.java:100) [mailsender-bundle-2.2.1.5.jar/:na]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [com.springsource.org.quartz-1.6.2.jar:1.6.2]
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) [com.springsource.org.quartz-1.6.2.jar:1.6.2]
Caused by: javax.mail.MessagingException: Exception reading response
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462) ~[na:na]
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260) ~[na:na]
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370) ~[na:na]
        at javax.mail.Service.connect(Service.java:297) ~[com.springsource.javax.mail-1.4.0.jar:1.4]
        at javax.mail.Service.connect(Service.java:156) ~[com.springsource.javax.mail-1.4.0.jar:1.4]
        at javax.mail.Service.connect(Service.java:105) ~[com.springsource.javax.mail-1.4.0.jar:1.4]
        at javax.mail.Transport.send0(Transport.java:168) ~[com.springsource.javax.mail-1.4.0.jar:1.4]
        at javax.mail.Transport.send(Transport.java:98) ~[com.springsource.javax.mail-1.4.0.jar:1.4]
        at mailsender.test.SendMail(test.java:227) ~[mailsender-bundle-2.2.1.5.jar/:na]
        ... 4 common frames omitted
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1731) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:925) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1170) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:785) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75) ~[na:1.6]
        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97) ~[na:na]
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) ~[na:1.6.0_33]
        at java.io.BufferedInputStream.read(BufferedInputStream.java:237) ~[na:1.6.0_33]
        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:75) ~[na:na]
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1440) ~[na:na]
        ... 12 common frames omitted
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323) ~[na:1.6.0_33]
        at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217) ~[na:1.6.0_33]
        at sun.security.validator.Validator.validate(Validator.java:218) ~[na:1.6.0_33]
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249) ~[na:1.6]
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185) ~[na:1.6]
        ... 24 common frames omitted
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174) ~[na:1.6.0_33]
        at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238) ~[na:1.6.0_33]
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:318) ~[na:1.6.0_33]
        ... 30 common frames omitted

有任何想法吗 ?

4

2 回答 2

2

http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert

于 2012-06-20T19:18:50.790 回答
1

它看起来像服务器配置问题,我在 Equinox 中运行相同的代码,所以它不太可能是与 OSGi 相关的问题。

我认为你需要将你的密钥(或者你的根密钥)安装到 Virgo,有点像这样:

https://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services

于 2012-06-20T16:47:43.060 回答