Springboot下邮件通知

       最近公司接了宜家的几个产品,需要在原有的抽取服务添加完成后邮寄通知功能。先搞个初级版本,话不多说,上代码:

一,引入依赖包

    
      org.apache.commons
      commons-email
      1.5
    

二,配置相关信息

email:
  yijia:                        ##  开发环境
    host: smtp.163.com
    charset: gbk
    username: 邮箱@163.com
    password: EPXXX
    templateName: yiJiaEmail
    subject: IKEA related code table update tips
    receiveMails: 接收[email protected],接收[email protected]

      如果本地测试用自己邮件,需要开下权限。

三,邮件模板

       在resources下创建templates文件夹写需要的模板,eg:





    


抽取任务完成
任务名称 产品编号

四,工具类

package com.easipass.extract.util;

import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * @program: BDP_SRVC_DAAS_EXTRACT @Package: com.easipass.extract.util @ClassName: EmailTool
 * @author: qqzhang
 * @create: 2020-05-27 15:13
 * @description: @UpdateUser: qqzhang @UpdateRemark: @UpdateDate: 2020-05-27 15:13 @Version: 1.0
 */
@Configuration
@Slf4j
public class EmailTool {
  private static TemplateEngine templateEngine;

  @Autowired
  public EmailTool(TemplateEngine templateEngine) {
    EmailTool.templateEngine = templateEngine;
  }
  // 1  字符串   2  html
  public static Map sendEmail(
      Map datamap, Map emailParam, String type)
      throws EmailException {
    Map resultMap = new HashMap();
    int successSum = 0;
    int errSum = 0;
    StringBuffer errEmails = new StringBuffer();
    String templateName = (String) emailParam.get("templateName");
    String[] receiveMails = (String[]) emailParam.get("receiveMails");
    String subject = (String) emailParam.get("subject");
    String context = null;
    if ("1".equals(type)) {
      context = (String) datamap.get("context");
    } else if ("2".equals(type)) {
      context = getContext(datamap, templateName);
    } else {
      log.error("#####  不支持此类型邮件  #####");
    }
    for (String email : receiveMails) {
      try {
        sendEmail(email, context, subject, emailParam);
        log.info("发送邮件 至:  " + email + "  成功");
        successSum++;
      } catch (Exception e) {
        log.info("发送邮件 至:  " + email + "  失败,异常信息:" + e.getMessage(), e);
        errSum++;
        errEmails.append(email + ",");
      }
    }
    resultMap.put("successSum", successSum);
    resultMap.put("errSum", errSum);
    if (errSum > 0) {
      resultMap.put("errEmails", errEmails.deleteCharAt(errEmails.length() - 1).toString());
    }
    return resultMap;
  }

  private static void sendEmail(
      String mailto, String context, String subject, Map emailParam)
      throws EmailException {
    String host = (String) emailParam.get("host");
    String charset = (String) emailParam.get("charset");
    String username = (String) emailParam.get("username");
    String password = (String) emailParam.get("password");

    HtmlEmail email = new HtmlEmail();
    email.setHostName(host);
    email.setCharset(charset);
    email.setAuthentication(username, password);
    email.setFrom(username, "");
    email.setSubject(subject);
    email.setHtmlMsg(context);
    email.addTo(mailto);
    String send = email.send();
    log.info("send = " + send);
  }

  // 获取html模板
  private static String getContext(Map datamap, String templateName) {
    Context context = new Context();
    context.setVariables(datamap);
    return templateEngine.process(templateName, context);
  }
}

五,使用

  private Map sendEmail(TaskInfoDTO taskInfo) throws Exception {
    Map datamap = new HashMap<>();
    String context = null;
    // ...
    // context 发送内容
    // ...
    datamap.put("context", context);
    Map emailParam = new HashMap<>();
    emailParam.put("host", host);
    emailParam.put("charset", charset);
    emailParam.put("username", username);
    emailParam.put("password", password);
    emailParam.put("templateName", templateName);
    emailParam.put("subject", subject);
    emailParam.put("receiveMails", receiveMails);
    return EmailTool.sendEmail(datamap, emailParam, "1");
  }

六,效果图

Springboot下邮件通知_第1张图片

邮件模板写的很简单,如需其他样式自行修改模板。

你可能感兴趣的:(SpringBoot)