com.github.dennisit.vplus.data.utils.EmailUtils Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.utils;
import com.github.dennisit.vplus.data.enums.common.BatchEnum;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
/**
* @author Elon.su
*/
public class EmailUtils {
/**
* 发送简单的文件邮件
* @param sender 邮件发送客户端
* @param to 邮件接收人
* @param subject 主题
* @param content 内容
* @param dealEnum 发送方式
* @return 邮件发送的消息集合
*/
public static List sendText(JavaMailSenderImpl sender, List to, String subject, String content, BatchEnum dealEnum) {
if(null == sender || org.apache.commons.collections.CollectionUtils.isEmpty(to) || org.apache.commons.lang3.StringUtils.isBlank(subject) || org.apache.commons.lang3.StringUtils.isBlank(content)){
throw new RuntimeException("参数错误");
}
List list = Lists.newArrayList();
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(sender.getUsername());
message.setSubject(subject);
message.setText(content);
if(BatchEnum.FOREACH == dealEnum){
for(String t : to){
message.setTo(t);
list.add(message);
}
}else{
message.setTo(to.toArray(new String[0]));
list.add(message);
}
sender.send(list.toArray(new SimpleMailMessage[0]));
return list;
}
/**
* 发送HTML邮件
* @param sender 邮件发送客户端
* @param to 邮件接收人
* @param subject 邮件主题
* @param html html内容
* @param attaches 邮件附件
* @param dealEnum 发送方式
* @return 邮件消息集合
* @throws Exception 发送异常
*/
public static List sendHtml(JavaMailSenderImpl sender, List to, String subject, String html, List attaches, BatchEnum dealEnum) throws Exception{
if(null == sender || CollectionUtils.isEmpty(to) || org.apache.commons.lang3.StringUtils.isBlank(subject) || StringUtils.isBlank(html)){
throw new RuntimeException("参数错误");
}
List list = Lists.newArrayList();
MimeMessage mimeMessage = sender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.name());
messageHelper.setFrom(sender.getUsername());
messageHelper.setSubject(subject);
messageHelper.setText(html, true);
attaches = Optional.ofNullable(attaches).orElse(Lists.newArrayList());
for(String attach: attaches){
FileSystemResource file = new FileSystemResource(new File(attach));
messageHelper.addAttachment(file.getFilename(), file);
}
if(BatchEnum.FOREACH == dealEnum){
for(String t : to){
messageHelper.setTo(t);
list.add(mimeMessage);
}
}else{
messageHelper.setTo(to.toArray(new String[0]));
list.add(mimeMessage);
}
sender.send(list.toArray(new MimeMessage[0]));
return list;
}
}