com.oakhole.core.email.MimeMailService.java Source code

Java tutorial

Introduction

Here is the source code for com.oakhole.core.email.MimeMailService.java

Source

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

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.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;

/**
 * MIME?.
 * <p/>
 * Freemarker?html?, .
 *
 * @author calvin
 */
@SuppressWarnings("ALL")
public class MimeMailService {

    private static final String DEFAULT_ENCODING = "utf-8";
    private static Logger logger = LoggerFactory.getLogger(MimeMailService.class);
    private JavaMailSender mailSender;
    private TemplateEngine templateEngine;
    private String template;
    private String attachment;
    private String subject;
    private Context context;
    private String from;
    private String to;

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

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

            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            //
            String content = generateContent();
            helper.setText(content, true);
            //
            File attachment = generateAttachment();
            helper.addAttachment(attachment.getName(), attachment);

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

    /**
     * Freemarker?html?.
     */
    private String generateContent() throws MessagingException {
        return templateEngine.process(template, context);
    }

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

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

    /**
     * thymeleaf?
     *
     * @param templateEngine
     */
    public void setTemplateEngine(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setTo(String to) {
        this.to = to;
    }
}