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
|
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MyMail {
private String smtpHostName; // serveur smtp .
private String smtpAuthUser;
private String smtpAuthpwd;
private String emailSubjectTxt ; //l'objectif .
private String emailMsgTxt ;
private String emailFromAddress; //l'expediteur .
private List<String> emailList=null ;
public MyMail(String server,String user,String password,String objet,String msg ,String exp,String[] dest){
smtpHostName=server.trim();
smtpAuthUser=user.trim();
smtpAuthpwd=password;
objet.trim();
if(objet==null || objet.equals(""))
objet="pas d'objet";
emailSubjectTxt=objet;
emailMsgTxt=msg;
emailFromAddress=exp.trim();
emailList =new ArrayList();
if(dest!=null && dest.length > 0)
for(int i=0 ;i<dest.length;i++){
emailList.add(dest[i]);
}
}
public void addEmail(String email){
this.emailList.add(email);
}
public boolean sendMail() throws MessagingException {
boolean resultat =false ;
if(smtpHostName==null ||smtpHostName.equals("")
||smtpAuthUser==null ||smtpAuthUser.equals("")
||smtpAuthpwd==null || smtpAuthpwd.equals("")
||emailMsgTxt ==null || emailMsgTxt.equals("")
|| emailList.size()==0 )
return false ;
boolean debug = true;
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//Set the host smtp address
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", smtpHostName);
props.put("mail.smtp.auth", "true");
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.size()];
for (int i = 0; i < emailList.size(); i++) {
addressTo[i] = new InternetAddress(emailList.get(i));
}
resultat=true;
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(emailSubjectTxt);
msg.setContent(emailMsgTxt, "text/plain");
Transport.send(msg);
return resultat ;
}
/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication(){
String username = smtpAuthUser;
String password = smtpAuthpwd;
return new PasswordAuthentication(username, password);
}
}
} |
Partager