SpringBoot发送邮件

2021-04-11 02:25

阅读:366

标签:nbsp   value   pass   sum   @param   验证   消费者   autowire   container   

通过RabbitMQ来向邮箱随机发送一个验证码

1、导入相关依赖;


        org.springframework.boot
            spring-boot-starter-mail
        

2、准备一个邮箱服务器,QQ邮箱或者163邮箱单都可以;

开启相关权限:QQ邮箱==》设置==》账户==》开启服务

技术图片

3、java mail

1)配置文件中配置相关配置;

application.yml

spring:
  mail:
    username: **********@qq.com  #邮箱账号
    password: dfydrlvrnhxyhdia   #密码
    host: smtp.qq.com    

2)写一个邮箱的工具类,全部整合好了,只需要配置邮箱,密码即可;

MailService .java
package com.seecen.redis.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.List;

/**
 * 描述:
 *
 * @author bigpeng
 * @create 2019-12-24 17:39
 */
@Service("mailService")
public class MailService {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 发送简单邮件
     * @param to
     * @param title
     * @param content
     */
    public void sendSimpleMail(String to,String title,String content){
//邮件消息对象
try { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from);//发送者 message.setTo(to);//接受者 message.setSubject(title);//主题,标题 message.setText(content);//内容 mailSender.send(message); logger.info("邮件发送成功"); } catch (MailException e) { logger.error("邮件发送失败",e); } } /** * 发送带附件的邮件 * @param to * @param title * @param cotent * @param fileList */ public void sendAttachmentsMail(String to, String title, String cotent, List fileList){ MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(from); helper.setTo(to); helper.setSubject(title); helper.setText(cotent); String fileName = null; for (File file:fileList) { fileName = MimeUtility.encodeText(file.getName(), "GB2312", "B"); helper.addAttachment(fileName, file); } } catch (Exception e) { e.printStackTrace(); } mailSender.send(message); logger.info("邮件发送成功"); } }

3)写一个消费者;

EmailConsumer.java
package com.seecen.redis.rabbitmq;


import com.seecen.redis.service.MailService;
import io.netty.util.internal.StringUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;

import java.util.Map;

@Component
public class EmailConsumer {

    @Autowired
    private MailService mailService;
    /**
     * 监听对应的队列,有消息时,会触发该方法
     * @param message 用来接收消息内容时,需要与发送时的数据类型一样
     */
    @RabbitListener(queues = {"email.verifyCode"},
                     containerFactory = "rabbitListenerContainerFactory")
    public void sendVerifyCodeEmail(Map message){
        //todo 发送邮件验证码
        System.out.println("接收到消息:"+message);
        String email=message.get("email");
        if (!StringUtils.isEmpty(email)) {
            mailService.sendSimpleMail(email,
                    "xx网注册验证码",
                    "您的注册验证码为:" + message.get("code"));
        }
    }
}

4)测试结果:

技术图片

请求地址:localhost:8888/admin/sendCode?email=*******@qq.com

 

SpringBoot发送邮件

标签:nbsp   value   pass   sum   @param   验证   消费者   autowire   container   

原文地址:https://www.cnblogs.com/xie-qi/p/13363929.html


评论


亲,登录后才可以留言!