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
|
public class SisMail {
public void envoyerEmailText(String SMTP_HOST_NAME,String SMTP_AUTH_USER,String SMTP_AUTH_PWD,
String emailMsgTxt,String emailSubjectTxt,String emailFromAddress,
String[] emailList) throws MessagingException {
boolean debug = false;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.user", "");
props.setProperty("mail.password", "");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
//props.put("mail.smtp.EnableSSL.enable","true");
// props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.setProperty("mail.smtp.socketFactory.fallbac k", "false");
// props.setProperty("mail.smtp.port", "465");
// props.setProperty("mail.smtp.socketFactory.port", "465");
//Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[emailList.length];
for (int i = 0; i < emailList.length; i++) {
addressTo[i] = new InternetAddress(emailList[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(emailSubjectTxt);
msg.setContent(emailMsgTxt, "text/plain");
Transport.send(msg);
// }
}
/*Methode d'envoi de type message HTML*/
public void envoyerEmailHTML(String SMTP_HOST_NAME,String SMTP_AUTH_USER,String SMTP_AUTH_PWD,
String emailMsgTxt,String emailSubjectTxt,String emailFromAddress,
String[] emailList) throws MessagingException {
boolean debug = false;
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "false");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.user", "");
props.setProperty("mail.password", "");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[emailList.length];
for (int i = 0; i < emailList.length; i++) {
addressTo[i] = new InternetAddress(emailList[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(emailSubjectTxt);
msg.setContent(emailMsgTxt, "text/html");
Transport.send(msg);
// }
}
/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "";
String password = "";
return new PasswordAuthentication(username, password);
}
}
public static void main(String[] args) throws MessagingException {
String emailList[]={"aouatif.elkhou@gmail.com"};
new SisMail().envoyerEmailText("smtp.gmail.com", "", "", "hhhhhhhhh", "confirmation","", emailList);
}
} |