org.openmrs.notification.mail.MailMessageSender.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.notification.mail.MailMessageSender.java

Source

/**
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
 *
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
 * graphic logo is a trademark of OpenMRS Inc.
 */
package org.openmrs.notification.mail;

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.openmrs.api.context.Context;
import org.openmrs.notification.Message;
import org.openmrs.notification.MessageException;
import org.openmrs.notification.MessageSender;
import org.springframework.util.StringUtils;

public class MailMessageSender implements MessageSender {

    protected static final Log log = LogFactory.getLog(MailMessageSender.class);

    /**
     * JavaMail session
     */
    private Session session;

    /**
     * Default public constructor.
     */
    public MailMessageSender() {
    }

    /**
     * Public constructor.
     *
     * @param session
     */
    public MailMessageSender(Session session) {
        this.session = session;
    }

    /**
     * Set javamail session.
     *
     * @param session
     */
    public void setMailSession(Session session) {
        this.session = session;
    }

    /**
     * Send the message.
     *
     * @param message the message to be sent
     */
    public void send(Message message) throws MessageException {
        try {
            MimeMessage mimeMessage = createMimeMessage(message);
            Transport.send(mimeMessage);
        } catch (Exception e) {
            log.error("failed to send message", e);

            // catch mail-specific exception and re-throw it as app-specific exception
            throw new MessageException(e);
        }
    }

    /**
     * Converts the message object to a mime message in order to prepare it to be sent.
     *
     * @param message
     * @return MimeMessage
     */
    public MimeMessage createMimeMessage(Message message) throws Exception {

        if (message.getRecipients() == null) {
            throw new MessageException("Message must contain at least one recipient");
        }

        // set the content-type to the default if it isn't defined in Message
        if (!StringUtils.hasText(message.getContentType())) {
            String contentType = Context.getAdministrationService().getGlobalProperty("mail.default_content_type");
            message.setContentType(StringUtils.hasText(contentType) ? contentType : "text/plain");
        }

        MimeMessage mimeMessage = new MimeMessage(session);

        // TODO Need to test the null case.  
        // Transport should use default mail.from value defined in properties.
        if (message.getSender() != null) {
            mimeMessage.setSender(new InternetAddress(message.getSender()));
        }

        mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO,
                InternetAddress.parse(message.getRecipients(), false));
        mimeMessage.setSubject(message.getSubject());

        if (!message.hasAttachment()) {
            mimeMessage.setContent(message.getContent(), message.getContentType());
        } else {
            mimeMessage.setContent(createMultipart(message));
        }

        return mimeMessage;
    }

    /**
     * Creates a MimeMultipart, so that we can have an attachment.
     *
     * @param message
     * @return
     */
    private MimeMultipart createMultipart(Message message) throws Exception {
        MimeMultipart toReturn = new MimeMultipart();

        MimeBodyPart textContent = new MimeBodyPart();
        textContent.setContent(message.getContent(), message.getContentType());

        MimeBodyPart attachment = new MimeBodyPart();
        attachment.setContent(message.getAttachment(), message.getAttachmentContentType());
        attachment.setFileName(message.getAttachmentFileName());

        toReturn.addBodyPart(textContent);
        toReturn.addBodyPart(attachment);

        return toReturn;
    }

}