【问题标题】:"java.net.connectionException" showing connection refused“java.net.connectException”显示连接被拒绝
【发布时间】:2014-06-28 17:04:00
【问题描述】:

我正在尝试使用 java 邮件 api,并在我的 servlet 中有以下代码来发送邮件。我找不到解决错误的方法。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // Recipient's email ID needs to be mentioned.
          String to = "xyz@gmail.com";

          // Sender's email ID needs to be mentioned
          String from = "abc.com";

          // Assuming you are sending email from localhost
          String host = "localhost";

          // Get system properties
          Properties properties = System.getProperties();

          // Setup mail server
          properties.setProperty("mail.smtp.host", host);

          // Get the default Session object.
          Session session = Session.getDefaultInstance(properties);

          // Set response content type
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();

          try{
             // Create a default MimeMessage object.
             MimeMessage message = new MimeMessage(session);
             // Set From: header field of the header.
             message.setFrom(new InternetAddress(from));
             // Set To: header field of the header.
             message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));
             // Set Subject: header field
             String subject=request.getParameter("subject");
             String content=request.getParameter("mail");


             message.setSubject(subject);
             // Now set the actual message
             message.setText(content);
             // Send message
             Transport.send(message);
             String title = "Send Email";
             String res = "Sent message successfully....";
             String docType =
             "<!doctype html public \"-//w3c//dtd html 4.0 " +
             "transitional//en\">\n";
             out.println(docType +
             "<html>\n" +
             "<head><title>" + title + "</title></head>\n" +
             "<body bgcolor=\"#f0f0f0\">\n" +
             "<h1 align=\"center\">" + title + "</h1>\n" +
             "<p align=\"center\">" + res + "</p>\n" +
             "</body></html>");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          }

    }

我在控制台中收到以下错误:

原因:java.net.ConnectException:连接被拒绝:连接在 java.net.DualStackPlainSocketImpl.connect0(Native Method) 在 java.net.DualStackPlainSocketImpl.socketConnect(未知来源)在 java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(未知来源)在 java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) 在 java.net.Socket.connect(Unknown Source) 在 java.net.Socket.connect(Unknown Source) 在 com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312) 在 com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236) 在 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019) ... 29 更多

【问题讨论】:

  • String from = "abc.com"; - 首先这不是一个有效的电子邮件地址。

标签: java email servlets


【解决方案1】:

您需要在本地机器上安装 smtp 服务器或使用一些现有的 smtp 服务器(如 yahoo、gmail 等。您可以在互联网上获取 smtp 设置)。您可以使用 apache james 服务器并了解有关它的邮件发送 api。

【讨论】:

    【解决方案2】:

    您没有在 localhost 上运行 SMTP 服务器,并且您正在告诉 API 使用 localhost 作为您的邮件服务器。

    您要么需要安装本地 SMTP 服务器,要么将 smtp 主机名设置为您有权访问的服务器,例如:

          String host = "smtp.yourisp.com"; // real server name required here
    
          // Get system properties
          Properties properties = System.getProperties();
    
          // Setup mail server
          properties.setProperty("mail.smtp.host", host);
    

    【讨论】:

      猜你喜欢
      • 2015-05-18
      • 2018-06-10
      • 2023-03-21
      • 2017-08-10
      • 1970-01-01
      • 2014-11-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多