Bonjour

J'ai créé une application J2EE contenant une partie "contactez-nous" qui permet au visiteur de m'envoyer un mail. Je dois normalement réceptionner ce mail dans la boite de messagerie de mon hébergeur. Le problème est que quand je clique sur "envoyer", je reçois le message d'erreur suivant:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect
Voici mes codes:

La class EmailUtility

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 
package utility;
 
 
import java.io.UnsupportedEncodingException;
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 EmailUtility {
    public static void sendEmail( String toAddress,
			String subject, String message) throws AddressException,
			MessagingException,
			UnsupportedEncodingException {
 
		// sets SMTP server properties
		Properties properties = new Properties();
 
                properties.setProperty("mail.from", toAddress);
 
		Session session = Session.getInstance(properties);
 
		// creates a new e-mail message
		MimeMessage msg = new MimeMessage(session);
 
 
		InternetAddress[] toAddresses = { new InternetAddress("xxxxx@monhebergeur.com") };
		msg.setRecipients(Message.RecipientType.TO, toAddresses);
		msg.setSubject(subject);
		msg.setSentDate(new Date());
		msg.setText(message,"utf-8", "html");
 
		// sends the e-mail
		Transport.send(msg);
 
	}
}
La Servlet:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;
 
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.Message;
import forms.ContactForm;
import utility.EmailUtility;
import static servlets.Contact.ATT_FORM;
import static servlets.Contact.ATT_MESSAGE;
import static servlets.Contact.ATT_MSG;
import static servlets.Contact.VUE_ECHEC;
import static servlets.Contact.VUE_SUCCES;
 
 
public class Contact extends HttpServlet {
 
    public static final String ATT_MSG = "message";
 
    public static final String ATT_FORM   = "form";
    public static final String ATT_MESSAGE   = "msg";
    public static final String VUE_SUCCES   = "/envoireussi.jsp";
    public static final String VUE_ECHEC   = "/contact.jsp";
 
 
 
    public void init() throws ServletException {
 
 
    }
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        /* À la réception d'une requête GET, simple affichage du formulaire */
        this.getServletContext().getRequestDispatcher( VUE_SUCCES ).forward( request, response );
    }
    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
 
        /* Préparation de l'objet formulaire */
        ContactForm form = new ContactForm(  );
        /* Traitement de la requête et récupération du bean en résultant */
        Message message = form.creerMessage( request );
        /* Ajout du bean et de l'objet métier à l'objet requête */
        request.setAttribute( ATT_MSG, message );
        request.setAttribute( ATT_FORM, form );
 
        String msgRetour = "";
 
            Long id = message.getId();
            String entrep = message.getEntrep();
            String nom = message.getNom();
            String prenom = message.getPrenom();
            String email = message.getEmail();
            String sujet = message.getSujet();
            String msg = message.getMessage();
 
 
        /* Si aucune erreur */
        if ( form.getErreurs().isEmpty() ) {
 
            StringBuilder builder = new StringBuilder("<html><body>");
 
            builder.append("Nom:").append(nom).append("<br><br> Prénom:").append(prenom).append("<br><br>Entreprise: ").append(entrep).append(" <br><br>Message: ").append(msg);
 
            builder.append("</body></html>");
 
             String emailBody = builder.toString(); // le bouton est moche, suis d'accord :D
 
            try {
			EmailUtility.sendEmail(  email, sujet, emailBody);
			msgRetour = "<span id="+"resultat_vert"+">Message envoyé avec succès !</span>";
                        request.setAttribute(ATT_MESSAGE, msgRetour);
                        this.getServletContext().getRequestDispatcher(VUE_SUCCES).forward(request, response);
		} 
            catch (Exception ex) {
                System.out.println(ex);
	msgRetour = "<div class=\"echec2\">Message non envoyé! Adresse Email non valide ou problème de connexion. Veuillez réessayer</div>" ;
	request.setAttribute(ATT_MESSAGE, msgRetour);
        this.getServletContext().getRequestDispatcher(VUE_ECHEC).forward(request, response);
            }
 
 
            finally {
 
 
		}
 
 
 
        } else {
            /* Sinon, ré-affichage du formulaire de création avec les erreurs */
            this.getServletContext().getRequestDispatcher( VUE_ECHEC ).forward( request, response );
        }
    }
}
Merci