zoukankan      html  css  js  c++  java
  • springboot发送邮箱验证码

    原博客链接: https://www.cnblogs.com/raicho/p/11490933.html

    导包

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

    创建java类

    结构是这样的

    image-20210722133030587

    MailService接口
    package com.zq.email.service;
     public interface MailService {
        /**
         * 发送邮件
         *
         * @param to         邮件收件人
         * @param subject    邮件主题
         * @param verifyCode 邮件验证码
         */
        void sendMail(String receive, String subject, String verifyCode);
    }
    MailService实现类
    package com.zq.email.service.iml;
    import com.zq.email.service.MailService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Service;
    

    @Service
    public class MailServiceIml implements MailService {

    @Autowired
    private JavaMailSender sender;
    
    @Value("${mail.fromMail.addr}")
    private String from;
    
    
    /**
     * @param receive    邮件收件人
     * @param subject    邮件主题
     * @param verifyCode 邮件验证码
     */
    @Override
    public void sendMail(String receive, String subject, String verifyCode){
    
        SimpleMailMessage message = new SimpleMailMessage();
    
        // 主题
        message.setSubject(subject);
    
        // 文本
        message.setText("暗号是:" + verifyCode);
    
        // 发件人
        message.setFrom(from);
    
        // 收件人
        message.setTo(receive);
    
        // 发送
        sender.send(message);
    }
    

    }

    MailController
    package com.zq.email.controller;
    

    import com.zq.email.service.iml.MailServiceIml;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RequestMapping("/mail")
    public class MailController {

    @Autowired
    private MailServiceIml mailSender;
    
    @RequestMapping("/send")
    public String mailSend() {
        mailSender.sendMail("ovo.sk@qq.com", "请回暗号", "5201314");
        return "发送成功";
    }
    

    }

    测试方法一

    • 效果图1

    image-20210722134336521

    • 效果图2

    image-20210722134413009

    测试方法二

    启动项目:访问 http://localhost:8080/mail/send

    • 效果图1

    image-20210722135331314

    • 效果图2

    image-20210722134413009

    源码地址:https://gitee.com/kk-dad/email/tree/main

  • 相关阅读:
    node
    github
    [模块] pdf转图片-pdf2image
    python 15 自定义模块 随机数 时间模块
    python 14 装饰器
    python 13 内置函数II 匿名函数 闭包
    python 12 生成器 列表推导式 内置函数I
    python 11 函数名 迭代器
    python 10 形参角度 名称空间 加载顺序
    python 09 函数参数初识
  • 原文地址:https://www.cnblogs.com/qqkkOvO/p/15043740.html
Copyright © 2011-2022 走看看