邮件发送
This commit is contained in:
parent
a57a39662d
commit
c2e3ca063c
|
@ -0,0 +1,72 @@
|
||||||
|
package com.hzya.frame.email;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import javax.mail.internet.MimeUtility;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName EmailUtil
|
||||||
|
* @Description
|
||||||
|
* @Author llg
|
||||||
|
* Date 2025/7/1 3:28 下午
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class EmailUtil {
|
||||||
|
@Value("${spring.mail.username}")
|
||||||
|
private String sendEmail ;
|
||||||
|
@Resource
|
||||||
|
private JavaMailSender mailSender;
|
||||||
|
|
||||||
|
public void sendHtmlMessage(String to, String subject, String htmlContent) throws MessagingException {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||||
|
helper.setFrom(sendEmail);
|
||||||
|
helper.setTo(to);
|
||||||
|
helper.setSubject(subject);
|
||||||
|
helper.setText(htmlContent, true); // 第二个参数设为true表示启用HTML格式
|
||||||
|
mailSender.send(message);
|
||||||
|
}
|
||||||
|
public void sendHtmlAndFileMessage(String to, String subject, String htmlContent, List<MultipartFile> attachments) throws MessagingException, UnsupportedEncodingException {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
// 使用MimeMessageHelper的构造函数设置multipart模式为true,并指定编码
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name());
|
||||||
|
helper.setFrom(sendEmail);
|
||||||
|
helper.setTo(to);
|
||||||
|
helper.setSubject(subject);
|
||||||
|
helper.setText(htmlContent, true); // 启用HTML格式
|
||||||
|
|
||||||
|
// 处理附件
|
||||||
|
if (attachments != null && attachments.size() > 0) {
|
||||||
|
for (int i = 0; i < attachments.size(); i++) {
|
||||||
|
if (!attachments.get(i).isEmpty()) {
|
||||||
|
// 使用MimeUtility.encodeWord解决中文附件名乱码问题
|
||||||
|
String encodedFileName = MimeUtility.encodeWord(attachments.get(i).getOriginalFilename());
|
||||||
|
helper.addAttachment(encodedFileName, attachments.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
}
|
||||||
|
public void sendSimpleMessage(String to, String subject, String text) {
|
||||||
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
|
message.setFrom(sendEmail);
|
||||||
|
message.setTo(to);
|
||||||
|
message.setSubject(subject);
|
||||||
|
message.setText(text);
|
||||||
|
mailSender.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -22,14 +22,19 @@ import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.ServletResponse;
|
import javax.servlet.ServletResponse;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Content程序唯一入口controller
|
* @Content程序唯一入口controller
|
||||||
|
@ -239,11 +244,22 @@ public class EntranceController {
|
||||||
// "测试邮件简单",
|
// "测试邮件简单",
|
||||||
// "这是一封来自Spring Boot的测试邮件"
|
// "这是一封来自Spring Boot的测试邮件"
|
||||||
//);
|
//);
|
||||||
|
String filePath = "/Users/apple/Desktop/【C4协同费控产品】(报表相关)字典说明zm@2025.pdf";
|
||||||
|
File file = new File(filePath);
|
||||||
|
List<MultipartFile> attachments = new ArrayList<>();
|
||||||
|
try (FileInputStream input = new FileInputStream(file)) {
|
||||||
|
MultipartFile multipartFile = new MockMultipartFile(
|
||||||
|
file.getName(), // 对应表单中的文件字段名
|
||||||
|
file.getName(), // 原始文件名
|
||||||
|
"application/pdf", // 或根据文件类型指定 MIME 类型
|
||||||
|
input
|
||||||
|
);
|
||||||
|
attachments.add(multipartFile);
|
||||||
|
}
|
||||||
String htmls = "<div style=\"font-family:'Arial',sans-serif;margin:20px;\"><style>table{width:100%;border-collapse:collapse;border:1px solid #ddd}th,td{padding:10px 15px;border:1px solid #ddd;text-align:left}th{background-color:#4CAF50;color:white;font-weight:bold}tr:nth-child(even){background-color:#f2f2f2}tr:hover{background-color:#e0e0e0}h2{text-align:center;font-size:24px;margin-bottom:15px;color:#333}.priority-high{background-color:#ffebee}.priority-medium{background-color:#fff3e0}.priority-low{background-color:#e8f5e9}</style><h2>任务列表</h2><table><thead><tr><th>问题标题</th><th>详细描述</th><th>预计完成时间</th></tr></thead><tbody><tr class=\"priority-medium\"><td>共享结算备注</td><td>结算单备注字段赋值</td><td>2025年7月2日</td></tr><tr class=\"priority-medium\"><td>流水状态展示调整</td><td>1、流水匹配界面,明细行匹配状态 调整为已推送<br>2、查询条件 已推送明源 调整为已推送</td><td>2025年7月3日</td></tr><tr class=\"priority-high\"><td>共享单据挂起后,银行流水对账界面客商有误</td><td></td><td>2025年7月4日</td></tr><tr class=\"priority-high\"><td>单据在审核中心 详细信息默认不自动带出</td><td>审批界面表体明细数据未自动展开</td><td>2025年7月4日</td></tr><tr class=\"priority-medium\"><td>单据传输失败后,ncc 有自由状态的单子</td><td>通过接口对接NC单据,附件上传失败后,单据未删除</td><td>2025年7月3日</td></tr><tr class=\"priority-low\"><td>对接审核状态更新优化</td><td>共享审批,取消审批,共享审核,共享复核,驳回状态回传简道云</td><td>2025年7月10日</td></tr></tbody></table></div>";
|
String htmls = "<div style=\"font-family:'Arial',sans-serif;margin:20px;\"><style>table{width:100%;border-collapse:collapse;border:1px solid #ddd}th,td{padding:10px 15px;border:1px solid #ddd;text-align:left}th{background-color:#4CAF50;color:white;font-weight:bold}tr:nth-child(even){background-color:#f2f2f2}tr:hover{background-color:#e0e0e0}h2{text-align:center;font-size:24px;margin-bottom:15px;color:#333}.priority-high{background-color:#ffebee}.priority-medium{background-color:#fff3e0}.priority-low{background-color:#e8f5e9}</style><h2>任务列表</h2><table><thead><tr><th>问题标题</th><th>详细描述</th><th>预计完成时间</th></tr></thead><tbody><tr class=\"priority-medium\"><td>共享结算备注</td><td>结算单备注字段赋值</td><td>2025年7月2日</td></tr><tr class=\"priority-medium\"><td>流水状态展示调整</td><td>1、流水匹配界面,明细行匹配状态 调整为已推送<br>2、查询条件 已推送明源 调整为已推送</td><td>2025年7月3日</td></tr><tr class=\"priority-high\"><td>共享单据挂起后,银行流水对账界面客商有误</td><td></td><td>2025年7月4日</td></tr><tr class=\"priority-high\"><td>单据在审核中心 详细信息默认不自动带出</td><td>审批界面表体明细数据未自动展开</td><td>2025年7月4日</td></tr><tr class=\"priority-medium\"><td>单据传输失败后,ncc 有自由状态的单子</td><td>通过接口对接NC单据,附件上传失败后,单据未删除</td><td>2025年7月3日</td></tr><tr class=\"priority-low\"><td>对接审核状态更新优化</td><td>共享审批,取消审批,共享审核,共享复核,驳回状态回传简道云</td><td>2025年7月10日</td></tr></tbody></table></div>";
|
||||||
emailUtil.sendHtmlMessage("number8912@qq.com",
|
emailUtil.sendHtmlAndFileMessage("number8912@qq.com",
|
||||||
"测试邮件html",
|
"测试邮件html",
|
||||||
htmls);
|
htmls,attachments);
|
||||||
|
|
||||||
return BaseResult.getSuccessMessageEntity("成功");
|
return BaseResult.getSuccessMessageEntity("成功");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue