com.it.j2ee.modules.email.MimeMailService.java Source code

Java tutorial

Introduction

Here is the source code for com.it.j2ee.modules.email.MimeMailService.java

Source

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

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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;

/**
 * MIME?.
 * 
 * Freemarker?html?, .
 * 
 * @author calvin
 */
public class MimeMailService {

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

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

    private JavaMailSender mailSender;

    private Template template;

    /**
     * ??MIME?.
     */
    public void sendNotificationMail(String userName) {

        try {
            MimeMessage msg = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, DEFAULT_ENCODING);

            helper.setTo("springside3.demo@gmail.com");
            helper.setFrom("springside3.demo@gmail.com");
            helper.setSubject("");

            String content = generateContent(userName);
            helper.setText(content, true);

            File attachment = generateAttachment();
            helper.addAttachment("mailAttachment.txt", attachment);

            mailSender.send(msg);
            logger.info("HTML??springside3.demo@gmail.com");
        } catch (MessagingException e) {
            logger.error("", e);
        } catch (Exception e) {
            logger.error("??", e);
        }
    }

    /**
     * Freemarker?html?.
     */
    private String generateContent(String userName) throws MessagingException {

        try {
            Map context = Collections.singletonMap("userName", userName);
            return FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
        } catch (IOException e) {
            logger.error("?, FreeMarker??", e);
            throw new MessagingException("FreeMarker??", e);
        } catch (TemplateException e) {
            logger.error("?, FreeMarker?", e);
            throw new MessagingException("FreeMarker?", e);
        }
    }

    /**
     * ?classpath.
     */
    private File generateAttachment() throws MessagingException {
        try {
            Resource resource = new ClassPathResource("/email/mailAttachment.txt");
            return resource.getFile();
        } catch (IOException e) {
            logger.error(",?", e);
            throw new MessagingException("?", e);
        }
    }

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

    /**
     * Freemarker?,Freemarker ?.
     */
    public void setFreemarkerConfiguration(Configuration freemarkerConfiguration) throws IOException {
        // ?freemarkerConfigurationtemplateLoaderPath.
        template = freemarkerConfiguration.getTemplate("mailTemplate.ftl", DEFAULT_ENCODING);
    }
}