feat(邮件): 发送富文本邮件

This commit is contained in:
bunny 2024-05-07 14:24:57 +08:00
parent 6ff2a0a6c4
commit 0fcf84e304
4 changed files with 69 additions and 25 deletions

View File

@ -52,6 +52,11 @@ public class MailSender {
javaMailSender.send(mailMessage);
}
/**
* 发送带附件邮件
*
* @param emailSend 邮件消息
*/
public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file) throws MessagingException {
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage();
@ -72,4 +77,28 @@ public class MailSender {
// 发送邮件
javaMailSender.send(mailMessage);
}
/**
* 发送富文本邮件
*
* @param emailSend 邮件消息
*/
public void sendRichText(EmailSend emailSend, boolean isRich) throws MessagingException {
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper
MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true);
// 设置邮件发送者
helper.setFrom(username);
// 设置邮件接受者
helper.setTo(emailSend.getSendTo());
// 设置邮件主题
helper.setSubject(emailSend.getSubject());
// 设置邮件富文本后面跟true 表示HTML格式发送
helper.setText(emailSend.getMessage(), isRich);
// 发送邮件
javaMailSender.send(mailMessage);
}
}

View File

@ -8,10 +8,7 @@ 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.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.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@ -38,4 +35,21 @@ public class MailController {
boolean isSuccess = emailService.sendAttachmentEmail(send, file);
return isSuccess ? Result.success() : Result.error();
}
@Operation(summary = "发送富文本邮件", description = "发送富文本邮件")
@PostMapping("send-rich")
public Result<String> sendRich(@RequestBody EmailSend emailSend) {
log.info("发送富文本邮件");
boolean isSuccess = emailService.sendRich(emailSend);
return isSuccess ? Result.success() : Result.error();
}
@Operation(summary = "发送带抄送的邮件", description = "发送带抄送的邮件")
@PostMapping("send-cc")
public Result<String> sendCC(@RequestBody EmailSend emailSend, @RequestParam String ccParam) {
log.info("发送带抄送的邮件");
boolean isSuccess = emailService.sendCC(emailSend, ccParam);
return isSuccess ? Result.success() : Result.error();
}
}

View File

@ -2,7 +2,6 @@ package cn.bunny.service.service;
import cn.bunny.entity.email.EmailSend;
import jakarta.mail.MessagingException;
import org.springframework.web.multipart.MultipartFile;
public interface EmailService {
@ -25,14 +24,16 @@ public interface EmailService {
* 发送富文本邮件
*
* @param emailSend 邮件消息
* @return 是否成功
*/
void sendRich(EmailSend emailSend) throws MessagingException;
boolean sendRich(EmailSend emailSend);
/**
* 发送带抄送的邮件
*
* @param emailSend 邮件消息
* @param ccParam 抄送人
* @return 是否成功
*/
void sendCC(EmailSend emailSend, String ccParam) throws MessagingException;
boolean sendCC(EmailSend emailSend, String ccParam);
}

View File

@ -43,6 +43,7 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setPort(465);
emailSendInit.setUsername("3324855376@qq.com");
emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSender mailSender = new MailSender(emailSendInit);
mailSender.sendAttachmentEmail(emailSend, file);
return true;
@ -55,25 +56,23 @@ public class EmailServiceImpl implements EmailService {
* 发送富文本邮件
*
* @param emailSend 邮件消息
* @return 是否成功
*/
@Override
public void sendRich(EmailSend emailSend) {
// // 创建 MimeMessage 对象用于发送邮件富文本或者附件
// MimeMessage message = javaMailSender.createMimeMessage();
// // 创建 MimeMessageHelper
// MimeMessageHelper helper = new MimeMessageHelper(message, true);
//
// // 设置邮件发送者
// helper.setFrom(emailFrom);
// // 设置邮件接受者
// helper.setTo(emailSend.getSendTo());
// // 设置邮件主题
// helper.setSubject(emailSend.getSubject());
// // 设置邮件富文本后面跟true 表示HTML格式发送
// helper.setText(emailSend.getMessage(), true);
//
// // 发送邮件
// javaMailSender.send(message);
public boolean sendRich(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.sendRichText(emailSend, true);
return true;
} catch (MessagingException e) {
return false;
}
}
/**
@ -83,7 +82,7 @@ public class EmailServiceImpl implements EmailService {
* @param ccParam 抄送人
*/
@Override
public void sendCC(EmailSend emailSend, String ccParam) {
public boolean sendCC(EmailSend emailSend, String ccParam) {
// // 创建 MimeMessage 对象用于发送邮件富文本或者附件
// MimeMessage message = javaMailSender.createMimeMessage();
// // 创建 MimeMessageHelper
@ -102,5 +101,6 @@ public class EmailServiceImpl implements EmailService {
//
// // 发送邮件
// javaMailSender.send(message);
return false;
}
}