springboot如何使用outlook发送邮件

首先要登陆outlook邮箱,点击设置滑到最下面选择完整设置

在这里插入图片描述

进入后选择邮件->同步电子邮件
在这里插入图片描述

打开pop如上设置

下面是我的application.propertis设置

请填上自己的邮箱名与密码,outlook邮箱目前不需要授权码,密码就是自己的邮箱密码

记得按下面开启tls验证,不然会显示匿名用户无法通过验证

1 spring.mail.username=xxxxxxx@outlook.com
2 spring.mail.password=xxxxxxxxx
3 spring.mail.port=587
4 spring.mail.host=smtp-mail.outlook.com
5 # 设置ssl认证
6 # spring.mail.properties.mail.smtp.ssl.enable=true
7 # 设置TLS认证
8 spring.mail.properties.mail.smtp.starttls.required=true

下方是我的java代码:

//setSubject是邮件标题
//setText是邮件内容
//setTo是发送给哪个邮箱
//setFrom是从哪个邮箱发出
复制代码
 1 import org.junit.Test;
 2 import org.junit.runner.RunWith;
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.boot.test.context.SpringBootTest;
 5 import org.springframework.mail.SimpleMailMessage;
 6 import org.springframework.mail.javamail.JavaMailSenderImpl;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 @RunWith(SpringRunner.class)
10 @SpringBootTest
11 public class Springboot04TaskApplicationTests {
12 
13     @Autowired
14     JavaMailSenderImpl mailSender;
15 
16     @Test
17     public void contextLoads() {
18         SimpleMailMessage message = new SimpleMailMessage();
19         // 邮箱设置
20         message.setSubject("通知-程序测试");
21         message.setText("正在进行程序测试");
22 
23         message.setTo("xxxxxx@163.com");
24         message.setFrom("xxxxxx@outlook.com");
25 
26         mailSender.send(message);
27     }
28 
29 }

下面是pom文件:

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.atguigu</groupId>
 7     <artifactId>springboot-04-task</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>springboot-04-task</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.15.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-mail</artifactId>
36         </dependency>
37 
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-test</artifactId>
41             <scope>test</scope>
42         </dependency>
43     </dependencies>
44 
45     <build>
46         <plugins>
47             <plugin>
48                 <groupId>org.springframework.boot</groupId>
49                 <artifactId>spring-boot-maven-plugin</artifactId>
50             </plugin>
51         </plugins>
52     </build>
53 
54 
55 </project>

复制代码
运行成功:

在这里插入图片描述

转载自 https://www.cnblogs.com/renlei-213/p/9476031.html

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用SpringBoot发送邮件的示例代码,其中包括了如何发送带附件的邮件和如何开启TLS验证: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootApplication @EnableConfigurationProperties(MailProperties.class) public class MailApplication { @Autowired private JavaMailSender mailSender; @Autowired private MailProperties mailProperties; public static void main(String[] args) { SpringApplication.run(MailApplication.class, args); } public void sendMailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(mailProperties.getUsername()); helper.setTo("[email protected]"); helper.setSubject("Test email with attachment"); // 添加附件 FileSystemResource file = new FileSystemResource(new File("attachment.txt")); helper.addAttachment("attachment.txt", file); // 发送邮件 mailSender.send(message); } } ``` 在application.properties文件中添加以下配置: ``` spring.mail.username=xxxxxxx@outlook.com spring.mail.password=xxxxxxxxx spring.mail.port=587 spring.mail.host=smtp-mail.outlook.com spring.mail.properties.mail.smtp.starttls.required=true ``` 注意:在使用Outlook发送邮件时,需要开启TLS验证,否则会显示匿名用户无法通过验证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值