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 visiteur doit remplir les champs nom et adresse mail.

Le problème est que quand un visiteur m'envoie un message, c'est mon adresse mail (contact@monsite.com) qui apparait au niveau du nom de l'expéditeur. C'est normal parce que c'est contact@monsite.com que j'ai défini dans le fichier web.xml.

Donc comment faire en sorte que l'adresse mail de l'expéditeur apparaisse dans la colonne "nom de l'expéditeur" au niveau de ma boite de messagerie?

Voici mon fichier web.xml:

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
...
<context-param>
        <param-name>host</param-name>
        <param-value>monsiteweb.com</param-value>
    </context-param>
    <context-param>
        <param-name>port</param-name>
        <param-value>587</param-value>
    </context-param>
    <context-param>
        <param-name>user</param-name>
        <param-value>contact@monsite.com</param-value>
    </context-param>
    <context-param>
        <param-name>pass</param-name>
        <param-value>xxxxxxx</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
...
Voici le code d'envoi:

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
 
 
 
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 host, String port,final String userName, final String password, String toAddress,
			String subject, String message) throws AddressException,
			MessagingException,
			UnsupportedEncodingException {
 
		// sets SMTP server properties
		Properties properties = new Properties();
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");
 
		// creates a new session with an authenticator
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		};
 
		Session session = Session.getInstance(properties, auth);
 
		// creates a new e-mail message
		MimeMessage msg = new MimeMessage(session);
 
		msg.setFrom(new InternetAddress(userName,"Mon site Web"));
		InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
		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);
 
	}
}
Merci