Springboot整合邮箱验证码

首先添加maven依赖包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.4</version>
</dependency>

写一下Eamil类

import org.apache.commons.mail.*;

public class Email {

    /**
     * @describe 发送内容为简单文本的邮件
     * @throws EmailException
     */
    public static void sendSimpleTextEmail(String email_text) throws EmailException {
        SimpleEmail email = new SimpleEmail();

        //设置主机名,QQ邮箱是"smtp.qq.com",网易邮箱是"smtp.163.com"
        email.setHostName("smtp.163.com");

        // 用户名和密码为邮箱的账号和授权码(不需要进行base64编码)
        email.setAuthenticator(new DefaultAuthenticator("xxxxxxxx@163.com", "此处写授权码"));
        //设置SSL连接,这样写就对了
        email.setSSLOnConnect(true);
        //设置来源,就是发送方的邮箱地址
        email.setFrom("xxxxxxxx@163.com");
        //设置主题,可以不设置
        email.setSubject("java发送邮件");
        //设置信息,就是内容,这个必须要有
        email.setMsg("这是测试邮件 ... :-)");
        //接收人邮箱地址
        email.addTo("2074296104@qq.com");
        email.send();
    }

}

测试图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中集成邮箱验证码功能,你可以按照以下步骤进行操作: 1. 在项目的pom.xml文件中添加mail模块的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 这样就可以使用Spring Boot提供的邮件功能了。\[1\] 2. 在配置文件(application.properties或application.yml)中填写相关的邮箱配置,例如使用163邮箱: ```properties # Mail spring.mail.host=smtp.163.com spring.mail.username=your-email@163.com spring.mail.password=your-password spring.mail.default-encoding=UTF-8 mail.from=your-email@163.com ``` 其中,`spring.mail.host`是SMTP服务器地址,`spring.mail.username`和`mail.from`是你的邮箱地址,`spring.mail.password`是你的邮箱授权码(不是邮箱密码)。你需要在相关邮箱设置中开启SMTP服务,并获取授权码。\[2\]\[3\] 3. 在你的代码中使用JavaMailSender发送邮件,可以通过注入`JavaMailSender`对象来实现: ```java @Autowired private JavaMailSender javaMailSender; public void sendVerificationCode(String email, String code) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(email); message.setSubject("验证码"); message.setText("您的验证码是:" + code); javaMailSender.send(message); } ``` 以上代码示例中,`sendVerificationCode`方法用于发送验证码邮件,其中`email`是收件人邮箱地址,`code`是验证码内容。你可以根据实际需求自定义邮件的主题和内容。 这样,你就可以在Spring Boot中集成邮箱验证码功能了。记得替换相关配置为你自己的邮箱信息。 #### 引用[.reference_title] - *1* *2* *3* [Spring Boot 整合163或者qq邮箱发送验证码](https://blog.csdn.net/hghjgkjn/article/details/125952509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值