qq_37789351
Suoyi丶O
采纳率50%
2018-10-12 08:50

JavaEmail 本地测试代码 无法发送邮件,报错连接超时?

160
已采纳

经过本人测试,初步可以排除代码问题,
因为随便在网上COPY的小例子都无法发送邮件,
后台报错,如下:

 com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2118)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:146)
    at com.leron.base.controllers.EmailController.sendEmail(EmailController.java:121)
    at com.leron.base.controllers.EmailController.VerifyCode(EmailController.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1441)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2084)
    ... 50 more

然后我telnet了下本地端口 25端口是关闭掉的,我本地好像也没有SMTP服务,但是又不知道怎么去弄这个,所以求大神帮助下,感激不尽!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • zhaoqingkaitt 清月晨光 3年前

    配置正确的SMTP服务主机,保证服务连接上,假设你用的是163的话,确保你的163已经smtp服务已经开放,默认是关闭的,其它邮件服务器类似,在设置中可以设置

     String server = "smtp.163.com";
    String username = "";
    String password = "";
    
    Transport transport = session.getTransport();
    transport.connect(server, username, password);
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();
    
    点赞 评论 复制链接分享
  • liyiming2017 稀有气体 3年前

    端口号是邮件服务提供商提供的,新浪给多少就是多少。

    点赞 评论 复制链接分享
  • qq_37789351 Suoyi丶O 3年前
      //设置邮箱IP
            mailInfoConfig.setMailServerHost("smtp.sina.com");
            //设置端口号
            mailInfoConfig.setMailServerPort("465");
            //设置用户邮箱
            mailInfoConfig.setMailUsername("xxxx");
            //设置邮箱密码
            mailInfoConfig.setMailPassword("xxxx");
    

    现在解决了,不知道为什么 port设置为465就行了,希望有大神可以帮我解答疑惑。

    点赞 评论 复制链接分享
  • qq_34445142 爱打架的小白兔 3年前

    发送邮件,如果是自己的邮件服务器,那么需要检查你当前配置的host是否正确.如果是三方的话,例如阿里云或腾讯云的话,那么需要在三方后台进行配置.

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 3年前

    Couldn't connect to host, port: localhost, 25; timeout -1;
    nested exception is:
    java.net.ConnectException: Connection refused: connect

    localhost本地的25端口没有打开,你需要在防火墙打开。虚拟主机的话,还有额外的设置。另外检查smtp服务器是否在运行。

    点赞 评论 复制链接分享

相关推荐