springboot整合网易云邮箱
时间: 2025-04-20 14:29:21 浏览: 31
### Spring Boot 整合网易云邮箱发送邮件
#### 准备工作
为了使Spring Boot应用程序能够通过网易云邮箱发送电子邮件,在配置文件中需设置相应的SMTP服务器信息。由于网易云邮箱默认关闭了IMAP/SMTP以及POP3/SMTP服务,因此需要先登录网页版邮箱并开启这些服务[^3]。
#### 添加依赖
在项目的`pom.xml`文件里加入Mail Starter依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
#### 配置application.properties或application.yml
对于`.properties`格式的配置如下所示:
```properties
spring.mail.host=smtp.163.com
spring.mail.port=465
[email protected]
spring.mail.password=your_authorization_code # 授权码而非密码
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.default-encoding=UTF-8
```
如果是采用YAML格式,则可以这样写:
```yaml
spring:
mail:
host: smtp.163.com
port: 465
username: [email protected]
password: 'your_authorization_code' # 使用授权码而不是账户密码
protocol: smtps
properties:
mail:
smtp:
auth: true
socketFactory:
class: javax.net.ssl.SSLSocketFactory
default-encoding: UTF-8
```
注意这里使用的是SSL加密连接方式(`mail.smtp.socketFactory.class`),并且指定了端口号为465,这是用于安全传输的标准端口之一;同时启用了身份验证机制(`mail.smtp.auth`),确保只有合法用户才能发送邮件。
#### 编写Java代码实现邮件发送功能
定义一个简单的控制器来测试邮件发送的功能:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RestController
public class MailController {
private final JavaMailSender mailSender;
@Autowired
public MailController(JavaMailSender mailSender){
this.mailSender = mailSender;
}
/**
* 测试简单文本邮件发送.
*/
@GetMapping("/sendSimpleEmail")
public String sendSimpleEmail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("[email protected]");
message.setTo("[email protected]");
message.setSubject("主题:这是一封来自SpringBoot应用的测试信件!");
message.setText("正文内容...");
try {
mailSender.send(message);
return "成功发送了一封纯文本形式的邮件.";
} catch (Exception e) {
return "发送失败:" + e.getMessage();
}
}
/**
* 带附件的HTML格式邮件.
*/
@GetMapping("/sendHtmlEmailWithAttachment")
public String sendHtmlEmailWithAttachment() throws MessagingException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setSubject("带图片和链接的HTML邮件");
// HTML内容支持样式表、超链接等富文本特性
String htmlContent="<h2 style='color:red;'>欢迎访问我们的网站!</h2><br/>"
+"点击下方按钮立即注册:<a href='http://example.com/register'>现在就去注册</a>";
helper.setText(htmlContent,true);
File file=new File("path/to/file.txt"); // 替换成实际路径下的文件名
FileSystemResource resource = new FileSystemResource(file);
helper.addAttachment(resource.getFilename(),resource);
try {
mailSender.send(mimeMessage);
return "已成功发送一封带有附件的HTML格式邮件";
}catch(Exception ex){
return "发送过程中出现问题:"+ex.getMessage();
}
}
}
```
上述例子展示了两种不同类型的邮件——一种是最基本的文字型消息,另一种则是包含了HTML标签及附加文档在内的复杂结构化邮件。每种情况都提供了对应的HTTP请求接口以便于调用者触发操作。
#### 启用异步处理(可选)
如果希望提高性能表现,还可以考虑启用异步执行模式以减少主线程等待时间。只需给主程序入口处加上@EnableAsync注解即可激活此特性[^2]。
```java
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
之后便可以在业务逻辑层面上利用`CompletableFuture<T>`或者其他并发工具包来进行非阻塞式的任务调度。
阅读全文
相关推荐












