0

我正在使用 wildfly 一段时间,但我无法让它的邮件子系统工作。我看过很多教程如何设置它,无论是通过 Web 控制台、CLI 还是直接在standalone.xml 中,所以我很确定我的配置是正确的:

<subsystem xmlns="urn:jboss:domain:mail:2.0">
        <mail-session debug="true" name="Gmail" jndi-name="java:jboss/mail/Gmail" from="MAILFROM">
            <smtp-server outbound-socket-binding-ref="mail-smtp" ssl="true" username="MAILFROM" password="PASSWORD"/>
        </mail-session>
</subsystem>
<outbound-socket-binding name="mail-smtp">
        <remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>

然后在我的 Java 文件中:

@Resource(mappedName = "java:jboss/mail/Gmail")
private javax.mail.Session gmailSession;

public void sendEmail(String to,String subject,String msg){
 Message message = new MimeMessage(gmailSession);
 message.setFrom(newInternetAddress("MAILFROM"));
 message.setRecipients(Message.RecipientType.TO,
 InternetAddress.parse(to));
 message.setSubject(subject);
 message.setContent(msg,"text/html");
 Transport.send(message);}

我得到:

java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at com.liferoles.controller.UserManager.sendEmail(UserManager.java:487)
at com.liferoles.controller.UserManager.sendResetLink(UserManager.java:520)
at com.liferoles.rest.RestAuth.sendResetLink(RestAuth.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:329)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2020)
... 52 more

奇怪的是,当像这样直接使用 javax.mail API 时,它具有相同的属性:

public void sendEmail(String to,String subject,String msg){
        Properties properties = System.getProperties();
        String user = "MAILFROM";
        String passwd = "PASSWORD";
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.host", "smtp.gmail.com");
        properties.setProperty("mail.smtp.port", "587");
        javax.mail.Session gmailSession = javax.mail.Session.getInstance(properties,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, passwd);
                    }
                  });
        Message message = new MimeMessage(gmailSession);
        message.setFrom(new InternetAddress("MAILFROM"));
        message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(msg,"text/html");
        Transport.send(message);

我试图解决这个问题有两个原因:

  1. 我想在standalone.xml 中而不是在我的java 类中拥有我的凭据。
  2. 主要是因为我讨厌有些事情很奇怪,我无法让它工作!

我不清楚的第二件事是为什么我会得到这个异常:

    15:25:28,587 错误 [stderr](默认任务 10)javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorExc
eption:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效证书
请求目标的化路径
15:25:28,587 错误 [stderr](默认任务 10)在 com.liferoles.controller.UserManager.sendEmail(UserManager.java:484)
15:25:28,587 错误 [stderr](默认任务 10)在 com.liferoles.controller.UserManager.sendResetLink(UserManager.java:5
14)
15:25:28,587 错误 [stderr](默认任务 10)... 44 更多
15:25:28,588 错误 [stderr](默认任务 10) 原因:javax.mail.MessagingException:无法将套接字转换为 TLS;

15:25:28,588 错误 [stderr](默认任务 10)嵌套异常是:
15:25:28,588 错误 [stderr](默认任务 10)javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorExc
eption:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效证书
请求目标的化路径
15:25:28,588 错误 [stderr](默认任务 10)在 com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2000)
15:25:28,588 错误 [stderr](默认任务 10)在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:70
9)
15:25:28,588 错误 [stderr](默认任务 10)在 javax.mail.Service.connect(Service.java:386)
15:25:28,589 错误 [stderr](默认任务 10)在 javax.mail.Service.connect(Service.java:245)
15:25:28,589 错误 [stderr](默认任务 10)在 javax.mail.Service.connect(Service.java:194)
15:25:28,589 错误 [stderr](默认任务 10)在 javax.mail.Transport.send0(Transport.java:253)
15:25:28,589 错误 [stderr](默认任务 10)在 javax.mail.Transport.send(Transport.java:124)
15:25:28,589 错误 [stderr](默认任务 10)在 com.liferoles.controller.UserManager.sendEmail(UserManager.java:481)
15:25:28,590 错误 [stderr](默认任务 10)... 45 更多
15:25:28,590 错误 [stderr](默认任务 10)由:javax.net.ssl.SSLHandshakeException:sun.security.validator.Val
idatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到 v
到请求目标的有效认证路径

尝试通过我提到的第二种方法发送电子邮件时。仅当我使用standalone.bat 命令从控制台启动我的wildfly 服务器时才会出现此错误。如果我通过eclipse“在服务器上运行”运行它,它可以工作。第二次糊涂了。我试图通过将我的自签名证书添加到 jdks cacerts 来解决第二个问题,但它没有帮助。

4

1 回答 1

0

带有@Resource 的Java 代码属于哪种类?很可能没有注入资源,因为您的代码不在某种托管 bean/类中。检查 gmailSession 是否为非 null 以确保注入工作正常。

证书路径错误肯定是您的信任库中的证书有问题。通常,Gmail 证书应使用默认信任存储进行验证。也许您有拦截请求的防病毒产品?您检查过JavaMail 常见问题解答条目吗?

于 2016-03-21T17:29:43.990 回答