Bonjour,
J'essaie d'envoyer un mail via un script en utilisant javax.mail mais je ne reçois jamais le mail.
Aucune exception n'est levée, l'authentification à l'air de bien se passer.
En production, mes autres sites sur le même serveur utilisant la même classe envoient bien les mails eux.
Je vous donne mon code :
methode dans mon ManagedBean :
La méthode qui envoie le mail dans la classe parente :
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 public void actionValidate() { try { validate(); sendValidateMail(); createCookie("cookie_status", "1"); setHTTP(); facesContext.getExternalContext().redirect(response.encodeRedirectURL("/")); } catch (SQLException ex) { Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex); sqlException(ex); } catch (NamingException ex) { Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex); namingException(ex); } catch (MyException ex) { Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex); myException(ex); } catch (MessagingException ex) { Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex); messagingException(ex); } catch (UnsupportedEncodingException ex) { Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex); unsupportedEncodingException(ex); } catch (IOException ex) { Logger.getLogger(Validate.class.getName()).log(Level.SEVERE, null, ex); ioException(ex); } finally { closeConnection(); } // if (test == 2) { messageError("Erreur : ", errorMsg); } }
Ma classe SendMail :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 public void sendValidateMail() throws MessagingException, UnsupportedEncodingException { SendMail sendMail = new SendMail(); ValidateMail mail = new ValidateMail(url, categories.categories); sendMail.setDestinat(user.email); sendMail.setSubject(Datas.SITENAME + " : Votre site est accepté."); sendMail.setContent(mail.getContent()); sendMail.sendMessage(); }
dans mon managedBean le script s'execute après la tentative d'envoi du mail (je fais une redirection après et elle a bien lieu), j'en conclue qu'il n'y a aucun problème d'authenficication ou de port...
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 package classes; import java.io.Serializable; import java.io.UnsupportedEncodingException; 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.InternetAddress; import javax.mail.internet.MimeMessage; /** * * @author pj */ public class SendMail extends SuperClass implements Serializable { Session sess = null; private String destinat = ""; private String subject = ""; private String content = ""; public SendMail() { super(); Properties properties = new Properties(); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.host", Datas.SMTPHOST); properties.put("mail.smtp.port", Datas.SMTPPORT); //properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); //properties.put("mail.debug", "true"); //sess = Session.getDefaultInstance(properties, null); sess = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(Datas.NOREPLY, Datas.PWDNOREPLY); } }); } public void sendMessage() throws MessagingException, UnsupportedEncodingException { Transport transport = sess.getTransport(); Message mess = new MimeMessage(sess); mess.setFrom(new InternetAddress(Datas.NOREPLY, "Télétravail.coM")); mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinat)); mess.setSubject(subject); mess.setContent(content, "text/html; charset=UTF-8"); mess.setHeader("Content-Type", "text/html; charset=\"UTF-8\""); transport.connect(); transport.sendMessage(mess, mess.getRecipients(Message.RecipientType.TO)); transport.close(); } public void sendMessage(String FromEmail, String FromName) throws MessagingException, UnsupportedEncodingException { Transport transport = sess.getTransport(); Message mess = new MimeMessage(sess); mess.setFrom(new InternetAddress(FromEmail, FromName)); mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinat)); mess.setSubject(subject); mess.setContent(content, "text/html; charset=UTF-8"); mess.setHeader("Content-Type", "text/html; charset=\"UTF-8\""); transport.connect(); transport.sendMessage(mess, mess.getRecipients(Message.RecipientType.TO)); transport.close(); } /** * @param destinat the destinat to set */ public void setDestinat(String destinat) { this.destinat = destinat; } /** * @param subject the subject to set */ public void setSubject(String subject) { this.subject = subject; } /** * @param content the content to set */ public void setContent(String content) { this.content = content; } }
Peut être que ça vous parle...
Merci.
Partager