feat(mail): 集成邮件发送功能并配置SMTP服务
- 添加 spring-boot-starter-mail依赖 - 在 application.yml 中配置 SMTP 服务器信息 - 注入 JavaMailSender 并实现消息监听器中的邮件发送逻辑- 设置邮件发送者、接收者、主题及内容 - 使用 RabbitMQ 消息队列触发邮件通知机制
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -91,6 +91,11 @@
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.hanserwei.chat.consumer;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -11,10 +14,24 @@ import java.nio.charset.StandardCharsets;
|
||||
@Component
|
||||
public class RabbitMQConsumer {
|
||||
|
||||
@Resource
|
||||
private JavaMailSender mailSender;
|
||||
|
||||
@RabbitListener(queues = "chat.queue")
|
||||
public void receiveMessage(Object message) {
|
||||
String messageText = extractMessage(message);
|
||||
log.info("Received message text:\n{}", messageText);
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
// 配置发送者邮箱
|
||||
mailMessage.setFrom("2628273921@qq.com");
|
||||
// 配置接受者邮箱
|
||||
mailMessage.setTo("ssw010723@gmail.com");
|
||||
// 配置邮件主题
|
||||
mailMessage.setSubject("主题:及时联系客户");
|
||||
// 配置邮件内容
|
||||
mailMessage.setText(messageText);
|
||||
// 发送邮件
|
||||
mailSender.send(mailMessage);
|
||||
}
|
||||
|
||||
private String extractMessage(Object message) {
|
||||
|
||||
@@ -12,6 +12,15 @@ spring:
|
||||
jackson:
|
||||
serialization:
|
||||
write-dates-as-timestamps: false
|
||||
mail:
|
||||
host: ${MAIL_HOST:smtp.qq.com}
|
||||
port: ${MAIL_PORT:587}
|
||||
username: ${MAIL_USERNAME:2628273921@qq.com}
|
||||
password: ENC(ARrAyZNZhbaG6tebogv6WSQbtCO+Vq93NfSA6tMAiD0tTogujERVwEGBECakH0LUhYq9oTaXgfw7tonxNAFEwg==)
|
||||
properties:
|
||||
mail.smtp.auth: true
|
||||
mail.smtp.starttls.enable: true
|
||||
mail.smtp.starttls.required: true
|
||||
data:
|
||||
redis:
|
||||
host: localhost
|
||||
|
||||
Reference in New Issue
Block a user