尝试在tomcat中发送邮件异常:连接超时:connect

时间:2018-10-31 15:09:41

标签: java email tomcat mailing

我尝试通过tomcat发送电子邮件,但我总是收到

  

连接超时:连接

     

at java.net.DualStackPlainSocketImpl.connect0(本机方法)       在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)       在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)       在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)       在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)       在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)       在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)       在java.net.Socket.connect(Socket.java:589)       在java.net.Socket.connect(Socket.java:538)       在sun.net.NetworkClient.doConnect(NetworkClient.java:180)       在sun.net.NetworkClient.openServer(NetworkClient.java:136)       在sun.net.smtp.SmtpClient.openServer(SmtpClient.java:128)       在sun.net.smtp.SmtpClient。(SmtpClient.java:200)       在sun.net.www.protocol.mailto.MailToURLConnection.connect(MailToURLConnection.java:86)       在sun.net.www.protocol.mailto.MailToURLConnection.getOutputStream(MailToURLConnection.java:97)       在de.mytoys.group.pfone.jira.statistic.Mailing.sendMail(Mailing.java:49)       在de.mytoys.group.pfone.jira.statistic.BuildStatistics.sendMail(BuildStatistics.java:527)       在de.mytoys.group.pfone.jira.statistic.BuildStatistics.doGet(BuildStatistics.java:91)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)       在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:166)       在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745)

我尝试了MailToURLConnection代码:

    public static void sendMail(String from, String to, String subject, String body){
    try{
       System.setProperty("mail.host", "localhost");
       URL u = new URL("mailto:"+to);
       MailToURLConnection con = (MailToURLConnection) u.openConnection();

       DateFormat df = new SimpleDateFormat("E, d MMM yyyy H:mm:ss Z");
       Date d = new Date();
       String dt = df.format(d);
       String mid = d.getTime()+from.substring(from.indexOf('@'));
       String message = "Subject: "+subject+"\r\n"
            + "Date: " +dt+ "\r\n"
            + "From: "+from+"\r\n"
            + "To: <"+to+">\r\n"
            + "\r\n"
            + body.replace("\n", "\r\n");
       System.out.println(u);
       System.out.println(message);
        OutputStream os = con.getOutputStream();
        OutputStreamWriter w = new OutputStreamWriter(os);
        w.write(message);
        w.flush();
        w.close();
        con.close();
       System.out.println("gesendet");
    }
    catch(Exception e){
        e.printStackTrace();
    }

但是它无法发送我的消息。我也用JavaMail尝试过,但也无法发送邮件。

有人可以告诉我我在做什么错吗?

0 个答案:

没有答案