Bonjour tout le monde,
j'ai un soucis concernant l'envoi de mail sous format HTML,j'ai commencé par essayer l'API :FileUtils pour lire le fichier html et le convertir en string,mais le problème est comment récupérer le contenu tel qu'il est et l'envoyer dans content en mail (en UTF8 pour éviter le problème des accents).
voila ma fonction d'envoi de mail :
Cette fonction prends en paramètre 2 attributs:Code:
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 public static boolean envoiMails(String mails,String nom_prenom) { String from = "@mail1"; StringWriter writer = new StringWriter(); try { StringBuilder stringBuilder = new StringBuilder(); //InputStreamReader inputStreamReader = new InputStreamReader(BatchEmailingRelances.class.getResourceAsStream("./relance.htm")); InputStreamReader inputStreamReader2 = new InputStreamReader(BatchEmailingRelances.class.getResourceAsStream("./mail1FR.txt")); BufferedReader bufferedReader = new BufferedReader(new FileReader("./relance.htm")); BufferedReader bufferedReader2 = new BufferedReader(inputStreamReader2); String subject = bufferedReader2.readLine(); String str = ""; // String content = ""; //String content = Files.toString(new File("relance.htm", Charsets.UTF_8)); File myFile = new File("C:\\Users\\belkaika\\Documents\\NetBeansProjects\\BatchEmailingRelances\\src\\batchemailingrelances\\relance.htm"); File myfile = null; String content = FileUtils.readFileToString(myfile, "UTF-8"); // IOUtils.copy(new FileInputStream(new File("./relance.htm")), writer); while ((str=bufferedReader.readLine()) != null) { stringBuilder.append(str); //stringBuilder.replace(exit, exit, str); content = stringBuilder.toString(); } logger.log(Level.INFO, "affichage mail ..............", str); String host = "host"; Properties props = new Properties(); props.setProperty("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put ("mail.smtp.ssl.trust", host); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); props.put("mail.smtp.socketFactory.port", "587"); props.put("mail.smtp.socketFactory.fallback", "true"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("login", "*****"); } }); String to; try { to = mails; // Création d'un message Message message = new MimeMessage(session); // Modification de l'expéditeur message.setFrom(new InternetAddress(from)); // Modification du destinataire InternetAddress[] address = {new InternetAddress(to)}; message.setRecipients(Message.RecipientType.TO, address); message.setSubject(subject); //message.setContent(writer.toString(), "text/html"); message.setContent(content.replace("{Nom_prenom}", nom_prenom), "text/plain;charset=utf-8"); // message.setContent(content, "text/plain;charset=utf-8"); affiche("Contenu du message à envoyer:" + str+".......................ce message à été envoyé à"+to); // Envoi du message affiche("Essai d'envoi mail a :....." + to); //envoi du mail Transport.send(message); logger.log(Level.INFO, " Message envoye a :", to); } catch (MessagingException mex) { mex.getMessage(); mex.printStackTrace(); logger.log(Level.WARNING, " Problème envoi du MESSAGE de relance"); return false; } } catch (IOException ex) { // logger.log(Level.WARNING, "Probleme d'acces a:", mailFile + " ou " + destFile); ex.getMessage(); return false; } return true; }
- String mails :l'adresse mail du destinataire
- String nom_prenom: nom & prenom du client récupéré de la BD pour qu'il soit affiché au début du mail (au niveau du mail en html : <p>Madame / Monsieur {Nom_prenom}</p>)
le mail est sous le format suivant:
Merci de m'aviser sur ce volet SVP.Code:
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 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- saved from url=(0014)about:internet --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>relance.png</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style type="text/css"> td img { display: block; } .parag p { margin-left: 25px; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 12px; } </style> </head> <body bgcolor="#ffffff"> <table border="0" cellpadding="0" cellspacing="0" width="845" align="center"> <tr> <td><img name="relance_r1_c1" src="images/relance_r1_c1.png" width="845" height="129" border="0" id="relance_r1_c1" alt=""/></td> </tr> <tr> <td><img name="relance_r2_c1" src="images/relance_r2_c1.png" width="845" height="33" border="0" id="relance_r2_c1" alt=""/></td> </tr> <tr> <td width="845" height="111" class="parag"> <p>Madame / Monsieur {Nom_prenom}</p> <p> Nous vous remercions d'avoir entamé votre ouverture de compte en ligne . </p> <p>Pour finaliser votre demande, nous vous invitons à vous connecter avec votre identifiant et votre mot de passe en cliquant sur le lien ci-dessous</p> </td> </tr> <tr> <td><img name="relance_r4_c1" src="images/relance_r4_c1.png" width="845" height="18" border="0" id="relance_r4_c1" alt=""/></td> </tr> <tr> <td> <table align="left" border="0" cellpadding="0" cellspacing="0" width="845"> <tr> <td><img name="relance_r5_c1" src="images/relance_r5_c1.png" width="107" height="69" border="0" id="relance_r5_c1" alt=""/></td> <td><img name="relance_r5_c2" src="images/relance_r5_c2.png" width="615" height="69" border="0" id="relance_r5_c2" alt=""/></td> <td><img name="relance_r5_c3" src="images/relance_r5_c3.png" width="123" height="69" border="0" id="relance_r5_c3" alt=""/></td> </tr> </table> </td> </tr> <tr> <td><img name="relance_r6_c1" src="images/relance_r6_c1.png" width="845" height="27" border="0" id="relance_r6_c1" alt=""/></td> </tr> <tr> <td width="845" height="41" class="parag"> <p> Pour toutes vos question nhésitez pas à contacter notre équipe Agence Directe au ..., 7j/7, de 08h à 22h ou par e-mail à l'adresse <a href="mailto:">url site</a> </p> </td> </tr> <tr> <td><img name="relance_r8_c1" src="images/relance_r8_c1.png" width="845" height="74" border="0" id="relance_r8_c1" alt=""/></td> </tr> <tr> <td><img name="relance_r9_c1" src="images/relance_r9_c1.png" width="845" height="105" border="0" id="relance_r9_c1" alt=""/></td> </tr> </table> </body> </html>
Bonne journée à tous les membres.