feat(新增): 封装为空对象判断,检测邮件,邮件多附件发送
This commit is contained in:
parent
c1f4b9b40a
commit
e113d46ee5
|
@ -1,11 +1,20 @@
|
|||
package cn.bunny.common.service.utils;
|
||||
|
||||
import cn.bunny.common.service.exception.BunnyException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@Slf4j
|
||||
public class EmptyUtil {
|
||||
/**
|
||||
* 是否为空
|
||||
*
|
||||
* @param value 判断值
|
||||
* @param message 错误消息
|
||||
*/
|
||||
public static void isEmpty(Object value, String message) {
|
||||
if (value == null || !StringUtils.hasText(value.toString())) {
|
||||
log.error("为空对象错误:{},{}", value, message);
|
||||
throw new BunnyException(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
package cn.bunny.entity.email;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 邮件发送对象
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class EmailSend {
|
||||
// 给谁发送
|
||||
@NotNull(message = "发送者不能为空")
|
||||
private String sendTo;
|
||||
// 发送主题
|
||||
@NotNull(message = "主题不能为空")
|
||||
private String subject;
|
||||
// 是否为富文本
|
||||
private Boolean isRichText;
|
||||
|
@ -25,5 +25,5 @@ public class EmailSend {
|
|||
// 抄送人
|
||||
private String ccParam;
|
||||
// 发送的文件
|
||||
private MultipartFile file;
|
||||
private MultipartFile[] file;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,12 @@ import cn.bunny.common.constant.MessageConstant;
|
|||
import cn.bunny.common.service.utils.EmptyUtil;
|
||||
import cn.bunny.entity.email.EmailSend;
|
||||
|
||||
public class MailSendCheck {
|
||||
public class MailSendCheckUtil {
|
||||
/**
|
||||
* 检测发送对象是否为空的对象
|
||||
*
|
||||
* @param emailSend 邮件发送对象
|
||||
*/
|
||||
public static void check(EmailSend emailSend) {
|
||||
// 空发送对象
|
||||
EmptyUtil.isEmpty(emailSend, MessageConstant.EMPTY_SEND_OBJECT);
|
|
@ -11,14 +11,14 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.util.Objects;
|
||||
|
||||
public class MailSenderHelper {
|
||||
public class MailSenderUtil {
|
||||
private final String username;
|
||||
private final JavaMailSenderImpl javaMailSender;
|
||||
|
||||
/**
|
||||
* 初始化构造函数进行当前类赋值
|
||||
*/
|
||||
public MailSenderHelper(EmailSendInit emailSendInit) {
|
||||
public MailSenderUtil(EmailSendInit emailSendInit) {
|
||||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
|
||||
javaMailSender.setHost(emailSendInit.getHost());
|
||||
javaMailSender.setPort(emailSendInit.getPort());
|
||||
|
@ -37,7 +37,7 @@ public class MailSenderHelper {
|
|||
* @param emailSend 邮件消息
|
||||
*/
|
||||
public void sendSimpleEmail(EmailSend emailSend) {
|
||||
MailSendCheck.check(emailSend);
|
||||
MailSendCheckUtil.check(emailSend);
|
||||
// 创建邮件消息体 SimpleMailMessage 发送简单邮件
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
|
||||
|
@ -59,7 +59,7 @@ public class MailSenderHelper {
|
|||
* @param emailSend 邮件消息
|
||||
*/
|
||||
public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file, boolean isRich) throws MessagingException {
|
||||
MailSendCheck.check(emailSend);
|
||||
MailSendCheckUtil.check(emailSend);
|
||||
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
|
||||
MimeMessage mailMessage = javaMailSender.createMimeMessage();
|
||||
// 创建 MimeMessageHelper
|
||||
|
@ -86,7 +86,7 @@ public class MailSenderHelper {
|
|||
* @param emailSend 邮件消息
|
||||
*/
|
||||
public void sendRichText(EmailSend emailSend, boolean isRich) throws MessagingException {
|
||||
MailSendCheck.check(emailSend);
|
||||
MailSendCheckUtil.check(emailSend);
|
||||
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
|
||||
MimeMessage mailMessage = javaMailSender.createMimeMessage();
|
||||
// 创建 MimeMessageHelper
|
||||
|
@ -111,7 +111,7 @@ public class MailSenderHelper {
|
|||
* @param emailSend 邮件消息
|
||||
*/
|
||||
public void sendCC(EmailSend emailSend, boolean isRich) throws MessagingException {
|
||||
MailSendCheck.check(emailSend);
|
||||
MailSendCheckUtil.check(emailSend);
|
||||
// 创建 MimeMessage 对象用于发送邮件富文本或者附件
|
||||
MimeMessage message = javaMailSender.createMimeMessage();
|
||||
// 创建 MimeMessageHelper
|
||||
|
@ -138,7 +138,7 @@ public class MailSenderHelper {
|
|||
* @param emailSend 邮件消息
|
||||
*/
|
||||
public void sendEmail(EmailSend emailSend) throws MessagingException {
|
||||
MailSendCheck.check(emailSend);
|
||||
MailSendCheckUtil.check(emailSend);
|
||||
|
||||
// 创建 MimeMessage 对象用于发送邮件富文本或者附件
|
||||
MimeMessage message = javaMailSender.createMimeMessage();
|
||||
|
@ -156,8 +156,10 @@ public class MailSenderHelper {
|
|||
// 设置抄送人
|
||||
helper.setCc(emailSend.getCcParam().split(","));
|
||||
// 邮件添加附件
|
||||
MultipartFile file = emailSend.getFile();
|
||||
helper.addAttachment(Objects.requireNonNull(file.getOriginalFilename()), file);
|
||||
MultipartFile[] files = emailSend.getFile();
|
||||
for (MultipartFile file : files) {
|
||||
helper.addAttachment(Objects.requireNonNull(file.getOriginalFilename()), file);
|
||||
}
|
||||
|
||||
// 发送邮件
|
||||
javaMailSender.send(message);
|
|
@ -0,0 +1,4 @@
|
|||
package cn.bunny.module.minio.utils;
|
||||
|
||||
public class MinioUtil {
|
||||
}
|
|
@ -2,7 +2,7 @@ package cn.bunny.service.service.impl;
|
|||
|
||||
import cn.bunny.entity.email.EmailSend;
|
||||
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 jakarta.mail.MessagingException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -24,8 +24,8 @@ public class EmailServiceImpl implements EmailService {
|
|||
emailSendInit.setPort(465);
|
||||
emailSendInit.setUsername("3324855376@qq.com");
|
||||
emailSendInit.setPassword("axyqbvfuxkdqdaai");
|
||||
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit);
|
||||
mailSenderHelper.sendSimpleEmail(emailSend);
|
||||
MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
|
||||
mailSenderUtil.sendSimpleEmail(emailSend);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,8 +43,8 @@ public class EmailServiceImpl implements EmailService {
|
|||
emailSendInit.setUsername("3324855376@qq.com");
|
||||
emailSendInit.setPassword("axyqbvfuxkdqdaai");
|
||||
|
||||
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit);
|
||||
mailSenderHelper.sendEmail(emailSend);
|
||||
MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
|
||||
mailSenderUtil.sendEmail(emailSend);
|
||||
return true;
|
||||
} catch (MessagingException e) {
|
||||
return false;
|
||||
|
@ -66,8 +66,8 @@ public class EmailServiceImpl implements EmailService {
|
|||
emailSendInit.setUsername("3324855376@qq.com");
|
||||
emailSendInit.setPassword("axyqbvfuxkdqdaai");
|
||||
|
||||
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit);
|
||||
mailSenderHelper.sendRichText(emailSend, true);
|
||||
MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
|
||||
mailSenderUtil.sendRichText(emailSend, true);
|
||||
return true;
|
||||
} catch (MessagingException e) {
|
||||
return false;
|
||||
|
@ -88,8 +88,8 @@ public class EmailServiceImpl implements EmailService {
|
|||
emailSendInit.setUsername("3324855376@qq.com");
|
||||
emailSendInit.setPassword("axyqbvfuxkdqdaai");
|
||||
|
||||
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit);
|
||||
mailSenderHelper.sendCC(emailSend, true);
|
||||
MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
|
||||
mailSenderUtil.sendCC(emailSend, true);
|
||||
return true;
|
||||
} catch (MessagingException e) {
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue