Springboot发送邮件

本文详细介绍了如何在SpringBoot项目中集成邮件发送功能,包括引入依赖、配置属性、编写服务类及测试方法,解决了常见的503和504错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot对发送邮件做了很好的封装,使用起来也非常简单。

首先引入maven依赖:

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

再在appication.properties中进行一些邮件的配置:

spring.mail.host=smtp.163.com
spring.mail.username=你的邮箱名账号
spring.mail.password=你要邮箱授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

接着在业务类中实现发送邮件:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class MailService {
    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;


    public void sendMail(String title, String text, String to) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 发送人的邮箱
        message.setTo(to); //发给谁对方邮箱
        message.setSubject(title); //标题
        message.setText(text); //内容
        javaMailSender.send(message); //发送
        System.out.println("时间: " + LocalDateTime.now() + "邮件发送成功!");
    }
}

最后,是测试:

import com.xqnode.mail.service.MailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {

    @Resource
    private MailService mailService;

    @Test
    public void contextLoads() {
        mailService.sendMail("大猪蹄子", "大猪蹄子", "test@163.com");
    }
}

运行这个测试类就可以实现邮件发送。可能会遇到一些问题,例如503错误,这个可能是你的邮箱设置不对,在邮箱里开启smtp服务:
163

然后获取你的授权码填写在spring.mail.password中:

163

504错误表示可能你发送的邮件带有敏感的词汇,需要重新编辑一下。

Spring Boot发送邮件通常通过JavaMail API实现,Spring Boot提供了一个方便的集成方式,无需额外配置SMTP服务器。以下是使用Spring Boot发送邮件的基本步骤: 1. 添加依赖:在`pom.xml`文件中添加Spring Boot Actuator和JavaMail的相关依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮箱服务:在application.properties或application.yml文件中设置SMTP服务器的信息,如主机名、端口、用户名、密码等: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=your-email@example.com spring.mail.password=your-password spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建邮件消息:创建一个Java类,继承`AbstractMessageConverter`或使用`SimpleMailMessage`来构建邮件内容: ```java import org.springframework.mail.SimpleMailMessage; SimpleMailMessage message = new SimpleMailMessage(); message.setTo("recipient@example.com"); message.setFrom("sender@example.com"); message.setSubject("Hello from Spring Boot"); message.setText("This is a test email."); ``` 4. 使用Java配置或注解:在Spring Boot应用中,你可以使用Java配置类配置一个`JavaMailSender`实例,然后在需要的地方发送邮件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; @Autowired private JavaMailSender javaMailSender; public void sendEmail(SimpleMailMessage message) { javaMailSender.send(message); } ``` 或者使用`@Autowired`自动注入并在方法上使用`@SendMail`注解: ```java @RestController public class EmailController { @Autowired private JavaMailSender javaMailSender; @PostMapping("/send-email") @SendMail public ResponseEntity<String> sendMessage(SimpleMailMessage message) { // ...处理并返回响应 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员青戈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值