com.evasion.plugin.common.MailManager Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.plugin.common;
import com.evasion.ejb.local.MailManagerLocal;
import com.evasion.ejb.local.TemplateManagerLocal;
import com.evasion.ejb.remote.MailManagerRemote;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author sebastien.glon
*/
@Stateless
@Local(value = MailManagerLocal.class)
@Remote(value = MailManagerRemote.class)
public class MailManager implements MailManagerLocal, MailManagerRemote {
/** LOGGER */
private static final Logger LOGGER = LoggerFactory.getLogger(
MailManager.class);
@Resource(name = "mail/Evasion")
private Session mailSession;
@EJB
private TemplateManagerLocal templateEJB;
protected MailManager(Session mailSession) {
this.mailSession = mailSession;
}
public MailManager() {
super();
}
/**
* Send an email message to a list of addresses
*
* @param config
* The configuration to use (specified in the email properties)
* @param address
* The receivers of the email
* @throws Exception
*/
@Override
public void sendMail(String subject, String contenu, String addrs) {
if (mailSession == null) {
LOGGER.error("No JNDI 'mail/Evasion' JavaMail found. ");
} else {
if (LOGGER.isDebugEnabled()) {
mailSession.setDebug(true);
}
// Validation de l'adresse mail.
Address mailAddress = null;
try {
mailAddress = new InternetAddress(addrs);
} catch (AddressException e) {
LOGGER.error("Invalid mail address: " + e.getMessage(), e);
return;
}
// Construction du message
//MessageContext context = mimePartDatasource.getMessageContext();
//Message message = context.getMessage();
Message message = new MimeMessage(mailSession);
try {
message.setContent(contenu, "text/plain");
message.setSubject(subject);
} catch (MessagingException e) {
LOGGER.error("Cannot set message content: ", e);
return;
}
// Création de la couche de transport
Transport transport = null;
try {
transport = mailSession.getTransport(mailAddress);
} catch (NoSuchProviderException e) {
LOGGER.error("No provider found for @: {} \n {}", addrs, e.getMessage());
return;
}
// Envoi du mail.
try {
transport.connect();
transport.sendMessage(message, new Address[]{mailAddress});
transport.close();
} catch (MessagingException e) {
LOGGER.error("Cannot send message: {}", e.getMessage());
return;
}
LOGGER.debug("Mail successfully sent to: {}", addrs);
}
}
/**
* Merge the email template(s) using the velocity context and add the
* content to the JavaMail message object
*
* @param message
* The message that will receive the message content
*
* @param configuration
* The configuration to use
*
* @param context
* The velocity context that will be used to merge the mail
* templates
*
* @throws Exception
*/
protected static void setMessageContent(Message message,
String contenu) throws IOException, MessagingException {
Multipart multipart = new MimeMultipart();
StringWriter writer = new StringWriter();
writer.write(contenu);
writer.close();
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(writer.toString(), "text/html");
multipart.addBodyPart(bodyPart);
message.setContent(multipart);
}
/**
* {@inheritDoc }
*/
@Override
public void sendEmailWithTemplate(String email, String templateKey, Map properties) {
String subject = templateEJB.merge(Constante.PREFIX_EMAIL_TEMPLATE_SUBJECT + templateKey,
Locale.FRENCH, properties);
String body = templateEJB.merge(Constante.PREFIX_EMAIL_TEMPLATE_BODY + templateKey,
Locale.FRENCH, properties);
if (StringUtils.isBlank(subject) || StringUtils.isBlank(body) || email == null) {
throw new IllegalArgumentException("Template or email can not be found.");
} else {
sendMail(subject, body, email);
}
}
/**
* {@inheritDoc }
*/
@Override
public void sendEmailWithTemplate(List usersName, String TemplateKey, Map properties) {
for (String userName : usersName) {
sendEmailWithTemplate(userName, TemplateKey, properties);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy