com.jrzmq.core.email.SimpleMailService.java Source code

Java tutorial

Introduction

Here is the source code for com.jrzmq.core.email.SimpleMailService.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.jrzmq.core.email;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

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

/**
 * ?.
 * 
 * @author calvin
 */
public class SimpleMailService {
    private static Logger logger = LoggerFactory.getLogger(SimpleMailService.class);

    private static final String DEFAULT_ENCODING = "utf-8";

    private JavaMailSender mailSender;

    private Configuration freemarkerConfiguration;

    /**
     * ??
     * @param emailTo 
     * @param project 
     * @param ???
     * @param map     ?
     */
    public void sendMail(String emailTo, String subject, String templateName, Map<String, Object> map) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setSentDate(new Date());
        msg.setTo(emailTo);
        msg.setSubject(subject);

        try {
            msg.setText(generateContent(templateName, map));
            mailSender.send(msg);
            if (logger.isInfoEnabled()) {
                logger.info("??{}", StringUtils.join(msg.getTo(), ","));
            }
        } catch (Exception e) {
            logger.error("??{},:{},", new Object[] { emailTo, subject }, e);
        }
    }

    /**
     * ?
     * @param templateName        ???
     * @param map                 ?
     * @return
     * @throws MessagingException
     */
    private String generateContent(String templateName, Map<String, Object> map) throws MessagingException {

        try {
            return FreeMarkerTemplateUtils.processTemplateIntoString(
                    freemarkerConfiguration.getTemplate(templateName, DEFAULT_ENCODING), map);
        } catch (IOException e) {
            logger.error("?, FreeMarker??", e);
            throw new MessagingException("FreeMarker??", e);
        } catch (TemplateException e) {
            logger.error("?, FreeMarker?", e);
            throw new MessagingException("FreeMarker?", e);
        }
    }

    public JavaMailSender getMailSender() {
        return mailSender;
    }

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public Configuration getFreemarkerConfiguration() {
        return freemarkerConfiguration;
    }

    public void setFreemarkerConfiguration(Configuration freemarkerConfiguration) {
        this.freemarkerConfiguration = freemarkerConfiguration;
    }
}