Bon j'en suis arrivée à ça, j'ai deux erreurs soulignées, j'ai suivi "je pense" ce que tu m'as dit. J'ai pas l'habitude de bosser avec les mails c'est un truc de fou lol.
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 101 102 103 104 105 106 107 108 109 110 111 112
| package fr.isa.metier;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class JavaMailHTML
{
public static void main(String[] argv)throws Exception
{
final String PATH_TO_FILE = "C:/Users/xxx/Pictures/";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.orange.fr");
prop.put("mail.from", "xxx@bbox.fr");
// Note: Aucune authentification n'est définie
Session session = Session.getInstance(prop, null);
//Ajout pièce jointe
File file = new File(PATH_TO_FILE + "orang-outan.jpg");
FileDataSource datasource1 = new FileDataSource(file);
DataHandler handler1 = new DataHandler(datasource1);
//creation du mimeMultiPart afin de contenir les bodyParts
MimeMultipart mimeMultiPart = new MimeMultipart("related");
//creation du BodyPart pour l'image pièce jointe
MimeBodyPart orangOutanBodyPart = new MimeBodyPart();
//creation d'un bodyPart text html
MimeBodyPart textBodyPart = new MimeBodyPart();
try {
orangOutanBodyPart.setDataHandler(handler1);
orangOutanBodyPart.setFileName(datasource1.getName());
orangOutanBodyPart.setContentID("oran");
} catch (MessagingException e) {
e.printStackTrace();
}
try {
//ajout du bodypart texte dans le multiPart
mimeMultiPart.addBodyPart(textBodyPart);
mimeMultiPart.addBodyPart(orangOutanBodyPart);
MimeMessage msg = new MimeMessage(session);
// msg.setContent(mimeMultiPart);
msg.setContent(mimeMultiPart);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"xxx@bbox.fr");
// Sujet
msg.setSubject("Bienvenu (objet du mail)");
// Date d'envoi
msg.setSentDate(new java.util.Date());
// Appel de la fonction qui construit le contenu HTML
buildMessage("Contenu du fichier", textBodyPart);
Transport.send(msg);
System.out.println("Email envoyé avec succès.");
} catch (MessagingException ex)
{
System.out.println("Erreur d'envoi, cause: " + ex);
}
}
public static void buildMessage(String contenu, MimeBodyPart textBodyPart) throws MessagingException, IOException {
String sujet = textBodyPart.getSubject();
StringBuilder sb = new StringBuilder();
sb.append("<HTML>");
sb.append("<HEAD>");
sb.append("<TITLE>");
sb.append(sujet + "");
sb.append("</TITLE>");
sb.append("</HEAD>");
sb.append("<BODY>");
sb.append("<H1>" + sujet + "</H1>" + "");
sb.append(contenu);
sb.append("");
sb.append("<H1>Orang-outan</H1><img src=\"cid:oran\"/><br/>");
sb.append("</BODY>");
sb.append("</HTML>");
textBodyPart.setText( sb.toString() , "UTF-8", "html");
// msg.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString(), "text/html")));
}
} |
Dans le
textBodyPart.setText( sb.toString() , "UTF-8", "html");
j'ai une erreur car il attend soit un paramétre
textBodyPart.setText(String)
, ou
textBodyPart.setText(String, String)
.
Et le String sujet =
textBodyPart.getSubject();
le getSubject() connait pas...
J'y suis presque je pense ^^ :'(
Partager