添加发送邮件相关功能
This commit is contained in:
6
pom.xml
6
pom.xml
@ -110,7 +110,11 @@
|
|||||||
<version>3.10.2</version>
|
<version>3.10.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Mail -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.extend.mail.controller;
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
||||||
|
import com.chinaunicom.mall.ebtp.extend.mail.entity.MailRequest;
|
||||||
|
import com.chinaunicom.mall.ebtp.extend.mail.service.IMailService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
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 javax.annotation.Resource;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/v1/mails")
|
||||||
|
@Api(tags = "邮件服务API")
|
||||||
|
public class MailController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IMailService mailService;
|
||||||
|
|
||||||
|
@PostMapping("/simple")
|
||||||
|
@ApiOperation(value = "发送简单文本邮件", notes = "发送纯文本格式的邮件")
|
||||||
|
public BaseResponse<String> sendSimpleMail(@RequestBody MailRequest request) {
|
||||||
|
try {
|
||||||
|
mailService.sendSimpleMail(request.getTo(), request.getSubject(), request.getContent());
|
||||||
|
return BaseResponse.success("邮件发送成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
return BaseResponse.fail("邮件发送失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/html")
|
||||||
|
@ApiOperation(value = "发送HTML格式邮件", notes = "发送HTML格式的邮件,支持富文本内容")
|
||||||
|
public BaseResponse<String> sendHtmlMail(@RequestBody MailRequest request) {
|
||||||
|
try {
|
||||||
|
mailService.sendHtmlMail(request.getTo(), request.getSubject(), request.getContent());
|
||||||
|
return BaseResponse.success("HTML邮件发送成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
return BaseResponse.fail("HTML邮件发送失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.extend.mail.dao;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.chinaunicom.mall.ebtp.extend.mail.entity.MailLog;
|
||||||
|
|
||||||
|
public interface MailLogMapper extends BaseMapper<MailLog> {
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.extend.mail.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("t_mail_log")
|
||||||
|
public class MailLog {
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
private String mailFrom;
|
||||||
|
private String mailTo;
|
||||||
|
private String subject;
|
||||||
|
private String content;
|
||||||
|
private Date sendTime;
|
||||||
|
private Integer status;
|
||||||
|
private String errorMsg;
|
||||||
|
private Boolean isHtml;
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.extend.mail.entity;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("邮件请求参数")
|
||||||
|
public class MailRequest {
|
||||||
|
@ApiModelProperty(value = "收件人邮箱", required = true, example = "recipient@example.com")
|
||||||
|
private String to;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "邮件主题", required = true, example = "测试邮件")
|
||||||
|
private String subject;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "邮件内容", required = true,
|
||||||
|
example = "这是一封测试邮件")
|
||||||
|
private String content;
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.extend.mail.service;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
|
||||||
|
public interface IMailService {
|
||||||
|
/**
|
||||||
|
* 发送简单文本邮件
|
||||||
|
* @param to 收件人邮箱
|
||||||
|
* @param subject 邮件主题
|
||||||
|
* @param text 邮件内容
|
||||||
|
*/
|
||||||
|
void sendSimpleMail(String to, String subject, String text);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送HTML格式邮件
|
||||||
|
* @param to 收件人邮箱
|
||||||
|
* @param subject 邮件主题
|
||||||
|
* @param htmlContent HTML格式的邮件内容
|
||||||
|
* @throws MessagingException 邮件发送异常
|
||||||
|
*/
|
||||||
|
void sendHtmlMail(String to, String subject, String htmlContent) throws MessagingException;
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.extend.mail.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.chinaunicom.mall.ebtp.extend.mail.dao.MailLogMapper;
|
||||||
|
import com.chinaunicom.mall.ebtp.extend.mail.entity.MailLog;
|
||||||
|
import com.chinaunicom.mall.ebtp.extend.mail.service.IMailService;
|
||||||
|
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.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MailServiceImpl extends ServiceImpl<MailLogMapper, MailLog> implements IMailService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private JavaMailSender mailSender;
|
||||||
|
@Resource
|
||||||
|
private MailLogMapper mailLogMapper;
|
||||||
|
|
||||||
|
@Value("${spring.mail.username}")
|
||||||
|
private String from;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void sendSimpleMail(String to, String subject, String text) {
|
||||||
|
MailLog log = createLog(to, subject, text, false);
|
||||||
|
try {
|
||||||
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
|
message.setFrom(from);
|
||||||
|
message.setTo(to);
|
||||||
|
message.setSubject(subject);
|
||||||
|
message.setText(text);
|
||||||
|
mailSender.send(message);
|
||||||
|
updateLogStatus(log.getId(), 1, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
updateLogStatus(log.getId(), 2, e.getMessage());
|
||||||
|
throw new RuntimeException("邮件发送失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void sendHtmlMail(String to, String subject, String htmlContent) throws MessagingException {
|
||||||
|
MailLog log = createLog(to, subject, htmlContent, true);
|
||||||
|
try {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||||
|
helper.setFrom(from);
|
||||||
|
helper.setTo(to);
|
||||||
|
helper.setSubject(subject);
|
||||||
|
helper.setText(htmlContent, true);
|
||||||
|
mailSender.send(message);
|
||||||
|
updateLogStatus(log.getId(), 1, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
updateLogStatus(log.getId(), 2, e.getMessage());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private MailLog createLog(String to, String subject, String content, boolean isHtml) {
|
||||||
|
MailLog log = new MailLog();
|
||||||
|
log.setMailFrom(from);
|
||||||
|
log.setMailTo(to);
|
||||||
|
log.setSubject(subject);
|
||||||
|
log.setContent(content);
|
||||||
|
log.setSendTime(new Date());
|
||||||
|
log.setStatus(0);
|
||||||
|
log.setIsHtml(isHtml);
|
||||||
|
mailLogMapper.insert(log);
|
||||||
|
return log;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateLogStatus(Long id, int status, String errorMsg) {
|
||||||
|
MailLog log = new MailLog();
|
||||||
|
log.setId(id);
|
||||||
|
log.setStatus(status);
|
||||||
|
log.setErrorMsg(errorMsg);
|
||||||
|
mailLogMapper.updateById(log);
|
||||||
|
}
|
||||||
|
}
|
@ -76,7 +76,7 @@ spring:
|
|||||||
redis:
|
redis:
|
||||||
sentinel:
|
sentinel:
|
||||||
master: mymaster
|
master: mymaster
|
||||||
# nodes: 10.125.164.124:32718, 10.125.164.118:32716, 10.125.164.121:32716
|
# nodes: 10.60.161.59:26379, 10.60.161.59:26380, 10.60.161.59:26381
|
||||||
nodes: localhost:26379, localhost:26380, localhost:26381
|
nodes: localhost:26379, localhost:26380, localhost:26381
|
||||||
password: pass
|
password: pass
|
||||||
database:
|
database:
|
||||||
@ -85,6 +85,30 @@ spring:
|
|||||||
cache: 1
|
cache: 1
|
||||||
userinfo: 1
|
userinfo: 1
|
||||||
|
|
||||||
|
# 邮件配置
|
||||||
|
mail:
|
||||||
|
# host: smtp.126.com
|
||||||
|
# username: clc0820@126.com
|
||||||
|
# password: WGZnauWK9RXHYQcJ
|
||||||
|
host: mailtest.coscoshipping.com
|
||||||
|
username: bidding.test@coscoshipping.com
|
||||||
|
password: WBksH6GjtBBV7q6e
|
||||||
|
port: 465
|
||||||
|
default-encoding: UTF-8
|
||||||
|
properties:
|
||||||
|
mail:
|
||||||
|
smtp:
|
||||||
|
auth: true
|
||||||
|
starttls:
|
||||||
|
enable: true
|
||||||
|
# required: true
|
||||||
|
ssl:
|
||||||
|
enable: true
|
||||||
|
trust: mailtest.coscoshipping.com
|
||||||
|
# checkServerIdentity: false
|
||||||
|
socketFactory:
|
||||||
|
class: javax.net.ssl.SSLSocketFactory
|
||||||
|
|
||||||
mybatis-plus:
|
mybatis-plus:
|
||||||
configuration:
|
configuration:
|
||||||
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
|
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
|
||||||
|
Reference in New Issue
Block a user