简体   繁体   English

javax.mail.MessagingException:无法连接到 SMTP 主机:本地主机,端口:25

[英]javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25

I am getting issue while sending email.我在发送电子邮件时遇到问题。

javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at org.apache.jsp.online_005fScheme_005fSend_005fMail_jsp.sendMail(online_005fScheme_005fSend_005fMail_jsp.java:116)
    at org.apache.jsp.online_005fScheme_005fSend_005fMail_jsp._jspService(online_005fScheme_005fSend_005fMail_jsp.java:416)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Thread.java:619)

I use below code snipet.我使用下面的代码片段。

Properties props = new Properties();
     props.put("mail.smtp.host", "10.101.3.229");

email send program is running on tomcat 5. Some times it is working fine and some times it results above exception.电子邮件发送程序在 tomcat 5 上运行。有时它工作正常,有时它会导致异常。 once it satart resulting above exception it results the same on every access.一旦它启动导致上述异常,它在每次访问时都会产生相同的结果。 but as soon as i restart tomcat server it starts working fine again.但是一旦我重新启动 tomcat 服务器,它就会再次开始正常工作。

So i could not find what is reason.所以我找不到原因。 as sometimes same is working fine and sometimes results above exception.因为有时相同工作正常,有时结果高于异常。

Can anybody help me out this issue.任何人都可以帮我解决这个问题。

package sn;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
  public static void main(String[] args) {
    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
 // Get a Properties object
    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.store.protocol", "pop3");
    props.put("mail.transport.protocol", "smtp");
    final String username = "xxxx@gmail.com";//
    final String password = "0000000";
    try{
      Session session = Session.getDefaultInstance(props, 
                          new Authenticator(){
                             protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(username, password);
                             }});

   // -- Create a new message --
      Message msg = new MimeMessage(session);

   // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress("xxxx@gmail.com"));
      msg.setRecipients(Message.RecipientType.TO, 
                        InternetAddress.parse("user120@example.com",false));
      msg.setSubject("Hello");
      msg.setText("How are you");
      msg.setSentDate(new Date());
      Transport.send(msg);
      System.out.println("Message sent.");
    }catch (MessagingException e){ 
      System.out.println("Erreur d'envoi, cause: " + e);
    }
  }
}

It is very clear from your exception that it is trying to connect to localhost and not to 10.101.3.229从您的异常中可以清楚地看出它正在尝试连接到localhost而不是10.101.3.229

exception snippet : Could not connect to SMTP host: localhost, port: 25;异常片段: Could not connect to SMTP host: localhost, port: 25;

1.) Please check if there are any null check which is setting localhost as default value 1.) 请检查是否有任何空检查将 localhost 设置为默认值

2.) After restarting, if it is working fine, then it means that only at first-run, the proper value is been taken from Properties and from next run the value is set to default. 2.) 重新启动后,如果它工作正常,那么这意味着只有在第一次运行时,才从属性中获取正确的值,并且在下次运行时将该值设置为默认值。 So keep the property-object as a singleton one and use it all-over your project因此,将属性对象保持为单例对象,并在整个项目中使用它

This should not happen.这不应该发生。 Can you try doing this?你可以试试这样做吗? Use the system properties and set the property as below:使用系统属性并设置属性如下:

Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", "10.101.3.229");

And if you have a port associated, then set this as well.如果你有一个关联的端口,那么也设置它。

properties.setProperty("mail.smtp.port", "8080");

Try to set the property when starting JVM, for example, add -Djava.net.preferIPv4Stack=true .尝试在启动JVM时设置该属性,例如添加-Djava.net.preferIPv4Stack=true

You can't set it when code running, as the java.net just read it when jvm starting.你不能在代码运行时设置它,因为 java.net 只是在 jvm 启动时读取它。

And about the root cause, this article give some hint: Why do I need java.net.preferIPv4Stack=true only on some windows 7 systems?关于根本原因,本文给出了一些提示: 为什么我只在某些 Windows 7 系统上需要 java.net.preferIPv4Stack=true ? . .

I was also facing the same error.我也面临同样的错误。 The reason for this is that there is no smtp server on your environment.原因是您的环境中没有 smtp 服务器。 For creating a fake smtp server I used this fake-smtp.jar file for creating a virtual server and listening to all the requests.为了创建一个假的 smtp 服务器,我使用了这个 fake-smtp.jar 文件来创建一个虚拟服务器并监听所有的请求。 If you are facing the same error, I recommend you to use this jar and run it after extracting and then try to run your application.如果您遇到同样的错误,我建议您使用此 jar 并在解压缩后运行它,然后尝试运行您的应用程序。

Download latest version of fake smtp 下载最新版本的假smtp

看看这个解决方案,确保你在你的谷歌帐户上打开了对安全性较低的应用程序的访问: javax.mail.MessagingException:无法连接到 SMTP 主机:本地主机,端口:25

暂无
暂无

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

相关问题 发送邮件错误,javax.mail.MessagingException:无法连接到SMTP主机:本地主机,端口:25; - Sending mail error, javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:25; - javax.mail.MessagingException: 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;” javax.mail.MessagingException:无法连接到SMTP主机:<主机名>端口:25响应:554 - javax.mail.MessagingException: Could not connect to SMTP host : <host name> port : 25 response: 554 javax.mail.MessagingException:无法连接到SMTP主机:103.12.134.112,端口:25; - javax.mail.MessagingException: Could not connect to SMTP host: 103.12.134.112, port: 25; javax.mail.MessagingException:无法连接到SMTP主机:172.16.100.185,端口:25; - javax.mail.MessagingException: Could not connect to SMTP host: 172.16.100.185, port: 25; javax.mail.messagingexception无法连接到SMTP主机:主机名端口:25响应:552 - javax.mail.messagingexception could not connect to SMTP host : hostname port:25 response : 552 javax.mail.MessagingException无法连接到SMTP主机端口:25响应-1 - javax.mail.MessagingException could not connect to SMTP host port :25 response -1 通过Java发送电子邮件-javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:587; - Sending emails through Java - javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587; javax.mail.MessagingException:无法连接到 SMTP 主机:email-smtp.us-east-1.amazonaws.com,端口:25; - javax.mail.MessagingException: Could not connect to SMTP host: email-smtp.us-east-1.amazonaws.com, port: 25;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM