feat(新增): 封装为空对象判断,检测邮件,邮件多附件发送

This commit is contained in:
bunny 2024-05-09 16:54:37 +08:00
parent c1f4b9b40a
commit e113d46ee5
6 changed files with 43 additions and 23 deletions

View File

@ -1,11 +1,20 @@
package cn.bunny.common.service.utils; package cn.bunny.common.service.utils;
import cn.bunny.common.service.exception.BunnyException; import cn.bunny.common.service.exception.BunnyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@Slf4j
public class EmptyUtil { public class EmptyUtil {
/**
* 是否为空
*
* @param value 判断值
* @param message 错误消息
*/
public static void isEmpty(Object value, String message) { public static void isEmpty(Object value, String message) {
if (value == null || !StringUtils.hasText(value.toString())) { if (value == null || !StringUtils.hasText(value.toString())) {
log.error("为空对象错误:{}{}", value, message);
throw new BunnyException(message); throw new BunnyException(message);
} }
} }

View File

@ -1,22 +1,22 @@
package cn.bunny.entity.email; package cn.bunny.entity.email;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
/**
* 邮件发送对象
*/
@Data @Data
@Builder @Builder
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class EmailSend { public class EmailSend {
// 给谁发送 // 给谁发送
@NotNull(message = "发送者不能为空")
private String sendTo; private String sendTo;
// 发送主题 // 发送主题
@NotNull(message = "主题不能为空")
private String subject; private String subject;
// 是否为富文本 // 是否为富文本
private Boolean isRichText; private Boolean isRichText;
@ -25,5 +25,5 @@ public class EmailSend {
// 抄送人 // 抄送人
private String ccParam; private String ccParam;
// 发送的文件 // 发送的文件
private MultipartFile file; private MultipartFile[] file;
} }

View File

@ -4,7 +4,12 @@ import cn.bunny.common.constant.MessageConstant;
import cn.bunny.common.service.utils.EmptyUtil; import cn.bunny.common.service.utils.EmptyUtil;
import cn.bunny.entity.email.EmailSend; import cn.bunny.entity.email.EmailSend;
public class MailSendCheck { public class MailSendCheckUtil {
/**
* 检测发送对象是否为空的对象
*
* @param emailSend 邮件发送对象
*/
public static void check(EmailSend emailSend) { public static void check(EmailSend emailSend) {
// 空发送对象 // 空发送对象
EmptyUtil.isEmpty(emailSend, MessageConstant.EMPTY_SEND_OBJECT); EmptyUtil.isEmpty(emailSend, MessageConstant.EMPTY_SEND_OBJECT);

View File

@ -11,14 +11,14 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.Objects; import java.util.Objects;
public class MailSenderHelper { public class MailSenderUtil {
private final String username; private final String username;
private final JavaMailSenderImpl javaMailSender; private final JavaMailSenderImpl javaMailSender;
/** /**
* 初始化构造函数进行当前类赋值 * 初始化构造函数进行当前类赋值
*/ */
public MailSenderHelper(EmailSendInit emailSendInit) { public MailSenderUtil(EmailSendInit emailSendInit) {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(emailSendInit.getHost()); javaMailSender.setHost(emailSendInit.getHost());
javaMailSender.setPort(emailSendInit.getPort()); javaMailSender.setPort(emailSendInit.getPort());
@ -37,7 +37,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息 * @param emailSend 邮件消息
*/ */
public void sendSimpleEmail(EmailSend emailSend) { public void sendSimpleEmail(EmailSend emailSend) {
MailSendCheck.check(emailSend); MailSendCheckUtil.check(emailSend);
// 创建邮件消息体 SimpleMailMessage 发送简单邮件 // 创建邮件消息体 SimpleMailMessage 发送简单邮件
SimpleMailMessage mailMessage = new SimpleMailMessage(); SimpleMailMessage mailMessage = new SimpleMailMessage();
@ -59,7 +59,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息 * @param emailSend 邮件消息
*/ */
public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file, boolean isRich) throws MessagingException { public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file, boolean isRich) throws MessagingException {
MailSendCheck.check(emailSend); MailSendCheckUtil.check(emailSend);
// 创建 MimeMessage 对象用户发送附件或者是富文本内容 // 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage(); MimeMessage mailMessage = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper // 创建 MimeMessageHelper
@ -86,7 +86,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息 * @param emailSend 邮件消息
*/ */
public void sendRichText(EmailSend emailSend, boolean isRich) throws MessagingException { public void sendRichText(EmailSend emailSend, boolean isRich) throws MessagingException {
MailSendCheck.check(emailSend); MailSendCheckUtil.check(emailSend);
// 创建 MimeMessage 对象用户发送附件或者是富文本内容 // 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage(); MimeMessage mailMessage = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper // 创建 MimeMessageHelper
@ -111,7 +111,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息 * @param emailSend 邮件消息
*/ */
public void sendCC(EmailSend emailSend, boolean isRich) throws MessagingException { public void sendCC(EmailSend emailSend, boolean isRich) throws MessagingException {
MailSendCheck.check(emailSend); MailSendCheckUtil.check(emailSend);
// 创建 MimeMessage 对象用于发送邮件富文本或者附件 // 创建 MimeMessage 对象用于发送邮件富文本或者附件
MimeMessage message = javaMailSender.createMimeMessage(); MimeMessage message = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper // 创建 MimeMessageHelper
@ -138,7 +138,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息 * @param emailSend 邮件消息
*/ */
public void sendEmail(EmailSend emailSend) throws MessagingException { public void sendEmail(EmailSend emailSend) throws MessagingException {
MailSendCheck.check(emailSend); MailSendCheckUtil.check(emailSend);
// 创建 MimeMessage 对象用于发送邮件富文本或者附件 // 创建 MimeMessage 对象用于发送邮件富文本或者附件
MimeMessage message = javaMailSender.createMimeMessage(); MimeMessage message = javaMailSender.createMimeMessage();
@ -156,8 +156,10 @@ public class MailSenderHelper {
// 设置抄送人 // 设置抄送人
helper.setCc(emailSend.getCcParam().split(",")); helper.setCc(emailSend.getCcParam().split(","));
// 邮件添加附件 // 邮件添加附件
MultipartFile file = emailSend.getFile(); MultipartFile[] files = emailSend.getFile();
helper.addAttachment(Objects.requireNonNull(file.getOriginalFilename()), file); for (MultipartFile file : files) {
helper.addAttachment(Objects.requireNonNull(file.getOriginalFilename()), file);
}
// 发送邮件 // 发送邮件
javaMailSender.send(message); javaMailSender.send(message);

View File

@ -0,0 +1,4 @@
package cn.bunny.module.minio.utils;
public class MinioUtil {
}

View File

@ -2,7 +2,7 @@ package cn.bunny.service.service.impl;
import cn.bunny.entity.email.EmailSend; import cn.bunny.entity.email.EmailSend;
import cn.bunny.entity.email.EmailSendInit; import cn.bunny.entity.email.EmailSendInit;
import cn.bunny.module.mail.utils.MailSenderHelper; import cn.bunny.module.mail.utils.MailSenderUtil;
import cn.bunny.service.service.EmailService; import cn.bunny.service.service.EmailService;
import jakarta.mail.MessagingException; import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -24,8 +24,8 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setPort(465); emailSendInit.setPort(465);
emailSendInit.setUsername("3324855376@qq.com"); emailSendInit.setUsername("3324855376@qq.com");
emailSendInit.setPassword("axyqbvfuxkdqdaai"); emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit); MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
mailSenderHelper.sendSimpleEmail(emailSend); mailSenderUtil.sendSimpleEmail(emailSend);
} }
/** /**
@ -43,8 +43,8 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setUsername("3324855376@qq.com"); emailSendInit.setUsername("3324855376@qq.com");
emailSendInit.setPassword("axyqbvfuxkdqdaai"); emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit); MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
mailSenderHelper.sendEmail(emailSend); mailSenderUtil.sendEmail(emailSend);
return true; return true;
} catch (MessagingException e) { } catch (MessagingException e) {
return false; return false;
@ -66,8 +66,8 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setUsername("3324855376@qq.com"); emailSendInit.setUsername("3324855376@qq.com");
emailSendInit.setPassword("axyqbvfuxkdqdaai"); emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit); MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
mailSenderHelper.sendRichText(emailSend, true); mailSenderUtil.sendRichText(emailSend, true);
return true; return true;
} catch (MessagingException e) { } catch (MessagingException e) {
return false; return false;
@ -88,8 +88,8 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setUsername("3324855376@qq.com"); emailSendInit.setUsername("3324855376@qq.com");
emailSendInit.setPassword("axyqbvfuxkdqdaai"); emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit); MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
mailSenderHelper.sendCC(emailSend, true); mailSenderUtil.sendCC(emailSend, true);
return true; return true;
} catch (MessagingException e) { } catch (MessagingException e) {
return false; return false;