springboot 普通发送邮件 和 freemarker模板发送邮件

package com.rayclear.task;

import com.rayclear.common.JWTManager;
import com.rayclear.dao.CouponCodesMapper;
import com.rayclear.domain.CouponCodes;
import com.rayclear.util.SendMailUtil;
import com.rayclear.util.StringUtil;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.util.*;

import javax.mail.internet.MimeMessage;

/**
 * 异步执行方法类,需要执行的异步业务方法都可写在这里
 *
 * @author zhoushuai
 */
@Component
public class AsyncCommon {
    
    private static  Logger LOGGER = LoggerFactory.getLogger(AsyncCommon .class);

    @Autowired
    private JavaMailSender mailSender;
    @Autowired  
    Configuration configuration;


    /**
     * 发送普通邮件
     */

    @Async
    public void sendMail(String exceptionInfo) {
        List list = SendMailUtil.getAddressList();
        for (String string : list) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("[email protected]");
            message.setTo(string);
            message.setSubject("java api server exception");
            message.setText("Exception:" + exceptionInfo);
            mailSender.send(message);
        }
    }
    /**
     * 使用freemarker模板发送邮件
     */
    @Async
    public void sendApplyMail(Map map) {
        MimeMessage message = mailSender.createMimeMessage();
        List list = SendMailUtil.getApplyList();
        for (String string : list) {
            Map model = new HashMap();  
            model.put("nickname", StringUtil.isNull(map.get("nickname")) ? "" : map.get("nickname"));
            model.put("realname", StringUtil.isNull(map.get("realname")) ? "" : map.get("realname"));
            model.put("phone", StringUtil.isNull(map.get("phone")) ? "" : map.get("phone"));  
            model.put("wechat", StringUtil.isNull(map.get("wechat")) ? "" : map.get("wechat"));
            model.put("weibo", StringUtil.isNull(map.get("weibo")) ? "" : map.get("weibo"));
            model.put("organization", StringUtil.isNull(map.get("organization")) ? "" : map.get("organization"));
            model.put("reason", StringUtil.isNull(map.get("reason")) ? "" : map.get("reason"));
            try {
                Template t = configuration.getTemplate("sendEmail.ftl");  
                String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);  
                
                MimeMessageHelper helper = new MimeMessageHelper(message, true);  
                helper.setFrom("[email protected]");
                helper.setTo(string);
                helper.setSubject("讲师认证通知");
                helper.setText(content, true);  
                mailSender.send(message);
            } catch (Exception e) {
                LOGGER.info("****************讲师认证邮件发送失败**********失败账号为:{}******", string);
            }
        }
    }

}

你可能感兴趣的:(spring,boot)