com.github.dactiv.common.spring.mail.JavaMailService.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.common.spring.mail.JavaMailService.java

Source

/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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.github.dactiv.common.spring.mail;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * spring {@link JavaMailSender} ????Java?
 * 
 * @author maurice
 *
 */
public class JavaMailService {

    private static Logger logger = LoggerFactory.getLogger(JavaMailService.class);

    //spring??
    private JavaMailSender mailSender;
    //freemarker????html
    private Configuration freemarkerConfiguration;
    //MimeMessageHelperfreemarker?
    private String encoding;
    //?runnable ??
    private Executor executor;

    /**
     * spring??
     * 
     * @param mailSender spring??
     */
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * freemarker?
     * 
     * @param freemarkerConfiguration freemarker?
     */
    public void setFreemarkerConfiguration(Configuration freemarkerConfiguration) {
        this.freemarkerConfiguration = freemarkerConfiguration;
    }

    /**
     * MimeMessageHelperfreemarker?
     * 
     * @param encoding ?
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * ?runnable??
     * 
     * @param executor 
     */
    public void setExecutor(Executor executor) {
        this.executor = executor;
    }

    /**
     * freemarker????
     * 
     * @param sendTo ??
     * @param sendFrom ?
     * @param subject 
     * @param templateName freemarker???
     * @param attachment mapkey????value?????
     * @param model freemarker???el??
     * 
     * @throws IOException
     * @throws TemplateException
     * 
     */
    public void sendByTemplate(String sendTo, String sendFrom, String subject, String templateName,
            Map<String, File> attachment, Map<String, ?> model) throws IOException, TemplateException {
        send(sendTo, sendFrom, subject, getTemplateString(templateName, model), attachment);
    }

    /**
     * ??
     * 
     * @param sendTo ??
     * @param sendFrom ?
     * @param subject 
     * @param content 
     * @param attachment mapkey????value?????
     */
    public void send(final String sendTo, final String sendFrom, final String subject, final String content,
            final Map<String, File> attachment) {
        if (this.executor != null) {
            this.executor.execute(new Runnable() {
                @Override
                public void run() {
                    doSend(sendTo, sendFrom, subject, content, attachment);
                }
            });
        } else {
            doSend(sendTo, sendFrom, subject, content, attachment);
        }

    }

    /**
     * ??
     * 
     * @param sendTo ??
     * @param sendFrom ?
     * @param subject 
     * @param content 
     * @param attachment mapkey????value?????
     */
    private void doSend(String sendTo, String sendFrom, String subject, String content,
            Map<String, File> attachment) {
        try {
            MimeMessage msg = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, encoding);

            helper.setTo(sendTo);
            helper.setFrom(sendFrom);
            helper.setSubject(subject);
            helper.setText(content, true);

            if (!MapUtils.isEmpty(attachment)) {
                for (Entry<String, File> entry : attachment.entrySet()) {
                    helper.addAttachment(entry.getKey(), entry.getValue());
                }
            }

            mailSender.send(msg);
            logger.info("???");
        } catch (MessagingException e) {
            logger.error("", e);
        } catch (Exception e) {
            logger.error("??", e);
        }
    }

    /**
     * ?freemarker?
     * 
     * @param templateName ???
     * @param model freemarker???el??
     * 
     * @return String
     * 
     * @throws IOException
     * @throws TemplateException
     */
    private String getTemplateString(String templateName, Map<String, ?> model)
            throws IOException, TemplateException {
        Template template = freemarkerConfiguration.getTemplate(templateName, encoding);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }
}