spring boot 邮件服务

application.properties文件中添加如下配置:

(这里分别提供了qq邮箱和新浪邮箱的配置方法)

#spring.mail.host=smtp.qq.com
#spring.mail.port=465
#spring.mail.protocol=smtps
#spring.mail.default-encoding=UTF-8
#spring.mail.username=你要用来发送邮件的qq邮箱@qq.com
#spring.mail.password=你的授权码   #这里并不是自己的邮箱密码而是开启POP3/IMAP/SMTP/Exchange/CardDAV 服务 给你生成的授权码
#spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true
#spring.mail.properties.mail.smtp.ssl.trust=smtp.qq.com
spring.mail.host=smtp.sina.com
spring.mail.port=465
spring.mail.protocol=smtps
spring.mail.username=你要用来发送邮件的新浪邮箱@sina.com
spring.mail.password=你的授权码  #这里并不是自己的邮箱密码而是开启POP3/SMTP服务 给你生成的授权码
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.sina.com

pom.xml文件中引入如下Meven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

utils包下新建MailService类用于发送邮件

package com.example.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.Random;

/**
 * Created by Intellij IDEA.
 *
 * @Author hao-sen
 * Date:  2023/06/17
 */


public class MailService {
    @Autowired
    private JavaMailSender mailSender;

    private static final int CAPTCHA_LENGTH = 6;

    public String generateCaptcha() {
        Random random = new Random();
        StringBuilder captcha = new StringBuilder();
        for (int i = 0; i < CAPTCHA_LENGTH; i++) {
            captcha.append(random.nextInt(10));
        }
        return captcha.toString();
    }

    public void sendCaptcha(String to) {
        // 生成验证码
        String captcha = generateCaptcha();

        // 发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("配置文件中用来发送邮件的邮箱");//也就是spring.mail.username=等号后边的邮箱
        message.setTo(to);
        message.setSubject("验证码");
        message.setText("您的验证码是:" + captcha);
        mailSender.send(message);
    }
}

Controller类中的使用


```java
package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.User;
import com.example.service.IUserService;
import com.example.utils.MailService;
import com.example.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.entity.ResponseEntity;
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.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author lzq
 * @since 2023-06-17
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private MailService mailService;

    @PostMapping("/sendEmail")
    public ResponseEntity<String> sendEmail(@RequestBody User user, HttpSession httpSession) {
        /**获取邮箱*/
        String email = user.getEmail();
        if (StringUtils.isNotEmpty(email)) {
            /**生成随机的6位验证码*/
            String code = mailService.generateCaptcha();
            log.info("code={}", code);
            /**调用MailService中的方法发送邮件*/
            mailService.sendCaptcha(email,code);
            /**需要将生成的验证码保存到Session*/
            httpSession.setAttribute(email, code);

            return ResponseEntity.success("短信发送成功");

        }
        return ResponseEntity.error("短信发送失败");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值