Spring Boot---(3)邮件发送

博客介绍了Spring Boot邮件发送的方法,包括导入jar包、配置application.properties文件,还进行了邮件发送测试,涵盖简单文本测试和带有附件的测试,同时提醒测试时需获取验证。

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

Spring Boot 邮件发送

1.导入jar包

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

2.配置application.properties 文件

#发送者的邮箱
spring.mail.username=27553140@qq.com
#邮箱秘钥
spring.mail.password=nxnoashhoanvdfaf
#邮箱主机
spring.mail.host=smtp.qq.com
#开启qq邮箱的安全认证
spring.mail.properties.mail.smtp.ssl.enable=true

 

3.邮件发送测试

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

	@Autowired
	JavaMailSenderImpl mailSender;

	@Test
	public void contextLoads() {
		SimpleMailMessage message = new SimpleMailMessage();
		//邮件设置
		message.setSubject("通知-今晚开会");
		message.setText("今晚7:30开会");

		message.setTo("xxyyjj159357@163.com");
		message.setFrom("1787798327@qq.com");

		mailSender.send(message);
	}

	@Test
	public void test02() throws  Exception{
		//1、创建一个复杂的消息邮件
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

		//邮件设置
		helper.setSubject("通知-今晚开会");
		helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);

		helper.setTo("xxyyjj159357@163.com");
		helper.setFrom("1787798327@qq.com");

		//上传文件
		helper.addAttachment("1.png",new File("D:\\Images\\1.png"));
		helper.addAttachment("2.png",new File("D:\\Images\\2.png"));

		mailSender.send(mimeMessage);

	}

}

注意:
在进行测试的时候,需要获取其验证
2

简单的文本测试:
3
带有附件的测试:

4

Spring Boot Starter Mail是一个轻量级的启动器,它包含了发送电子邮件所需的基本依赖,使得在Spring Boot应用中集成邮箱功能变得更加简单。要使用这个组件,首先你需要添加`spring-boot-starter-mail`到你的项目的`pom.xml`或者`build.gradle`文件中。 以下是基本步骤: 1. 添加依赖:在Maven中,添加到`<dependencies>`标签内: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 在Gradle中,添加到`dependencies`块中: ```groovy implementation &#39;org.springframework.boot:spring-boot-starter-mail&#39; ``` 2. 配置邮件服务器:在`application.properties`或`application.yml`中设置发件人的邮箱地址、SMTP服务器信息,例如: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=myemail@example.com spring.mail.password=mypassword spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建Java配置类,如`MailSenderConfiguration`,并注入`JavaMailSender`: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; @Configuration public class MailSenderConfig { @Autowired private JavaMailSender javaMailSender; // 省略其他配置方法... } ``` 4. 实现发送邮件的Service或者Repository,调用`javaMailSender`实例的方法,比如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @Autowired private JavaMailSender mailSender; public void sendEmail(SimpleMailMessage message) { mailSender.send(message); } ``` 5. 使用`SimpleMailMessage`构造函数构建邮件内容: ```java SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo("recipient@example.com"); mailMessage.setSubject("测试邮件"); mailMessage.setText("这是一封测试邮件的内容."); sendEmail(mailMessage); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值