yoyo.framework.enterprise.infra.messaging.MailServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for yoyo.framework.enterprise.infra.messaging.MailServiceImpl.java

Source

// ========================================================================
// Copyright (C) YOYO Project Team. All rights reserved.
// GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
// http://www.gnu.org/licenses/agpl-3.0.txt
// ========================================================================
package yoyo.framework.enterprise.infra.messaging;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.Validate;
import yoyo.framework.enterprise.infra.messaging.MailService;
import yoyo.framework.enterprise.shared.EnterpriseException;

/**
 * I/F
 * @author nilcy
 */
@Stateless
@Alternative
public class MailServiceImpl implements MailService {
    /** ? */
    private static final long serialVersionUID = 5415112327668280272L;
    /**  */
    @Resource(name = "mail/primary")
    private Session session;

    @Override
    public void setup(final Session session) {
        this.session = session;
    }

    @Override
    public void send(final String from, final String to, final String subject, final Object objectBody,
            final String contentType) throws EnterpriseException {
        try {
            final Message message = createMessage(from, to, subject);
            message.setContent(objectBody, contentType);
            send(message);
        } catch (final MessagingException e) {
            throw new EnterpriseException(e.getLocalizedMessage());
        }
    }

    @Override
    public void send(final String from, final String to, final String subject, final String textBody)
            throws EnterpriseException {
        try {
            final Message message = createMessage(from, to, subject);
            message.setContent(textBody, "text/plain");
            send(message);
        } catch (final MessagingException e) {
            throw new EnterpriseException(e.getLocalizedMessage());
        }
    }

    @Override
    public void send(final String from, final String to, final String subject, final Multipart multipartBody)
            throws EnterpriseException {
        try {
            final Message message = createMessage(from, to, subject);
            message.setContent(multipartBody);
            send(message);
        } catch (final MessagingException e) {
            throw new EnterpriseException(e.getLocalizedMessage());
        }
    }

    /**
     * ??
     * @param from FROM
     * @param to TO
     * @param subject ??
     * @return 
     * @throws EnterpriseException ??
     */
    private Message createMessage(final String from, final String to, final String subject)
            throws EnterpriseException {
        Validate.notNull(session, "????????");
        try {
            final Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            return message;
        } catch (final AddressException e) {
            throw new EnterpriseException(e.getLocalizedMessage());
        } catch (final MessagingException e) {
            throw new EnterpriseException(e.getLocalizedMessage());
        }
    }

    /**
     * ?
     * @param message 
     * @throws EnterpriseException ?
     */
    private static void send(final Message message) throws EnterpriseException {
        try {
            Transport.send(message);
        } catch (final MessagingException e) {
            throw new EnterpriseException(e.getLocalizedMessage());
        }
    }
}