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
|
//Fonction
public void envoyer_email(String fromEmail,String username, String password, String toEmail,String subject,String message){
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Message mailMessage= new MimeMessage(mailSession);
mailMessage.setFrom(new InternetAddress(fromEmail));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
mailMessage.setContent(message,"text/html");
mailMessage.setSubject(subject);
Transport trns = mailSession.getTransport("smtp");
trns.connect("smtp.gmail.com", fromEmail, password);
trns.sendMessage(mailMessage, mailMessage.getAllRecipients());
} catch (MessagingException ex) {
Logger.getLogger(NewSessionBean.class.getName()).log(Level.SEVERE, null, ex);
}
} |
Partager