2013-10-21 14 views
7

Si verifica un problema durante l'invio di e-mail.javax.mail.MessagingException: Impossibile connettersi all'host SMTP: localhost, porta: 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) 

Io uso sotto pezzo di codice presentato.

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

programma di posta elettronica di invio è in esecuzione su Tomcat 5. Alcune volte si sta lavorando bene e alcune volte risultati di cui sopra fa eccezione. una volta satart risultante sopra l'eccezione risulta lo stesso su ogni accesso. ma non appena riavvio il server tomcat riprende a funzionare bene.

Quindi non riuscivo a trovare la ragione. come a volte lo stesso funziona bene ea volte risulta al di sopra dell'eccezione.

Qualcuno può aiutarmi a risolvere questo problema.

risposta

2

è molto chiaro dal vostro eccezione che sta tentando di connettersi a localhost e non 10.101.3.229

eccezione frammento: Could not connect to SMTP host: localhost, port: 25;

1.) Si prega di controllare se ci sono alcun controllo nullo, che sta mettendo localhost come valore predefinito

2.) Dopo il riavvio, se funziona correttamente, significa che solo al primo avvio, il valore corretto è stato preso da Proprietà e dalla prossima esecuzione il valore è impostato su predefinito. In modo da mantenere la proprietà-oggetto come un Singleton e usarlo su tutto il progetto

+1

Controllare anche le domande frequenti di JavaMail per questi [errori comuni] (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

Guarda questo funziona bene –

+2

è meglio spiegare invece del solo codice –

+0

Volevo completare l'ultima risposta perché hai un problema di porta. Dovresti cambiare la porta defaut con 465 anziché 25. props.setProperty ("mail.smtp.socketFactory.port", "465"); e ha aggiunto il socketFactory. –

0

Questo non dovrebbe accadere. Puoi provare a fare questo? utilizzare le proprietà di sistema e impostare la proprietà, come di seguito:

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

E se si dispone di una porta associata, quindi impostare questo pure.

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