javax.mail.MessagingException:无法连接到SMTP主机:localhost,port:25

问题描述 投票:9回答:5

我在发送电子邮件时遇到问题。

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)

我使用下面的代码片段。

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

电子邮件发送程序在tomcat 5上运行。有时它工作正常,有时它会导致异常。一旦它产生上述异常,它就会在每次访问时产生相同的结果。但是一旦我重新启动tomcat服务器,它就会再次正常工作。

所以我找不到原因。因为有时同样工作正常,有时会导致异常。

任何人都可以帮我解决这个问题。

java javamail tomcat5.5
5个回答
12
投票
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 = "[email protected]";//
     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("[email protected]"));
     msg.setRecipients(Message.RecipientType.TO, 
                      InternetAddress.parse("[email protected]",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);}
  }

}


3
投票

从您的例外情况可以看出,它正在尝试连接到localhost而不是10.101.3.229

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

1.)请检查是否存在将localhost设置为默认值的空检查

2.)重新启动后,如果它正常工作,则意味着仅在首次运行时,从属性中获取正确的值,并且从下次运行中将值设置为默认值。因此,将属性对象保持为单例对象并在整个项目中使用它


0
投票

这不应该发生。你能尝试这样做吗?使用系统属性并设置属性如下:

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

如果你有一个关联的端口,那么也设置它。

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

0
投票

尝试在启动JVM时设置属性,例如,添加-Djava.net.preferIPv4Stack=true

你无法在代码运行时设置它,因为java.net只是在jvm启动时读取它。

关于根本原因,本文给出了一些提示:Why do I need java.net.preferIPv4Stack=true only on some windows 7 systems?


0
投票

我也面临同样的错误。原因是您的环境中没有smtp服务器。为了创建虚假的smtp服务器,我使用了这个假的smtp.jar文件来创建虚拟服务器并监听所有请求。如果您遇到同样的错误,我建议您使用此jar并在解压缩后运行它,然后尝试运行您的应用程序。

Download latest version of fake smtp

© www.soinside.com 2019 - 2024. All rights reserved.