2013-10-21 290 views
7

發送電子郵件時出現問題。javax.mail.MessagingException:無法連接到SMTP主機:localhost,port:25

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服務器,它開始再次正常工作。

所以我找不到什麼理由。因爲有時候同樣工作正常,有時會導致異常。

任何人都可以幫我這個問題。

回答

2

它是從你的例外非常清楚,它試圖連接到localhost,而不是10.101.3.229

例外片斷:Could not connect to SMTP host: localhost, port: 25;

1)請檢查是否有任何空檢查其設置本地主機作爲默認值

2)重新啓動後,如果工作正常,那麼就意味着只能在首次運行,適當的值從屬性,並從旁邊的值設置爲默認運行服用。因此,保持房地產對象爲單身一個,並用它全在你的項目

+1

還檢查了JavaMail FAQ這些[常見錯誤](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)。 –

11
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);} 
    } 

}

+0

看一下它運行良好 –

+2

最好解釋一下,而不是代碼 –

+0

我想完成最後一個答案,因爲你有一個端口問題。你應該用465而不是25來改變默認端口。props.setProperty(「mail.smtp.socketFactory.port」,「465」);並添加了socketFactory。 –

0

這是不應該發生的。你可以嘗試這樣做嗎? 使用系統屬性和設置屬性如下:

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

如果你有相關的端口,然後設置這一點。

properties.setProperty("mail.smtp.port", "8080"); 
相關問題