web-dev-qa-db-fra.com

javax.mail.MessagingException: impossible de se connecter à l'hôte SMTP: smtp.gmail.com, port: 25;

Le code que j'utilise pour envoyer un simple mail

import javax.mail.*;
import javax.mail.internet.*;
import Java.util.*;
import Java.io.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class SendMailUsingAuthentication
{

  private static final String SMTP_Host_NAME = "smtp.gmail.com";
  private static final String SMTP_AUTH_USER = "[email protected]";
  private static final String SMTP_AUTH_PWD  = "mypassword";

  public static void main(String args[]) throws Exception
  {

  }

  public void postMail( String recipients[ ], String subject,String message , String from) throws MessagingException
  {
    try {
        boolean debug = false;

        Properties props = new Properties();
        props.put("mail.smtp.Host", SMTP_Host_NAME);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(debug);

        Message msg = new MimeMessage(session);

        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        msg.setSubject(subject);

        msg.setContent(message, "text/plain");

        Transport.send(msg);

    } 
    catch (Throwable e) 
    {
        e.printStackTrace();
    }
 }
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}
}

Lorsque j'exécute ce code sur ma machine locale, cela fonctionne bien .... Mais quand je l'ai déployé sur le serveur, cela me donne cette exception

javax.mail.MessagingException: Could not connect to SMTP Host: smtp.gmail.com, port: 25;
  nested exception is:
    Java.net.ConnectException: Connection timed out
    at com.Sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.Java:1282)
    at com.Sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.Java:370)
    at javax.mail.Service.connect(Service.Java:297)
    at javax.mail.Service.connect(Service.Java:156)
    at javax.mail.Service.connect(Service.Java:105)
    at javax.mail.Transport.send0(Transport.Java:168)
    at javax.mail.Transport.send(Transport.Java:98)
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.Java:97)
    at RegistrationServlet.doGet(RegistrationServlet.Java:98)
    at RegistrationServlet.doPost(RegistrationServlet.Java:125)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:722)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:304)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:224)
    at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:175)
    at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:472)
    at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:164)
    at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:100)
    at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:118)
    at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:405)
    at org.Apache.coyote.ajp.AjpProcessor.process(AjpProcessor.Java:196)
    at org.Apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.Java:515)
    at org.Apache.Tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.Java:300)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.Java:886)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:908)
    at Java.lang.Thread.run(Thread.Java:662)
Caused by: Java.net.ConnectException: Connection timed out
    at Java.net.PlainSocketImpl.socketConnect(Native Method)
    at Java.net.PlainSocketImpl.doConnect(PlainSocketImpl.Java:351)
    at Java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.Java:213)
    at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:200)
    at Java.net.SocksSocketImpl.connect(SocksSocketImpl.Java:366)
    at Java.net.Socket.connect(Socket.Java:529)
    at Java.net.Socket.connect(Socket.Java:478)
    at com.Sun.mail.util.SocketFetcher.createSocket(SocketFetcher.Java:232)
    at com.Sun.mail.util.SocketFetcher.getSocket(SocketFetcher.Java:189)
    at com.Sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.Java:1250)
15
Lokesh Bachani

Cela ressemble à un problème de pare-feu (les pare-feu suppriment silencieusement les paquets qui ne sont pas autorisés, c'est pourquoi vous verrez un délai d'expiration de connexion.

Peux-tu essayer

H:\> telnet smtp.gmail.com 25
Connecting To smtp.gmail.com...Could not open connection to the Host, on port 25
: Connect failed

sur votre serveur et voyez si vous obtenez une connexion? (Je suis derrière un pare-feu ici qui n'autorise pas les connexions à gmail)

19
beny23