shnakkydoodle.notifying.endpoints.EmailEndpoint.java Source code

Java tutorial

Introduction

Here is the source code for shnakkydoodle.notifying.endpoints.EmailEndpoint.java

Source

/*
 * Author Stephen Booysen
 *
 * Copyright (c) 2012 Stephen Booysen, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Stephen Booysen. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Stephen Booysen
 *
 * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package shnakkydoodle.notifying.endpoints;

import shnakkydoodle.common.PropertiesManager;
import shnakkydoodle.notifying.common.IEndpointProvider;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

/**
 * @author Stephen
 *
 */
public class EmailEndpoint implements IEndpointProvider {

    // region Private Members

    private PropertiesManager properties;

    // end region

    // region Public Members

    /**
     * Constructor
     * 
     * @param properties
     */
    public EmailEndpoint(PropertiesManager properties) {
        this.properties = properties;
    }

    /**
     * Send the notification
     * 
     * @param recipient
     * @param subject
     * @param message
     * @throws Exception
     */
    @Override
    public void send(String recipient, String subject, String message) throws Exception {

        Email email = null;

        if (!this.properties.getProperties().containsKey("notification.email.server")
                || !this.properties.getProperties().containsKey("notification.email.port")
                || !this.properties.getProperties().containsKey("notification.email.username")
                || !this.properties.getProperties().containsKey("notification.email.password")
                || !this.properties.getProperties().containsKey("notification.email.fromemail")) {
            throw new Exception("Notification email settings incomplete");
        }

        // Determine if we need to send an html or simple email
        if (message.trim().startsWith("<")) {
            email = new HtmlEmail();
        } else {
            email = new SimpleEmail();
        }

        email.setHostName(this.properties.getProperties().get("notification.email.server").toString());
        email.setSmtpPort(
                Integer.parseInt(this.properties.getProperties().get("notification.email.port").toString()));
        email.setAuthentication(this.properties.getProperties().get("notification.email.username").toString(),
                this.properties.getProperties().get("notification.email.password").toString());
        email.setFrom(this.properties.getProperties().get("notification.email.fromemail").toString());
        email.setSubject(subject);
        email.setMsg(message);
        email.addTo(recipient);
        email.send();
    }

    // end region

}