Bonjour,
J'utilise javamail pour envoyer des mails (et oui!!). Je me suis fais une petite classe pour gerer ca:
Bon c'est du code qui n'a rien de bien sorcier.
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 public class MailSender { public static final String DEFAULT_SUBJECT = "Aucun Sujet"; public static final String HEADER_XMAILER = "MyApp Integrated Mailer"; private static Session getSession(String _smtpServer) { final Properties prop = new Properties(); prop.setProperty("mail.smtp.host", _smtpServer); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.mime.charset", MimeUtility.mimeCharset("UTF-8")); prop.setProperty("mail.debug", "true"); Session session = Session.getDefaultInstance(prop); return session; } private static MimeMessage createMessage(final Session _session, final String _from, final String _replyTos, final String _tos, final String _ccs, final String _bccs, final String _subject) throws MessagingException { if (Utilities.isEmptyString(_from)) { throw new IllegalArgumentException("the \"from\" field cannot be empty"); } if (Utilities.isEmptyString(_tos)) { throw new IllegalArgumentException("the \"to\" field cannot be empty"); } final MimeMessage message = new MimeMessage(_session); message.setFrom(new InternetAddress(_from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(_tos)); if (!Utilities.isEmptyString(_replyTos)) { message.setReplyTo(InternetAddress.parse(_replyTos)); } else { message.setReplyTo(InternetAddress.parse(_from)); } if (!Utilities.isEmptyString(_ccs)) { message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(_ccs)); } if (!Utilities.isEmptyString(_bccs)) { message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(_bccs)); } if (Utilities.isEmptyString(_subject)) { message.setSubject(MailSender.DEFAULT_SUBJECT, "UTF-8"); } else { message.setSubject(_subject, "UTF-8"); } message.setContentLanguage(new String[] {"fr"}); message.setHeader("X-Mailer", MailSender.HEADER_XMAILER); message.setSentDate(new Date()); return message; } private static void sendMessage(Message _message) throws MessagingException { _message.saveChanges(); Transport.send(_message); } public final static void sendMail(final String _smtpServer, final String _from, final String _replyTos, final String _tos, final String _ccs, final String _bccs, final String _subject, final String _text) { try { final MimeMessage message = MailSender.createMessage(getSession(_smtpServer), _from, _replyTos, _tos, _ccs, _bccs, _subject); if (Utilities.isEmptyString(_text)) { message.setText("", "UTF-8"); } else { message.setText(_text, "UTF-8"); } message.setHeader("Content-Type", "text/plain; charset=UTF-8"); sendMessage(message); } catch (final MessagingException excp) { throw new RuntimeException("an error occured while sending message", excp); } } }
J'utilise cette classe comme ceci:
La encore rien de sorcier.
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 final String sbj = "Hello JavaMail"; final String msg = "Welcome to JavaMail"; try { System.out.println("sending plain mail"); sendMail( "smtp.orange.fr", "noreply@compagny.com", (String)null, "mon_email@provider.com", (String)null, (String)null, sbj, msg); System.out.println("plain mail sended"); } catch (Exception excp) { excp.printStackTrace(); }
Mais ça se corse a ce niveau la: Lorsque j'utilise ce code dans une methode main bidon (ie dans MailSender par exemple) ca marche. Lorsque j'utilise ce code dans ma webapp je recois un mail foireux (pas de sujet, pas d'expediteur, ...)
Voici ce que me dis javamail quand ca marche (debug de javamail):
Voici ce que me dis javamail quand ca marche pas (debug de javamail):
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 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "smtp.orange.fr", port 25, isSSL false 220 mwinf2701.orange.fr ESMTP ABO ************************** DEBUG SMTP: connected to host "smtp.orange.fr", port: 25 EHLO acs-vmware 250-mwinf2701.orange.fr 250-PIPELINING 250-SIZE 10485760 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "10485760" DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN" DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: use8bit false MAIL FROM:<noreply@compagny.com> 250 Ok RCPT TO:<mon_email@provider.com> 250 Ok DEBUG SMTP: Verified Addresses DEBUG SMTP: mon_email@provider.com DATA 354 End data with <CR><LF>.<CR><LF> Date: Tue, 20 May 2008 11:56:00 +0200 (CEST) From: noreply@compagny.com Reply-To: noreply@compagny.com To: mon_email@provider.com Message-ID: <13755908.1.1211277360245.JavaMail.acs@acs-vmware> Subject: Hello JavaMail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Language: fr X-Mailer: MyApp Integrated Mailer Welcome to JavaMail . 250 Ok: queued as E77AF1C001E5 QUIT 221 Bye
Dans les deux cas je recois bien un mail, mais dans le cas webapp (le deuxieme) mon message est foireux (je n'ai pas de sujet, l'expediteur s'appel "undisclosed-recipients: ;"...)
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 Loading javamail.default.providers from jar:file:/D:/cerbere2/workspace/cerbere2-webapp/target/webapp/WEB-INF/lib/mail-1.4.1.jar!/META-INF/javamail.default.providers DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@4e9bea; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "smtp.orange.fr", port 25, isSSL false 220 mwinf2826.orange.fr ESMTP ABO ************************** DEBUG SMTP: connected to host "smtp.orange.fr", port: 25 EHLO acs-vmware 250-mwinf2826.orange.fr 250-PIPELINING 250-SIZE 10485760 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "10485760" DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN" DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: use8bit false MAIL FROM:<noreply@compagny.com> 250 Ok RCPT TO:<mon_email@provider.com> 250 Ok DEBUG SMTP: Verified Addresses DEBUG SMTP: mon_email@provider.com DATA 354 End data with <CR><LF>.<CR><LF> Welcome to JavaMail . 250 Ok: queued as 8692B7000098 QUIT 221 Bye
Au cas ou, voici la source des messages reçus (ctrl+u sous thunderbird)
quand ca marcheet quand ca marche pas
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 From - Tue May 20 12:22:05 2008 X-Account-Key: account2 X-UIDL: UID395-1199695213 X-Mozilla-Status: 0001 X-Mozilla-Status2: 00000000 X-Mozilla-Keys: Return-Path: <noreply@compagny.com> Delivered-To: 3-mon_email@provider.com Received: (qmail 29968 invoked from network); 20 May 2008 12:14:47 +0200 Received: from smtp23.orange.fr (80.12.242.97) by diginext.dns26.com with SMTP; 20 May 2008 12:14:47 +0200 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2313.orange.fr (SMTP Server) with ESMTP id 0CB8570000F3 for <mon_email@provider.com>; Tue, 20 May 2008 12:14:17 +0200 (CEST) Received: from acs-vmware (AMarseille-158-1-52-19.w90-28.abo.wanadoo.fr [90.28.91.19]) by mwinf2313.orange.fr (SMTP Server) with ESMTP id DADC770000DF for <mon_email@provider.com>; Tue, 20 May 2008 12:14:16 +0200 (CEST) X-ME-UUID: 20080520101416896.DADC770000DF@mwinf2313.orange.fr Date: Tue, 20 May 2008 12:14:16 +0200 (CEST) From: noreply@compagny.com Reply-To: noreply@compagny.com To: mon_email@provider.com Message-ID: <13755908.1.1211278456216.JavaMail.acs@acs-vmware> Subject: Hello JavaMail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Language: fr X-Mailer: MyApp Integrated Mailer X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on diginext.dns26.com X-Spam-Level: *** X-Spam-Status: No, score=3.6 required=5.0 tests=LIMSI_20,MR_NOT_ATTRIBUTED_IP, NO_REAL_NAME,SARE_FROM_SPAM_WORD4,SUB_HELLO autolearn=no version=3.0.4 Welcome to JavaMailJ'ai cherché partout, essayé de créer un fichier javamail.provider, fais plein de test diffèrent... J'ai toujours le même problème.
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 From - Tue May 20 12:13:55 2008 X-Account-Key: account2 X-UIDL: UID394-1199695213 X-Mozilla-Status: 0001 X-Mozilla-Status2: 00000000 X-Mozilla-Keys: Return-Path: <noreply@compagny.com> Delivered-To: 3-mon_email@provider.com Received: (qmail 29838 invoked from network); 20 May 2008 12:13:22 +0200 Received: from smtp27.orange.fr (80.12.242.94) by diginext.dns26.com with SMTP; 20 May 2008 12:13:22 +0200 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2701.orange.fr (SMTP Server) with ESMTP id 847B91C001E5 for <mon_email@provider.com>; Tue, 20 May 2008 12:12:51 +0200 (CEST) Received: from acs-vmware (AMarseille-158-1-52-19.w90-28.abo.wanadoo.fr [90.28.91.19]) by mwinf2701.orange.fr (SMTP Server) with ESMTP id 5A90A1C001D4 for <mon_email@provider.com>; Tue, 20 May 2008 12:12:51 +0200 (CEST) X-ME-UUID: 20080520101251371.5A90A1C001D4@mwinf2701.orange.fr Message-Id: <20080520101251.5A90A1C001D4@mwinf2701.orange.fr> Date: Tue, 20 May 2008 12:12:51 +0200 (CEST) From: noreply@compagny.com To: undisclosed-recipients: ; X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on diginext.dns26.com X-Spam-Level: *** X-Spam-Status: No, score=3.2 required=5.0 tests=MISSING_SUBJECT, MR_NOT_ATTRIBUTED_IP,NO_REAL_NAME,SARE_FROM_SPAM_WORD4,UNDISC_RECIPS autolearn=no version=3.0.4 Welcome to JavaMail
Au cas ou, la webapp est deployé sur un tomcat 5.5.23, librairies JSF 1.2_09-BETA1, facelets 1.1.14, richfaces 3.2.1, spring 2.0.8, hibernate 3.2.6, Quartz 1.5.2.
PLZ help me!! Je prends aussi les remarques (même débile) parce que la je n'ai plus d'idée...
Je pense que cela viens du fait que javamail n'envoie pas la même chose si je suis dans une webapp ou pas (voir le résultats de debug, surtout la partie DATA). Mais ce que je veux savoir, c'est POURQUOI il n'envoie pas les même choses suivant les cas...
Partager