SpringBoot框架实现邮件发送(上)


前言

实现登录注册功能的时候,一些软件总是要手机号验证码或者邮件验证码,手机号验证码功能的实现是需要付费使用的,而且也比较容易搭建,例如阿里云的短信验证码服务,有说明文档给我们参考,比较容易实现。


1 邮件发送类依赖导入

springboot集成了邮件发送的类,Thymeleaf是为了邮件发送的HTML模板而导入的,在pom.xml导入如下依赖。

        <!--springboot邮件发送-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2 配置发件邮箱的信息

邮箱以网易邮箱为例,登录之后点击设置,选择POP3/SMTP/IMAP,将POP3/SMTP服务开启,然后新增授权码,根据网页提示新增完毕将授权码复制保存起来。根据网易邮箱的提示(如下),可以知道其SMTP服务器为smtp.163.com。QQ邮箱也可以的。

服务器地址:
POP3服务器: pop.163.com
SMTP服务器: smtp.163.com
IMAP服务器: imap.163.com
安全支持:
POP3/SMTP/IMAP服务全部支持SSL连接

根据自己的信息将下面的邮箱服务器地址,邮箱地址,授权码进行修改,链接协议建议使用smtp,较为可靠。

  # 邮箱发送邮件配置
  mail:
    # 邮箱服务器地址
    host: smtp.163.com
    # 邮箱地址
    username: xxxx@163.com
    # 邮箱授权码
    password: xxxx
    # 编码格式
    default-encoding: UTF-8
    # 连接协议
    protocol: smtp
    # 环境配置
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

3 邮件接收类

将邮件的接收者的信息集成为一个邮件接收类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MailBean implements Serializable {
    /**
     * 邮件接受者
     */
    private String toEmail;

    /**
     * 邮件主题
     */
    private String subject;

    /**
     * 验证码
     */
    private String code;
}

4 邮件发送工具类

import com.example.common.CustomException;
import com.example.pojo.MailBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 发送邮件工具类
 * 
 */
@Component
@Slf4j
public class MailUtils {
    /**
     * 邮件发送者
     */
    @Value("${spring.mail.username}")
    private String MAIL_SENDER;

    /**
     * 注入发送邮件bean
     */
    @Resource
    private JavaMailSender mailSender;
}

创建完MailUtils工具类后,就可以在里面创建发送邮件的方法了,例如

    /**
     * 邮箱发送纯文本
     *
     * @param mailBean 接收邮件
     */
    public void sendText(MailBean mailBean) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(MAIL_SENDER);
        mailMessage.setTo(mailBean.getToEmail());
        mailMessage.setSubject(mailBean.getSubject());
        mailMessage.setText(mailBean.getCode());
        mailSender.send(mailMessage);
    }

该方法就是直接发送纯文本验证码的邮件过去。


接下文:SpringBoot框架实现邮件发送(下)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架。它提供了许多开箱即用的功能,包括邮件发送。下面是使用Spring Boot实现邮件发送的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Boot的邮件依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮件信息:在application.properties或application.yml文件中配置邮件服务器的相关信息,例如: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=your-email@example.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建邮件服务类:创建一个邮件服务类,用于发送邮件。可以使用JavaMailSender来发送邮件,例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 4. 调用邮件服务类:在需要发送邮件的地方调用邮件服务类的sendEmail方法,例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { emailService.sendEmail("[email protected]", "Test Email", "This is a test email."); return "Email sent successfully."; } } ``` 以上就是使用Spring Boot实现邮件发送的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

czxboys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值