feat(邮件): 发送带抄送的邮件

This commit is contained in:
bunny 2024-05-07 14:34:46 +08:00
parent 0fcf84e304
commit 54870bd180
5 changed files with 51 additions and 29 deletions

View File

@ -57,7 +57,7 @@ public class MailSender {
*
* @param emailSend 邮件消息
*/
public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file) throws MessagingException {
public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file, boolean isRich) throws MessagingException {
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper
@ -68,7 +68,7 @@ public class MailSender {
// 设置邮件接受者
helper.setTo(emailSend.getSendTo());
// 设置邮件消息
helper.setText(emailSend.getMessage());
helper.setText(emailSend.getMessage(), isRich);
// 设置邮件主题
helper.setSubject(emailSend.getSubject());
// 邮件添加附件
@ -101,4 +101,30 @@ public class MailSender {
// 发送邮件
javaMailSender.send(mailMessage);
}
/**
* 发送带抄送的邮件
*
* @param emailSend 邮件消息
*/
public void sendCC(EmailSend emailSend, boolean isRich) throws MessagingException {
// 创建 MimeMessage 对象用于发送邮件富文本或者附件
MimeMessage message = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper
MimeMessageHelper helper = new MimeMessageHelper(message, true);
// 设置发送人
helper.setFrom(username);
// 设置邮件接受者
helper.setTo(emailSend.getSendTo());
// 设置邮件主题
helper.setSubject(emailSend.getSubject());
// 设置发送消息 为富文本
helper.setText(emailSend.getMessage(), isRich);
// 设置抄送人
helper.setCc(emailSend.getCcParam().split(","));
// 发送邮件
javaMailSender.send(message);
}
}

View File

@ -15,5 +15,6 @@ public class EmailSend {
private String sendTo;
private String subject;
private String message;
private String ccParam;
private File file;
}

View File

@ -8,7 +8,10 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@ -47,9 +50,9 @@ public class MailController {
@Operation(summary = "发送带抄送的邮件", description = "发送带抄送的邮件")
@PostMapping("send-cc")
public Result<String> sendCC(@RequestBody EmailSend emailSend, @RequestParam String ccParam) {
public Result<String> sendCC(@RequestBody EmailSend emailSend) {
log.info("发送带抄送的邮件");
boolean isSuccess = emailService.sendCC(emailSend, ccParam);
boolean isSuccess = emailService.sendCC(emailSend);
return isSuccess ? Result.success() : Result.error();
}
}

View File

@ -32,8 +32,7 @@ public interface EmailService {
* 发送带抄送的邮件
*
* @param emailSend 邮件消息
* @param ccParam 抄送人
* @return 是否成功
*/
boolean sendCC(EmailSend emailSend, String ccParam);
boolean sendCC(EmailSend emailSend);
}

View File

@ -45,7 +45,7 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSender mailSender = new MailSender(emailSendInit);
mailSender.sendAttachmentEmail(emailSend, file);
mailSender.sendAttachmentEmail(emailSend, file, true);
return true;
} catch (MessagingException e) {
return false;
@ -79,28 +79,21 @@ public class EmailServiceImpl implements EmailService {
* 发送带抄送的邮件
*
* @param emailSend 邮件消息
* @param ccParam 抄送人
*/
@Override
public boolean sendCC(EmailSend emailSend, String ccParam) {
// // 创建 MimeMessage 对象用于发送邮件富文本或者附件
// MimeMessage message = javaMailSender.createMimeMessage();
// // 创建 MimeMessageHelper
// MimeMessageHelper helper = new MimeMessageHelper(message, true);
//
// // 设置发送人
// helper.setFrom(emailFrom);
// // 设置邮件接受者
// helper.setTo(emailSend.getSendTo());
// // 设置邮件主题
// helper.setSubject(emailSend.getSubject());
// // 设置发送消息 为富文本
// helper.setText(emailSend.getMessage(), true);
// // 设置抄送人
// helper.setCc(ccParam.split(","));
//
// // 发送邮件
// javaMailSender.send(message);
return false;
public boolean sendCC(EmailSend emailSend) {
try {
EmailSendInit emailSendInit = new EmailSendInit();
emailSendInit.setHost("smtp.qq.com");
emailSendInit.setPort(465);
emailSendInit.setUsername("3324855376@qq.com");
emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSender mailSender = new MailSender(emailSendInit);
mailSender.sendCC(emailSend, true);
return true;
} catch (MessagingException e) {
return false;
}
}
}