com.formkiq.core.service.notification.MailSenderService.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.core.service.notification.MailSenderService.java

Source

/*
 * Copyright (C) 2017 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.formkiq.core.service.notification;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.internet.MimeMessage;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.formkiq.core.config.LoggerMailSender;
import com.formkiq.core.util.Resources;

/**
 * MailSenderService looks for a mail.properties file to configure sending of
 * email. If file doesn't exist, defaults to logging all emails.
 *
 */
public class MailSenderService implements JavaMailSender, InitializingBean {

    /** Logger. */
    private static final Logger LOG = Logger.getLogger(MailSenderService.class.getName());

    /** Logger Mail Sender. */
    private LoggerMailSender mailLogger = new LoggerMailSender();

    /** Java Mail Sender. */
    private JavaMailSenderImpl mailSender = null;

    @Override
    public void afterPropertiesSet() throws Exception {

        InputStream is = Resources.getResourceAsInputStream("/mail.properties");

        try {
            afterPropertiesSet(is);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    /**
     * After Properties Set.
     * @param is {@link InputStream}
     * @throws IOException IOException
     */
    public void afterPropertiesSet(final InputStream is) throws IOException {

        boolean loaded = false;

        if (is != null) {
            Properties prop = new Properties();
            prop.load(is);

            this.mailSender = createJavaMailSender(prop);
            loaded = true;
        }

        if (!loaded) {
            LOG.log(Level.WARNING, "Cannot find mail.properties defaulting to " + "java.util.logging.Logger");
        }
    }

    /**
     * @param prop {@link Properties}
     * @return {@link JavaMailSenderImpl}
     * @throws IOException IOException
     */
    private JavaMailSenderImpl createJavaMailSender(final Properties prop) throws IOException {

        JavaMailSenderImpl sender = new JavaMailSenderImpl();

        sender.setHost(prop.getProperty("mail.host"));
        sender.setPort(Integer.parseInt(prop.getProperty("mail.port")));

        sender.setUsername(prop.getProperty("mail.username"));
        sender.setPassword(prop.getProperty("mail.password"));

        Properties property = sender.getJavaMailProperties();

        if (prop.containsKey("mail.smtp.auth")) {
            property.setProperty("mail.smtp.auth", prop.getProperty("mail.smtp.auth"));
        }

        if (prop.containsKey("mail.smtp.starttls.enable")) {
            property.setProperty("mail.smtp.starttls.enable", prop.getProperty("mail.smtp.starttls.enable"));
        }

        if (prop.containsKey("mail.smtp.quitwait")) {
            property.setProperty("mail.smtp.quitwait", prop.getProperty("mail.smtp.quitwait"));
        }

        LOG.log(Level.INFO, "Setting mail.host = " + sender.getHost() + ":" + sender.getPort());
        LOG.log(Level.INFO, "Setting mail.username = " + sender.getUsername());

        for (Map.Entry<Object, Object> e : sender.getJavaMailProperties().entrySet()) {
            LOG.log(Level.INFO, "Setting " + e.getKey() + " = " + e.getValue());
        }

        return sender;
    }

    @Override
    public MimeMessage createMimeMessage() {
        return getMailSender().createMimeMessage();
    }

    @Override
    public MimeMessage createMimeMessage(final InputStream contentStream) throws MailException {
        return getMailSender().createMimeMessage(contentStream);
    }

    /**
     * @return {@link JavaMailSender}
     */
    public JavaMailSender getMailSender() {
        return this.mailSender != null ? this.mailSender : this.mailLogger;
    }

    @Override
    public void send(final MimeMessage mimeMessage) throws MailException {
        getMailSender().send(mimeMessage);
    }

    @Override
    public void send(final MimeMessage... mimeMessages) throws MailException {
        getMailSender().send(mimeMessages);
    }

    @Override
    public void send(final MimeMessagePreparator mimeMessagePreparator) throws MailException {
        getMailSender().send(mimeMessagePreparator);
    }

    @Override
    public void send(final MimeMessagePreparator... mimeMessagePreparators) throws MailException {
        getMailSender().send(mimeMessagePreparators);
    }

    @Override
    public void send(final SimpleMailMessage sm) throws MailException {
        getMailSender().send(sm);
    }

    @Override
    public void send(final SimpleMailMessage... messages) throws MailException {
        getMailSender().send(messages);
    }
}