买家端需要的获取商家主页和模糊搜索接口
This commit is contained in:
@@ -1,171 +1,171 @@
|
|||||||
package com.ai.da.common.utils;
|
package com.ai.da.common.utils;
|
||||||
|
|
||||||
import com.ai.da.common.constant.CommonConstant;
|
import com.ai.da.common.constant.CommonConstant;
|
||||||
import com.ai.da.model.dto.BasicEmailParamDTO;
|
import com.ai.da.model.dto.BasicEmailParamDTO;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import io.netty.util.internal.StringUtil;
|
import io.netty.util.internal.StringUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.InputStreamSource;
|
import org.springframework.core.io.InputStreamSource;
|
||||||
import org.springframework.mail.MailException;
|
import org.springframework.mail.MailException;
|
||||||
import org.springframework.mail.javamail.JavaMailSender;
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.thymeleaf.TemplateEngine;
|
import org.thymeleaf.TemplateEngine;
|
||||||
import org.thymeleaf.context.Context;
|
import org.thymeleaf.context.Context;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.mail.MessagingException;
|
import jakarta.mail.MessagingException;
|
||||||
import jakarta.mail.internet.*;
|
import jakarta.mail.internet.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class MailUtil {
|
public class MailUtil {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private JavaMailSender javaMailSender;
|
private JavaMailSender javaMailSender;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private TemplateEngine templateEngine;
|
private TemplateEngine templateEngine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送邮件 - 默认发件人
|
* 发送邮件 - 默认发件人
|
||||||
*
|
*
|
||||||
* @param basicEmailParamDTO 发送邮件所需参数
|
* @param basicEmailParamDTO 发送邮件所需参数
|
||||||
* @param fileName 附件名(如果有)
|
* @param fileName 附件名(如果有)
|
||||||
* @param inputStreamSource 附件(如果有)
|
* @param inputStreamSource 附件(如果有)
|
||||||
*/
|
*/
|
||||||
public int sendMail(BasicEmailParamDTO basicEmailParamDTO, String fileName, InputStreamSource inputStreamSource) throws MessagingException {
|
public int sendMail(BasicEmailParamDTO basicEmailParamDTO, String fileName, InputStreamSource inputStreamSource) throws MessagingException {
|
||||||
MimeMessage mimeMessage = createSimpleMail(basicEmailParamDTO, fileName, inputStreamSource);
|
MimeMessage mimeMessage = createSimpleMail(basicEmailParamDTO, fileName, inputStreamSource);
|
||||||
// 提取配置
|
// 提取配置
|
||||||
String host;
|
String host;
|
||||||
String username;
|
String username;
|
||||||
String password;
|
String password;
|
||||||
if (StringUtil.isNullOrEmpty(basicEmailParamDTO.getServiceAddress())) {
|
if (StringUtil.isNullOrEmpty(basicEmailParamDTO.getServiceAddress())) {
|
||||||
host = ((JavaMailSenderImpl) javaMailSender).getHost();
|
host = ((JavaMailSenderImpl) javaMailSender).getHost();
|
||||||
} else {
|
} else {
|
||||||
host = basicEmailParamDTO.getServiceAddress();
|
host = basicEmailParamDTO.getServiceAddress();
|
||||||
}
|
}
|
||||||
if (StringUtil.isNullOrEmpty(basicEmailParamDTO.getSenderUser())) {
|
if (StringUtil.isNullOrEmpty(basicEmailParamDTO.getSenderUser())) {
|
||||||
username = ((JavaMailSenderImpl) javaMailSender).getUsername();
|
username = ((JavaMailSenderImpl) javaMailSender).getUsername();
|
||||||
} else {
|
} else {
|
||||||
username = basicEmailParamDTO.getSenderUser();
|
username = basicEmailParamDTO.getSenderUser();
|
||||||
}
|
}
|
||||||
if (StringUtil.isNullOrEmpty(basicEmailParamDTO.getServiceAddress())) {
|
if (StringUtil.isNullOrEmpty(basicEmailParamDTO.getServiceAddress())) {
|
||||||
password = ((JavaMailSenderImpl) javaMailSender).getPassword();
|
password = ((JavaMailSenderImpl) javaMailSender).getPassword();
|
||||||
} else {
|
} else {
|
||||||
password = basicEmailParamDTO.getPassword();
|
password = basicEmailParamDTO.getPassword();
|
||||||
}
|
}
|
||||||
return sendMail(mimeMessage, host, username, password);
|
return sendMail(mimeMessage, host, username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int sendMail(MimeMessage mimeMessage, String host, String username, String password) throws MessagingException {
|
private int sendMail(MimeMessage mimeMessage, String host, String username, String password) throws MessagingException {
|
||||||
try {
|
try {
|
||||||
// 配置连接属性
|
// 配置连接属性
|
||||||
java.util.Properties props = mimeMessage.getSession().getProperties();
|
java.util.Properties props = mimeMessage.getSession().getProperties();
|
||||||
props.put("mail.smtp.auth", "true");
|
props.put("mail.smtp.auth", "true");
|
||||||
props.put("mail.smtp.host", host);
|
props.put("mail.smtp.host", host);
|
||||||
props.put("mail.user", username);
|
props.put("mail.user", username);
|
||||||
props.put("mail.password", password);
|
props.put("mail.password", password);
|
||||||
|
|
||||||
// 使用 JavaMailSender 发送邮件(Spring Boot 3.x 标准方式)
|
// 使用 JavaMailSender 发送邮件(Spring Boot 3.x 标准方式)
|
||||||
javaMailSender.send(mimeMessage);
|
javaMailSender.send(mimeMessage);
|
||||||
log.info("邮件发送成功至: {}", host);
|
log.info("邮件发送成功至: {}", host);
|
||||||
return 1;
|
return 1;
|
||||||
} catch (MailException e) {
|
} catch (MailException e) {
|
||||||
log.info("邮件发送失败:{}", e.getMessage());
|
log.info("邮件发送失败:{}", e.getMessage());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一封邮件
|
* 创建一封邮件
|
||||||
*
|
*
|
||||||
* @param basicEmailParamDTO 创建邮件需要的参数
|
* @param basicEmailParamDTO 创建邮件需要的参数
|
||||||
* @param inputStreamSource 附件(如果有)
|
* @param inputStreamSource 附件(如果有)
|
||||||
* @return 一封邮件
|
* @return 一封邮件
|
||||||
*/
|
*/
|
||||||
private MimeMessage createSimpleMail(BasicEmailParamDTO basicEmailParamDTO, String fileName, InputStreamSource inputStreamSource) throws MessagingException {
|
private MimeMessage createSimpleMail(BasicEmailParamDTO basicEmailParamDTO, String fileName, InputStreamSource inputStreamSource) throws MessagingException {
|
||||||
// 创建邮件对象
|
// 创建邮件对象
|
||||||
MimeMessage message = javaMailSender.createMimeMessage();
|
MimeMessage message = javaMailSender.createMimeMessage();
|
||||||
// 使用 MimeMessageHelper 简化邮件内容和附件的设置
|
// 使用 MimeMessageHelper 简化邮件内容和附件的设置
|
||||||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
|
||||||
// 设置发件人
|
// 设置发件人
|
||||||
mimeMessageHelper.setFrom(new InternetAddress(basicEmailParamDTO.getSenderUserMail()));
|
mimeMessageHelper.setFrom(new InternetAddress(basicEmailParamDTO.getSenderUserMail()));
|
||||||
// 设置收件人
|
// 设置收件人
|
||||||
mimeMessageHelper.setTo(basicEmailParamDTO.getMailTo());
|
mimeMessageHelper.setTo(basicEmailParamDTO.getMailTo());
|
||||||
// 设置抄送人
|
// 设置抄送人
|
||||||
if (basicEmailParamDTO.getCc() != null && basicEmailParamDTO.getCc().length > 0) {
|
if (basicEmailParamDTO.getCc() != null && basicEmailParamDTO.getCc().length > 0) {
|
||||||
mimeMessageHelper.setCc(basicEmailParamDTO.getCc());
|
mimeMessageHelper.setCc(basicEmailParamDTO.getCc());
|
||||||
}
|
}
|
||||||
// 设置暗送人
|
// 设置暗送人
|
||||||
if (basicEmailParamDTO.getBcc() != null && basicEmailParamDTO.getBcc().length > 0) {
|
if (basicEmailParamDTO.getBcc() != null && basicEmailParamDTO.getBcc().length > 0) {
|
||||||
mimeMessageHelper.setBcc(basicEmailParamDTO.getBcc());
|
mimeMessageHelper.setBcc(basicEmailParamDTO.getBcc());
|
||||||
}
|
}
|
||||||
// 设置邮件主题
|
// 设置邮件主题
|
||||||
mimeMessageHelper.setSubject(basicEmailParamDTO.getSubject());
|
mimeMessageHelper.setSubject(basicEmailParamDTO.getSubject());
|
||||||
// 设置邮件内容(HTML 格式)
|
// 设置邮件内容(HTML 格式)
|
||||||
mimeMessageHelper.setText(basicEmailParamDTO.getContent(), true);
|
mimeMessageHelper.setText(basicEmailParamDTO.getContent(), true);
|
||||||
// 设置附件
|
// 设置附件
|
||||||
if (inputStreamSource != null) {
|
if (inputStreamSource != null) {
|
||||||
mimeMessageHelper.addAttachment(fileName, inputStreamSource);
|
mimeMessageHelper.addAttachment(fileName, inputStreamSource);
|
||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置实体参数
|
* 设置实体参数
|
||||||
*
|
*
|
||||||
* @param mailTo 接收邮件的邮箱地址
|
* @param mailTo 接收邮件的邮箱地址
|
||||||
* @param jsonObject 模板中变量的值
|
* @param jsonObject 模板中变量的值
|
||||||
* @return 返回一个MailEntity
|
* @return 返回一个MailEntity
|
||||||
* @throws AddressException 邮箱地址值异常
|
* @throws AddressException 邮箱地址值异常
|
||||||
*/
|
*/
|
||||||
public BasicEmailParamDTO setBasicEmailParams(List<String> mailTo, JSONObject jsonObject, String templatePath, String title) throws AddressException {
|
public BasicEmailParamDTO setBasicEmailParams(List<String> mailTo, JSONObject jsonObject, String templatePath, String title) throws AddressException {
|
||||||
BasicEmailParamDTO basicEmailParamDTO = new BasicEmailParamDTO();
|
BasicEmailParamDTO basicEmailParamDTO = new BasicEmailParamDTO();
|
||||||
// basicEmailParamDTO.setSenderUserMail("info@aida.com.hk");
|
// basicEmailParamDTO.setSenderUserMail("info@aida.com.hk");
|
||||||
basicEmailParamDTO.setSenderUserMail(CommonConstant.senderEmail);
|
basicEmailParamDTO.setSenderUserMail(CommonConstant.senderEmail);
|
||||||
basicEmailParamDTO.setMailTo(getInternetAddressList(mailTo));
|
basicEmailParamDTO.setMailTo(getInternetAddressList(mailTo));
|
||||||
basicEmailParamDTO.setSubject(title);
|
basicEmailParamDTO.setSubject(title);
|
||||||
// todo 邮件模板不存在的报错与重试机制
|
// todo 邮件模板不存在的报错与重试机制
|
||||||
basicEmailParamDTO.setContent(setContent(jsonObject, templatePath));
|
basicEmailParamDTO.setContent(setContent(jsonObject, templatePath));
|
||||||
return basicEmailParamDTO;
|
return basicEmailParamDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BasicEmailParamDTO setBasicEmailParams(List<String> mailTo, String title) throws AddressException {
|
public BasicEmailParamDTO setBasicEmailParams(List<String> mailTo, String title) throws AddressException {
|
||||||
BasicEmailParamDTO basicEmailParamDTO = new BasicEmailParamDTO();
|
BasicEmailParamDTO basicEmailParamDTO = new BasicEmailParamDTO();
|
||||||
basicEmailParamDTO.setSenderUserMail("info@aida.com.hk");
|
basicEmailParamDTO.setSenderUserMail("info@aida.com.hk");
|
||||||
basicEmailParamDTO.setMailTo(getInternetAddressList(mailTo));
|
basicEmailParamDTO.setMailTo(getInternetAddressList(mailTo));
|
||||||
basicEmailParamDTO.setSubject(title);
|
basicEmailParamDTO.setSubject(title);
|
||||||
return basicEmailParamDTO;
|
return basicEmailParamDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将地址转换为InternetAddress类型
|
* 将地址转换为InternetAddress类型
|
||||||
*
|
*
|
||||||
* @param addressList 普通的地址字符串列表
|
* @param addressList 普通的地址字符串列表
|
||||||
* @return InternetAddress类型的地址列表
|
* @return InternetAddress类型的地址列表
|
||||||
* @throws AddressException 地址异常
|
* @throws AddressException 地址异常
|
||||||
*/
|
*/
|
||||||
public InternetAddress[] getInternetAddressList(List<String> addressList) throws AddressException {
|
public InternetAddress[] getInternetAddressList(List<String> addressList) throws AddressException {
|
||||||
InternetAddress[] toAddress = new InternetAddress[addressList.size()];
|
InternetAddress[] toAddress = new InternetAddress[addressList.size()];
|
||||||
for (String address : addressList) {
|
for (String address : addressList) {
|
||||||
toAddress[addressList.indexOf(address)] = new InternetAddress(address);
|
toAddress[addressList.indexOf(address)] = new InternetAddress(address);
|
||||||
}
|
}
|
||||||
return toAddress;
|
return toAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String setContent(JSONObject jsonObject, String templatePath) {
|
public String setContent(JSONObject jsonObject, String templatePath) {
|
||||||
Context context = new Context();
|
Context context = new Context();
|
||||||
if (Objects.nonNull(jsonObject)) {
|
if (Objects.nonNull(jsonObject)) {
|
||||||
for (String key : jsonObject.keySet()) {
|
for (String key : jsonObject.keySet()) {
|
||||||
context.setVariable(key, jsonObject.get(key));
|
context.setVariable(key, jsonObject.get(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return templateEngine.process(templatePath, context);
|
return templateEngine.process(templatePath, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user