邮件发送工具类

2019-03-26  本文已影响0人  刘书生

工具类

package cn.wgd.zmx.utils;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

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

/**
 * 邮件发送工具类
 */
@Component
public class MailUtils {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String username;

    /**
     * 发送简单邮件
     * @param email  接受方邮件
     * @param title  邮件主题
     * @param content   邮件内容
     */
    public void sendSimpleMailMessage(String email, String title, String content) {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(username);
        message.setTo(email);
        message.setSubject(title);
        message.setText(content);

        mailSender.send(message);
    }

    /**
     * 发送带附件的邮件
     * @param email  接受方邮件
     * @param title  邮件主题
     * @param content  邮件内容
     * @param name  附件名字
     * @param file  附件
     */
    public void sendComplexMail(String email, String title, String content, String name, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        /**
         * 发送的邮件是不是富文本(附件,图片,html等)
         */
        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

            mimeMessageHelper.setFrom(username);
            mimeMessageHelper.setTo(email);

            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content);

            if (file.exists()) {
                mimeMessageHelper.addAttachment(name, new FileSystemResource(file));
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        mailSender.send(mimeMessage);
    }

    /**
     * 发送模板邮件
     *
     * @param email 接受方邮件
     * @param params 模板里面的参数key与替换值value
     * @param model 模板,在/templates/mail下面
     * @param title 主题
     */
    public void FreemarkerMail(String email, HashMap<String, Object> params, String model, String title) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

            mimeMessageHelper.setFrom(username);
            mimeMessageHelper.setTo(email);

            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            configuration.setClassForTemplateLoading(this.getClass(), "/templates/mail");

            String html = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(model), params);

            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(html, true);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        mailSender.send(mimeMessage);
    }

}

mail.ftl模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>你好, <span style="color: red;">${username}</span>, 这是一封模板邮件!</h3>
</body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读