简体   繁体   English

Wildfly 9.1 smtp邮件子系统-无法连接到主机,端口:localhost,25; 超时-1;

[英]Wildfly 9.1 smtp mail substem - Couldn't connect to host, port: localhost, 25; timeout -1;

I'm working with wildfly as for a while and I cant get its mail subsystem working. 我在一段时间内正在使用wildfly,但无法使其邮件子系统正常工作。 I have seen many tutorials how to set it up whether through web console, CLI or directly in standalone.xml so I'm pretty sure that my config is right: 我看过很多教程,无论是通过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>

And then in my Java file: 然后在我的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);}

And I get: 我得到:

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

The weird thing is that it works with same properties when using javax.mail API directly like this: 奇怪的是, 当直接使用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);

I'm trying to solve this issue for two reasons: 我尝试解决此问题有两个原因:

  1. I want to have my credentials in standalone.xml not in my java class. 我希望我的凭据位于standalone.xml中,而不是在Java类中。
  2. And mainly because I hate when something is weird and i cant get it working! 主要是因为我讨厌什么东西很奇怪而我却无法正常工作!

The second thing i do not understand clearly is why am I getting this exception: 我不清楚的第二件事是为什么会出现此异常:

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

when trying to send email by the second method I mentioned. 当我尝试通过第二种方法发送电子邮件时。 This error only occurs in case that I am running starting my wildfly server from console with standalone.bat command.. If I am runing it through eclipse "run on server" it works. 仅当我正在使用standalone.bat命令从控制台启动我的wildfly服务器时,才会发生此错误。如果我通过eclipse“在服务器上运行”来运行它,那么它将起作用。 Confused for a second time. 再次感到困惑。 I tried to solve this second problem by adding my selfsigned certificate to jdks cacerts but it didn't help. 我试图通过将我的自签名证书添加到jdks cacerts来解决第二个问题,但这没有帮助。

What kind of class is your Java code with @Resource in? 带有@Resource的Java代码是哪种类? Most likely the resource is not being injected because your code is not in a managed bean/class of some sort. 由于您的代码不在某种托管bean /类中,因此很可能没有注入资源。 Check whether gmailSession is non-null to make sure the injection is working. 检查gmailSession是否为非空值,以确保注入工作正常。

The cert path error is definitely a problem with certificates in your trust store. 证书路径错误绝对是您的信任存储区中的证书存在的问题。 Normally the Gmail certificate should validate with the default trust store. 通常,Gmail证书应使用默认的信任库进行验证。 Perhaps you have an anti-virus product that's intercepting the request? 也许您有拦截请求的防病毒产品? Did you check the JavaMail FAQ entry ? 您是否检查了JavaMail FAQ条目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 回复邮件时无法连接到SMTP主机:localhost,端口:25- - Could not connect to SMTP host: localhost, port: 25 - while REPLYing to mail com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587; 超时-1 - com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1 在本地主机上发送邮件有效,但是当部署到网络服务器时,我得到无法连接到主机,端口:smtp.gmail.com,587 - Sending mail on localhost works, but when deployed to webserver I get Couldn't connect to host, port: smtp.gmail.com, 587 Java Mail异常。 无法连接到SMTP主机:localhost,端口:25; - Java Mail Exception. Could not connect to SMTP host: localhost, port: 25; 邮件服务GAE问题-发送邮件异常“ javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:25;” - Mail service GAE issue - sending mail exception “javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;” 无法连接到SMTP主机:…,端口:25? - Could not connect to SMTP host: …, port: 25? MailConnectException:无法连接到主机,端口:smtp.sendgrid.net - MailConnectException: Couldn't connect to host, port: smtp.sendgrid.net javax.mail.messagingexception无法连接到SMTP主机:主机名端口:25响应:552 - javax.mail.messagingexception could not connect to SMTP host : hostname port:25 response : 552 无法连接到SMTP主机:smtp.live.com,端口:25 - Could not connect to SMTP host: smtp.live.com, port: 25 mail():无法通过“ localhost”端口25连接到邮件服务器,无法在php.ini中验证“ SMTP”和“ smtp_port”设置,或使用ini_set() - mail(): Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM