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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
public class EnvoiMail {
/** Type texte brut */
static public final int TEXT = 0;
/** Type text/html */
static public final int HTML = 1;
/** Adresse "no-reply" par défaut de l'entreprise */
private static final String NOREPLY = "noreply@glbgroup.com";
/** Adresse du serveur SMTP de l'entreprise */
private static final String SMTPHOST = "smtp.wanadoo.fr";
private Properties props;
private Session session;
private MimeMessage mm;
private int mimetype;
/**
* Si on n'appelle pas setFrom, met automatiquement noreply@societe.fr
*/
private boolean noreply = true;
/**
* Créateur d'un mail.
*/
public EnvoiMail() {
//Par défaut en texte brut
mimetype = TEXT;
//Get system properties
props = System.getProperties();
//Setup mail server
props.put("mail.smtp.host", SMTPHOST);
//Get session
session = Session.getDefaultInstance(props, null);
//Define message
mm = new MimeMessage(session);
}
//############################################################
//Ajout d'expéditeur et destinataires
//############################################################
/**
* Ajoute un expéditeur
* @param expediteur
* @throws Exception
*/
public void setFrom(String expediteur) throws Exception {
if (expediteur.trim().length() > 0) {
mm.setFrom(checkMail(expediteur));
noreply = false;
}
}
/**
* Ajoute un destinataire
* @param destinataire
* @throws Exception
*/
public void setTo(String destinataire) throws Exception {
if (destinataire.trim().length() > 0) {
if (plusieursMails(destinataire))
setTo(decoupe(destinataire));
else {
mm.addRecipient(Message.RecipientType.TO, checkMail(destinataire));
}
}
}
/**
* Ajoute plusieurs destinataires
* @param dest
* @throws Exception
*/
public void setTo(String[] dest) throws Exception {
for (int i=0; i<dest.length; i++)
setTo(dest[i]);
}
/**
* Ajoute un destinataire en copie
* @param cc
* @throws Exception
*/
public void setCC(String cc) throws Exception {
if (cc.trim().length() > 0) {
if (plusieursMails(cc))
setCC(decoupe(cc));
else {
mm.addRecipient(Message.RecipientType.CC, checkMail(cc));
}
}
}
/**
* Ajoute plusieurs destinataires en copie
* @param cc
* @throws Exception
*/
public void setCC(String[] cc) throws Exception {
for (int i=0; i < cc.length; i++)
setCC(cc[i]);
}
/**
* Ajoute un destinataire en copie cachée.
* @param bcc
* @throws Exception
*/
public void setCopieCachee(String bcc) throws Exception {
if (bcc.trim().length() > 0) {
if (plusieursMails(bcc))
setCopieCachee(decoupe(bcc));
else {
mm.addRecipient(Message.RecipientType.BCC, checkMail(bcc));
}
}
}
/**
* Ajoute plusieurs destinataires en copie cachée.
* @param bcc
* @throws Exception
*/
public void setCopieCachee(String[] bcc) throws Exception {
for (int i=0; i < bcc.length; i++)
setCopieCachee(bcc[i]);
}
//############################################################
//Sujet et message
//############################################################
/**
* Ajoute le sujet
* @param sujet
* @throws Exception
*/
public void setSujet(String sujet) throws Exception {
mm.setSubject(sujet);
}
/**
* Choisir entre le type texte brut ({@link Mail#TEXT}) ou HTML ({@link Mail#HTML}).
* @param mime
* @throws Exception
*/
public void setMimeType(int mime) throws Exception {
mimetype = mime;
}
/**
* Ajoute le message
* @param message
* @throws Exception
*/
public void setMessage(String message) throws Exception {
if (mimetype == HTML)
mm.setContent(message, "text/html");
else mm.setText(message);
}
//############################################################
//Envoi
//############################################################
/**
* Envoie le mail
* @throws Exception
*/
public void send() throws Exception {
if (noreply)
setFrom(NOREPLY);
Transport.send(mm);
}
/
} |
Partager