springboot发送邮件

在项目开发中,除了需要短信验证外,有时候为了节省 短信费也会使用邮件发送。在Spring项目中发送邮件需要封装复杂的消息体,不太方便。而在Spring Boot项目中发送邮件就太简单了,下面一起来看看Spring Boot如何发送邮件。

本文以163邮箱为例进行邮件发送功能,其他邮箱的配置也都大同小异。

1. 获取授权码
常用的电子协议有POP3,SMTP,IMAP,协议的具体区别就不进行详细介绍了。这里选择smtp协议进行演示。登录邮箱,在设置中找到协议地址,点击开启。授权码只会显示一次,需要保存好。

下面是163邮箱对应的三种协议主机地址:

2. 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

3. 配置邮箱信息
需要注意的是password不是邮箱登录密码,而是第一步中获取的授权码。

spring 
  mail:
    default-encoding: utf-8
    # 主机地址
    host: smtp.163.com
    # 邮箱名
    username: ***@163.com
    # 授权码(不是密码)
    password: BSMXTXQAMWATBZGQ

4. 发送邮件
封装SimpleMailMessage消息内容,注入JavaMailSender调用其send()方法,完成邮件发送。其中收件人和抄送人支持多个发送,多个地址用,拼接起来完成批量发送。

@RestController
public class Email {

    @Autowired
    private JavaMailSender mailSender;
    @GetMapping("send")
    private void send(){

        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人
        message.setFrom("xxx@163.com");
        // 收件人
        message.setTo("xxx@163.com");
        // 邮件标题
        message.setSubject("Java发送的标题");
        // 邮件内容
        message.setText("你好,这是发送的内容~");
        // 抄送人
        message.setCc("xxx@qq.com");
        mailSender.send(message);
    }

5、效果

参考地址:https://blog.csdn.net/m0_37968982/article/details/109223961

参考的github 地址:

  • Github:https://github.com/binzh303/spring-boot-route/
在Spring Boot中发送邮件需要使用JavaMailSender接口来实现。以下是一个简单的示例代码: 首先,确保在你的项目中添加了相关依赖。在pom.xml文件中添加以下代码: ```xml <dependencies> <!-- Spring Boot Starter Mail --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> ``` 然后,在你的应用程序中创建一个类来发送邮件。例如,你可以创建一个名为EmailSender的类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Component; @Component public class EmailSender { @Autowired private JavaMailSender javaMailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 在上述示例中,我们使用了@Autowired注解来自动注入JavaMailSender对象,该对象是由Spring Boot自动配置提供的。 现在,你可以在你的应用程序的任何地方使用EmailSender类来发送邮件。例如,你可以在控制器中使用它: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailSender emailSender; @PostMapping("/sendEmail") public String sendEmail(@RequestBody EmailRequest emailRequest) { emailSender.sendEmail(emailRequest.getTo(), emailRequest.getSubject(), emailRequest.getText()); return "Email sent successfully!"; } } ``` 上述示例中,我们创建了一个名为EmailController的REST控制器,它接收一个包含收件人、主题和内容的EmailRequest对象,并使用EmailSender发送邮件。 请注意,你需要适当配置你的邮件服务器信息。在Spring Boot的application.properties(或application.yml)文件中添加以下配置: ```yaml spring.mail.host=your-mail-server spring.mail.port=your-mail-server-port spring.mail.username=your-username spring.mail.password=your-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 以上是一个简单的示例,你可以根据你的实际需求进行修改和扩展。希望对你有帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值