com.mylab.mail.OpenCmsMailService.java Source code

Java tutorial

Introduction

Here is the source code for com.mylab.mail.OpenCmsMailService.java

Source

/*
 * This file is part of the Synyx Greetingcard module for OpenCms.
 *
 * Copyright (c) 2007 Synyx GmbH & Co.KG (http://www.synyx.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.mylab.mail;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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.opencms.mail.CmsMailHost;
import org.opencms.main.OpenCms;

import com.mylab.DataAccessException;
import com.mylab.dao.GreetingcardConfig;
import com.mylab.dao.WhitelistDao;
import com.mylab.dao.JdbcWhitelistDao;

/**
 * Provides all functionality relevant for sending mail.
 * @author Florian Hopf, Synyx GmbH & Co. KG, hopf@synyx.de
 */
public class OpenCmsMailService implements MailService {

    private Log log = LogFactory.getLog(OpenCmsMailService.class);

    /** Creates a new instance of MailService */
    public OpenCmsMailService() {
    }

    public void addRecipientsWhitelist(MimeMessage message, String to_address, String to_name,
            GreetingcardConfig greetingcardConfig) throws UnsupportedEncodingException, MessagingException {
        WhitelistDao whitelistDao = new JdbcWhitelistDao(greetingcardConfig);
        List<String> receivers = new ArrayList<String>();

        if ((to_address != null) && to_address.contains("@"))
            receivers.add(to_address);
        else
            try {
                receivers = whitelistDao.getReceiverlist(to_name);
            } catch (DataAccessException e) {
            }

        Iterator<String> itr = receivers.iterator();
        String receiverAddress;
        String receiverName = null;
        while (itr.hasNext()) {
            receiverAddress = itr.next();
            try {
                receiverName = whitelistDao.getReceiverName(receiverAddress);
            } catch (DataAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverAddress, receiverName));
        }
    }

    public void sendMail(MessageConfig config) throws MessagingException {
        log.debug("Sending message " + config);

        Session session = getSession();
        final MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress(config.getFrom(), config.getFromName()));
            addRecipientsWhitelist(mimeMessage, config.getTo(), config.getToName(), config.getCardconfig());
        } catch (UnsupportedEncodingException ex) {
            throw new MessagingException("Setting from or to failed", ex);
        }
        mimeMessage.setSubject(config.getSubject());
        mimeMessage.setContent(config.getContent(), config.getContentType());
        // we don't send in a new Thread so that we get the Exception
        Transport.send(mimeMessage);
    }

    public void sendMultipartMail(MessageConfig config, DataSource ds, String filename) throws MessagingException {
        log.debug("Sending multipart message " + config);

        Session session = getSession();
        MimeMultipart multipart = new MimeMultipart();
        MimeBodyPart html = new MimeBodyPart();
        html.setContent(config.getContent(), config.getContentType());
        html.setHeader("MIME-Version", "1.0");
        html.setHeader("Content-Type", html.getContentType());
        multipart.addBodyPart(html);

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(ds));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);

        final MimeMessage message = new MimeMessage(session);
        message.setContent(multipart);
        try {
            message.setFrom(new InternetAddress(config.getFrom(), config.getFromName()));
            addRecipientsWhitelist(message, config.getTo(), config.getToName(), config.getCardconfig());
        } catch (UnsupportedEncodingException ex) {
            throw new MessagingException("Setting from or to failed", ex);
        }

        message.setSubject(config.getSubject());

        // we don't send in a new Thread so that we get the Exception
        Transport.send(message);
    }

    private Session getSession() {
        final CmsMailHost host = OpenCms.getSystemInfo().getMailSettings().getDefaultMailHost();
        Properties props = new Properties();
        props.put("mail.transport.protocol", host.getProtocol());
        props.put("mail.smtp.host", host.getHostname());
        props.put("mail.smtp.auth", host.isAuthenticating());

        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(host.getUsername(), host.getPassword());
            }
        };

        return Session.getInstance(props, authenticator);
    }

    public String getContent(String archiveUrl) {
        return archiveUrl;
    }

    public void sendMultipartMail(MessageConfig config, DataSource imageds, String image, DataSource audiods,
            String audio) throws MessagingException {
        log.debug("Sending multipart message " + config);

        Session session = getSession();
        MimeMultipart multipart = new MimeMultipart();
        MimeBodyPart html = new MimeBodyPart();
        html.setContent(config.getContent(), config.getContentType());
        html.setHeader("MIME-Version", "1.0");
        html.setHeader("Content-Type", html.getContentType());
        multipart.addBodyPart(html);

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(imageds));
        messageBodyPart.setFileName(image);
        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(audiods));
        messageBodyPart.setFileName(audio);
        multipart.addBodyPart(messageBodyPart);

        final MimeMessage message = new MimeMessage(session);
        message.setContent(multipart);
        try {
            message.setFrom(new InternetAddress(config.getFrom(), config.getFromName()));
            addRecipientsWhitelist(message, config.getTo(), config.getToName(), config.getCardconfig());
        } catch (UnsupportedEncodingException ex) {
            throw new MessagingException("Setting from or to failed", ex);
        }

        message.setSubject(config.getSubject());

        // we don't send in a new Thread so that we get the Exception
        Transport.send(message);

    }

}