<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
docker run -di --name htb_rabbit -e RABBITMQ_DEFAULT_USER=xxx -e RABBITMQ_DEFAULT_PASS=xxx -p 15672:15672 -p 5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
命令解释:上述的命令创建了一个 rabbitmq 容器
-d 后台运行的守护容器
-i 启动容器
–name 容器的名称
-e 设置容器的环境变量
用户名
密码
-p 映射一些端口给rabbit mq 使用
前面是 暴露的外部宿主机的端口,后面是容器内部使用的端口,一般保持一致即可
rabbitmq:management
镜像名称
选择 15672 端口
宿主机的IP+ 端口号
http://192.168.xxx.xxx:15672/
输入刚刚配置的用户名和密码,即可登录
spring:
rabbitmq:
port: 5672
username: admin
password: admin
host: 192.168.xxx.xxx
mail:
username: 邮箱地址
password: 授权码,可以百度
host: smtp.xxx.com
default-encoding: UTF-8
jndi-name: mail/Session
授权码参考
https://www.jianshu.com/p/59d15b357201
package com.htb.beidawebspringboot16rabbitmq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description:初始化rabbitmq邮箱队列和交换器的配置类
* @Author 16221
* @Date 2020/5/8
**/
@Configuration
public class MsgConfig {
/**
* 创建对列
* @return
*/
@Bean
public Queue emailQueue() {
return new Queue("emailQueue");
}
/**
* 创建交换器
* @return
*/
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("emailFanoutExchange");
}
/**
* 创建绑定关系
* @param queue
* @param fanoutExchange
* @return
*/
@Bean
public Binding binding(Queue queue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queue).to(fanoutExchange);
}
}
package com.htb.beidawebspringboot16rabbitmq.msg;
import com.htb.beidawebspringboot16rabbitmq.entity.User;
import com.htb.beidawebspringboot16rabbitmq.utils.SendEmailUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @Description:
* @Author 16221
* @Date 2020/5/8
**/
@Component
public class MsgListener {
@Resource
private SendEmailUtil sendEmailUtil;
@RabbitListener(queues = "emailQueue")
public void getSendMsg(User user) {
String content = "您正在注册天珩的网站
\n" +
"\t\t您注册的id是 "
+ user.getId() + "\n" +
"\t\t您注册的邮箱是 "
+ user.getEmail() + "\n" +
"\t\t请点击下方的连接激活邮箱
\n" +
" + user.getId() + "\"" + ">激活邮箱";
sendEmailUtil.sendHtmlMail(user.getEmail(), "注册", content);
System.out.println("RabbitListener " + user.toString());
}
}
@RabbitListener(queues = “emailQueue”)
本类的逻辑是 监听的是 emailQueue 队列的变化,消费队列里面的消息,在监听到变化时候,说明有新的消息进入,需要消费消息,发送注册邮件
sendEmailUtil.sendHtmlMail(user.getEmail(), "注册", content);
参考文章
https://www.jianshu.com/p/59d15b357201
package com.htb.beidawebspringboot16rabbitmq.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @Description:
* @Author 16221
* @Date 2020/5/8
**/
@Component
public class SendEmailUtil {
/**
* 使用@Value注入application.properties中指定的用户名
*/
@Resource
@Value("${spring.mail.username}")
private String from;
@Resource
private JavaMailSender mailSender;
/**
* 发送支持html格式的邮件
*
* @param to
* @param subject
* @param content
*/
public void sendHtmlMail(String to, String subject, String content) {
//使用MimeMessage,MIME协议
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
//MimeMessageHelper帮助我们设置更丰富的内容
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
//true代表支持html
helper.setText(content, true);
mailSender.send(message);
System.out.println("发送HTML邮件成功");
} catch (javax.mail.MessagingException e) {
System.out.println("发送HTML邮件失败:" + e);
e.printStackTrace();
}
}
/**
* 发送简单的文本格式的邮件
*
* @param to
* @param subject
* @param content
*/
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
//收信人
message.setTo(to);
//主题
message.setSubject(subject);
//内容
message.setText(content);
//发信人
message.setFrom(from);
mailSender.send(message);
}
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filePath
*/
public void sendAttachmentMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
//true代表支持多组件,如附件,图片等
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
//添加附件,可多次调用该方法添加多个附件
helper.addAttachment(fileName, file);
mailSender.send(message);
System.out.println("发送带附件邮件成功");
} catch (javax.mail.MessagingException ex) {
ex.printStackTrace();
}
}
/**
* 发送带图片的邮件
*
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
//重复使用添加多个图片
helper.addInline(rscId, res);
mailSender.send(message);
System.out.println("发送带图片邮件成功");
} catch (javax.mail.MessagingException ex) {
System.out.println("发送带图片邮件失败 " + ex);
ex.printStackTrace();
}
}
}
rabbitTemplate.convertAndSend(“emailFanoutExchange”,"",user);
接收到消息之后,就会异步的去发送邮件,这个是service层的部分的代码
成功之后,yml 配置的邮箱 可以接收到对应的邮件