我和野生蝇一起工作了一段时间,我无法让它的邮件子系统工作。我已经看过很多教程,无论是通过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);
我试图解决这个问题有两个原因:
我不明白的第二件事就是为什么我会得到这个例外:
15:25:28,587 ERROR [stderr] (default task-10)javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc eption: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid cert ification path to requested target 15:25:28,587 ERROR [stderr] (default task-10) at com.liferoles.controller.UserManager.sendEmail(UserManager.java:484) 15:25:28,587 ERROR [stderr] (default task-10) at com.liferoles.controller.UserManager.sendResetLink(UserManager.java:5 14) 15:25:28,587 ERROR [stderr] (default task-10) ... 44 more 15:25:28,588 ERROR [stderr] (default task-10) Caused by: javax.mail.MessagingException: Could not convert socket to TLS; 15:25:28,588 ERROR [stderr] (default task-10) nested exception is: 15:25:28,588 ERROR [stderr] (default task-10) javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc eption: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid cert ification path to requested target 15:25:28,588 ERROR [stderr] (default task-10) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2000) 15:25:28,588 ERROR [stderr] (default task-10) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:70 9) 15:25:28,588 ERROR [stderr] (default task-10) at javax.mail.Service.connect(Service.java:386) 15:25:28,589 ERROR [stderr] (default task-10) at javax.mail.Service.connect(Service.java:245) 15:25:28,589 ERROR [stderr] (default task-10) at javax.mail.Service.connect(Service.java:194) 15:25:28,589 ERROR [stderr] (default task-10) at javax.mail.Transport.send0(Transport.java:253) 15:25:28,589 ERROR [stderr] (default task-10) at javax.mail.Transport.send(Transport.java:124) 15:25:28,589 ERROR [stderr] (default task-10) at com.liferoles.controller.UserManager.sendEmail(UserManager.java:481) 15:25:28,590 ERROR [stderr] (default task-10) ... 45 more 15:25:28,590 ERROR [stderr] (default task-10) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.Val idatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find v alid certification path to requested target
尝试通过我提到的第二种方法发送电子邮件时。只有在我使用standalone.bat命令从控制台启动我的wildfly服务器时,才会出现此错误。如果我通过eclipse运行它,那么#34;在服务器上运行&#34;有用。第二次困惑。我尝试通过将自签名证书添加到jdks cacerts来解决第二个问题,但它没有帮助。
答案 0 :(得分:0)
@Resource的Java代码是什么类?很可能资源没有被注入,因为您的代码不在某种托管bean /类中。检查gmailSession是否为非null,以确保注入正常。
证书路径错误肯定是信任存储中的证书的问题。通常,Gmail证书应使用默认信任库进行验证。也许你有一个反病毒产品拦截了这个请求?您检查了JavaMail FAQ entry?