ips1ap101.ejb.core.mail.MailerBean.java Source code

Java tutorial

Introduction

Here is the source code for ips1ap101.ejb.core.mail.MailerBean.java

Source

/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los trminos
 * de la licencia "GNU General Public License" publicada por la Fundacin "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser til, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacin.
 */
package ips1ap101.ejb.core.mail;

import java.security.InvalidParameterException;
import java.util.Date;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import ips1ap101.lib.base.app.constant.EAB;
import ips1ap101.lib.core.util.EA;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author Jorge Campins
 */
@Named
@Stateless
public class MailerBean implements MailerBeanLocal {

    private static final Logger logger = Logger.getLogger(MailerBean.class);

    @Resource(name = EAB.MAILER_BEAN_SESSION)
    private Session session;

    @Asynchronous
    @Override
    public Future<Object> sendMessageAsynchronously(String addressList, String subject, String text) {
        try {
            Object message = sendMessage(addressList, subject, text);
            return new AsyncResult<>(message);
        } catch (Exception ex) {
            logger.fatal(ex.getMessage(), ex);
            Object message = ex;
            return new AsyncResult<>(message);
        }
    }

    @Override
    public Message sendMessage(String addressList, String subject, String text) throws MessagingException {
        if (EA.isMailingEnabled()) {
        } else {
            return null;
        }
        if (StringUtils.isBlank(addressList)) {
            throw new InvalidParameterException("addressList");
        }
        if (StringUtils.isBlank(subject)) {
            throw new InvalidParameterException("subject");
        }
        if (StringUtils.isBlank(text)) {
            throw new InvalidParameterException("text");
        }
        Address[] internetAddressList = InternetAddress.parse(addressList, false);
        Date timeStamp = new Date();
        Message message = new MimeMessage(session);
        message.setFrom();
        message.setHeader("X-Mailer", "JavaMail");
        message.setRecipients(Message.RecipientType.TO, internetAddressList);
        message.setSentDate(timeStamp);
        message.setSubject(subject);
        message.setText(text);
        Transport.send(message);
        return message;
    }

}