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
| package com.onbooje.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
public class EnvoiMail {
/** Logger for this class and subclasses */
//protected static final Log logger = LogFactory.getLog(EnvoiMail.class);
public static final String SMTP_SERVER = "localhost";
private static Properties smtpProp = null;
private static final String smtpPropertiesFile = "smtp.properties";
public static final String propSmtpServer = "smtp_server"; // le no Siret
// onbooje
/**
* @param key
* @param defaultValue
* @return
*/
public static String getSmtpProperty(final String key, final String defaultValue) {
return getSmtpPropertyFile().getProperty(key, defaultValue);
}
private static Properties getSmtpPropertyFile() {
if (smtpProp == null) {
smtpProp = new Properties();
FileInputStream in;
try {
ClassLoader classLoader = OnBoojeUtil.class.getClassLoader();
File file = new File(classLoader.getResource(smtpPropertiesFile).getFile());
in = new FileInputStream(file);
smtpProp.load(in);
}
catch (FileNotFoundException e) {
Logger.getLogger(EnvoiMail.class).error(e);
}
catch (IOException e) {
Logger.getLogger(EnvoiMail.class).error(e);
}
}
return smtpProp;
}
public static String getSmtpServer() {
return getSmtpProperty(propSmtpServer, "localhost");
}
/** Expediteur du message. */
protected String message_from;
/** Destinataire du message. */
protected String message_dest;
/* Objet du message. */
protected String message_objet;
/** Destinataire du message en copie (CC). */
protected String message_cc;
/** Texte du message. */
protected String message_corps;
/** Objet session de JavaMail. */
protected Session session;
/** Objet message de JavaMail. */
protected Message mesg;
/** Adresse du serveur SMTP */
protected String srv;
public EnvoiMail(String from, String dest, String cc, String objet, String msg) {
Logger.getLogger(EnvoiMail.class).info(">> EnvoiMail.EnvoiMail()");
this.message_from = from;
this.message_dest = dest;
this.message_cc = cc;
this.message_objet = objet;
this.message_corps = msg;
this.srv = getSmtpServer();
}
public void envoyerMail() throws MessagingException {
// Nous devons passer les informations au serveur de messagerie sous
// forme de proprietes car JavaMail en comporte beaucoup...
Properties props = new Properties();
// Votre reseau doit donner au serveur SMTP local le nom
// "nom_du_serveur_smtp"
props.put("mail.smtp.host", srv);
// Creer lobjet Session.
session = Session.getDefaultInstance(props, null);
session.setDebug(true); // activer le mode verbeux !
try {
// Creer un message.
mesg = new MimeMessage(session);
// Adresse From - Indiquer la provenance du message
mesg.setFrom(new InternetAddress(message_from));
// Adresse TO.
InternetAddress toAddress = new InternetAddress(message_dest);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
// Adresse CC.
if (OnBoojeUtil.isNotBlank(message_cc)) {
InternetAddress ccAddress = new InternetAddress(message_cc);
mesg.addRecipient(Message.RecipientType.CC, ccAddress);
}
// Objet.
mesg.setSubject(message_objet);
// Now the message body.
Multipart mp = new MimeMultipart();
BodyPart textPart = new MimeBodyPart();
textPart.setText(""); // sets type to "text/plain"
BodyPart pixPart = new MimeBodyPart();
pixPart.setContent(message_corps, "text/html");
// Collect the Parts into the MultiPart
mp.addBodyPart(textPart);
mp.addBodyPart(pixPart);
// Put the MultiPart into the Message
mesg.setContent(mp);
// Corps du message.
// mesg.setText(message_corps);
// Enfin, envoyer le message !
try {
Transport.send(mesg);
}
catch (RuntimeException e) {
e.printStackTrace();
}
Logger.getLogger(EnvoiMail.class).info("Message email envoye - from:" + message_from + ", dest:" + message_dest + ", objet:"
+ message_objet);
}
catch (MessagingException ex) {
Logger.getLogger(EnvoiMail.class).error("Erreur Envoi de mails :" + ex.getMessage() + ex.getCause() + ex.getStackTrace());
throw ex;
}
}
} |
Partager