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
|
public void envoyerEmail() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME); //Ici j'ai bien la valeur attendue soit smtp.live.com soit smtp.toto.com
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.EnableSSL.enable","true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
System.out.println("=>mailSession.getProperty : "+mailSession.getProperty("mail.smtp.host")); //Ici je n'ai pas la valeur attendue, c'est toujours l'ancienne si je ne relance pas l'application
// uncomment for debugging infos to stdout
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
Multipart multipart = new MimeMultipart("alternative");
BodyPart part1 = new MimeBodyPart();
part1.setText("Hello, juste un petit test");
BodyPart part2 = new MimeBodyPart();
part2.setContent("Hello, juste un petit test", "text/html");
multipart.addBodyPart(part1);
multipart.addBodyPart(part2);
message.setContent(multipart);
message.setFrom(new InternetAddress(SMTP_AUTH_USER));
message.setSubject("Hello, juste un petit test");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(EMAIL_DESTINATAIRE));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} |
Partager