我试图从捆绑包(在Virgo上运行)中使用JavaMail发送邮件,如果我在同一台计算机上尝试从简单测试罐(而不是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(
                        "[email protected]", "password");
            }
        });

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("[email protected]"));
    message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]"));
    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


有任何想法吗 ?

最佳答案

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

关于java - JavaMail在osgi捆绑包中失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11121490/

10-12 05:50