【问题标题】:Sending mail error, javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;发送邮件错误,javax.mail.MessagingException: 无法连接到 SMTP 主机:localhost,端口:25;
【发布时间】:2020-04-08 00:20:18
【问题描述】:

这是我的代码

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class MailSendClass {
    public static void main (String [] args){

      // Recipient's email ID needs to be mentioned.
      String to = "abc82@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "xyz@gmail.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);

      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
         message.setSubject("Thanks for registering on our website!");

         // Now set the actual message
         message.setText("Welcome To Job Portal !!!!  Again Thanks ");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }

    }
}

我每次都会收到这个错误

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at MailSendClass.main(MailSendClass.java:58)
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:345)
    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:284)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
    ... 7 more
BUILD SUCCESSFUL (total time: 3 seconds)

我没有得到为什么会这样的错误。请帮我解决这个错误。

【问题讨论】:

  • 请问本地 smtp 服务器是什么?
  • 这可能很明显,但是您的开发机器上是否有一个邮件服务器正在运行,它实际上正在监听 localhost?
  • apache tomcat 服务器

标签: java netbeans jakarta-mail sendmail connectionexception


【解决方案1】:

错误是自我解释:javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

您在本地主机上没有 SMTP 服务器,但您在那里配置它:

  // Assuming you are sending email from localhost
  String host = "localhost";
  ...
  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

所以你必须:

  • 将本地 SMTP 服务器配置为本地系统上的中继(Postfix 或 sendmail 是两个众所周知的服务器)
  • 配置一个只跟踪邮件请求但甚至不尝试投递邮件的虚拟服务器(众所周知,Python 有这样的虚拟服务器开箱即用)
  • 或使用允许使用的服务器配置您的应用程序 - 在公司环境中联系您的系统管理员,或在个人环境中联系您的 ISP。无论如何,即使是配置真正的继电器,您也需要它。

【讨论】:

  • 我在我的应用程序中使用 GlassFish 服务器。
  • @KetanGupta:AFAIK GlassFish 不是 SMTP 服务器...只需在命令行尝试telnet localhost 25,如果出现错误,则表明本地没有运行 SMTP 服务器。
  • 我没有完全理解你。请给我完整的答案,因为我不知道这些事情。
  • @KetanGupta:Apache 是一个 HTTP 服务器,它知道如何处理 HTTP 或 HTTPS 请求并将资源发送回或将请求中继到另一个 HTTP[S] 服务器。 Tomcat 或 GlassFish 是 HTTP servlet 容器,它们知道如何处理 HTTP 请求并将它们传递给 servlet 和过滤器。 Postfix 或 sendmail 知道如何处理 SMTP 请求(默认端口 25)。它是一种不同的协议,由不同的服务器应用程序处理。你知道如何从开发机发送邮件,以及本地邮件程序是如何配置的吗?
【解决方案2】:

这是可行的解决方案兄弟。是有保证的

1) 首先打开您要从中发送邮件的 gmail 帐户,例如“xyz@gmail.com”

2) 在下方打开此链接 https://support.google.com/accounts/answer/6010255?hl=en

3) 点击“转到我的帐户中的“安全性较低的应用程序”部分。”选项

4) 然后开机

5)就是这样(:

【讨论】:

    【解决方案3】:

    您应该使用免费的 Google SMTP 服务器作为测试。

    mail.host=smtp.gmail.com
    mail.username=//your gmail
    mail.password=//your password
    mail.defaultEncoding=UTF-8
    mail.smtp.auth=true
    mail.smtp.starttls.required=true
    mail.smtp.starttls.enable=true
    mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    mail.smtp.socketFactory.fallback=false
    mail.smtp.port=465
    mail.smtp.socketFactory.port=465
    

    接下来,使用您的 gmail 登录,然后打开 less secure apps

    【讨论】:

    • 首先,不要设置那些 socketFactory 属性,they're not needed。其次,没有 mail.password 属性,也没有 mail.defaultEncoding 属性。阅读JavaMail FAQ,了解使用 Gmail 的正确方法。
    【解决方案4】:

    你应该看看这两行:

    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
    Caused by: java.net.ConnectException: Connection refused: connect
    

    错误是:“在 localhost 的 25 端口没有监听”。

    您正在尝试使用 localhost:25 作为邮件服务器,但那里没有服务器。

    【讨论】:

    • 这个问题的解决方法是什么?
    • 正确配置您机器上的本地 smtp 服务器。如果是 linux 系统,可以安装 postfix。
    【解决方案5】:

    只需使用此提供的解决方案:javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25

    并确保在您的 Google 帐户上开启安全性较低的应用访问权限。

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2011-11-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 2012-12-21
      相关资源
      最近更新 更多