云计算百科
云计算领域专业知识百科平台

谷歌邮箱SMTP服务器连接异常:MailConnectException: Couldn‘t connect to host, port: smtp.gmail.com, 587

1、SpringBoot发送邮件

下面是一个使用 Spring Boot 发送邮件的最小完整示例。这个示例使用 Gmail 的 SMTP 服务,发送一封简单的纯文本邮件。

前提要求:使用idea的引导搭建一个SpringBoot的web项目基础框架。

1. 项目依赖

首先,在你的 pom.xml 文件中添加 Spring Boot Mail Starter 依赖。

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

2. 应用配置

在 src/main/resources/application.properties 文件中,配置邮件服务器的连接信息。这里以 Gmail 为例,你需要将 your@gmail.com 和 your-app-password 替换为你自己的邮箱地址和应用密码。

注意:为了安全,Gmail 等服务提供商通常需要你生成一个应用专用密码 (App Password),而不是直接使用你的账户登录密码。

# 邮件服务器配置
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your@gmail.com
spring.mail.password=your-app-password #注意:这里的密码是应用专用密码,而不是谷歌邮箱的账号密码

# 启用 SMTP 认证和 TLS 加密
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

3. 创建邮件服务类

创建一个简单的 Service 类来封装邮件发送逻辑。Spring Boot 会自动配置 JavaMailSender bean,你可以直接注入使用。

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

@Service
public class EmailService {

private final JavaMailSender mailSender;
@Value("${spring.mail.username}") // 注入配置文件中的值
private String fromEmail;

public EmailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}

public void sendSimpleEmail(String toEmail, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(fromEmail); // 发件人地址
message.setTo(toEmail); // 收件人地址
message.setSubject(subject); // 邮件主题
message.setText(body); // 邮件正文
mailSender.send(message);
System.out.println("邮件发送成功!");
}
}

4. 创建一个 Web 控制器来测试

创建一个简单的 RestController,通过 HTTP 请求来触发邮件发送。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailController {

private final EmailService emailService;

public EmailController(EmailService emailService) {
this.emailService = emailService;
}

@GetMapping("/send-email")
public String sendEmail(@RequestParam String to) {
emailService.sendSimpleEmail(to, "这是一封测试邮件", "你好,这是一封由 Spring Boot 发送的纯文本邮件。");
return "邮件发送请求已提交!请检查你的邮箱。";
}
}

5. 主应用类

这是 Spring Boot 应用的入口。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MailApplication {

public static void main(String[] args) {
SpringApplication.run(MailApplication.class, args);
}
}

运行和测试

  • 运行 MailApplication 类。
  • 打开浏览器或使用工具(如 Postman),访问以下 URL:
    http://localhost:8080/send-email?to=target@example.com
    请将 target@example.com 替换为实际的收件人邮箱地址。
  • 如果一切配置正确,你会在控制台看到“邮件发送成功!”的输出,并且收件人会收到这封测试邮件。

    2、测试过程中遇到的问题:MailConnectException

    项目启动后,测试报错:
    org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: org.eclipse.angus.mail.util.MailConnectException: Couldn’t connect to host, port: smtp.gmail.com, 587; timeout -1;

    这个报错的原因是不能连接到谷歌的SMTP服务器。

    3、解决办法

    首先,你需要在谷歌的账号设置页面开启登录的“两步验证”,按照提示要求来就行。
    在这里插入图片描述

    在这里插入图片描述

    其次,不需要设置谷歌邮箱的POP或者IMAP。很多陈旧的技术博客说要开启POP或者IMAP,目前(2025年8月6日)是不需要设置的,谷歌邮箱的POP默认是关闭状态,IMAP默认是开启状态,SMTP服务连同IMAP一起是默认开启。
    如果使用的是其它邮箱的SMTP服务发送邮件,则具体以其它邮箱的说明为准。
    在这里插入图片描述

    然后,在谷歌设置页面搜索框中搜索“应用专用密码”
    在这里插入图片描述

    然后随便输入一个名称,点击创建。
    在这里插入图片描述
    然后会出现一个16位数字的密码,其格式类似“aaaa bbbb cccc dddd",这个密码只出现一次,务必记下来。
    在这里插入图片描述

    接着转到Spring Boot的配置文件(比如application.properties),将密码填到如下位置:
    spring.mail.password=aaaabbbbccccdddd
    这个密码中间不要有任何空格,也不要使用引号包围。我测试的时候报错,就是因为这里出问题了,谷歌官方给的密码是有空格的,需要把空格删掉。

    如果这个地方设置成下面这样,就会报错:
    spring.mail.password=aaaa bbbb cccc dddd
    spring.mail.password=“aaaa bbbb cccc dddd”

    按照上面配置后,重启服务,然后测试,就能收到谷歌发过来的测试邮件了。

    参考博客:
    如何使用Gmail的SMTP服务器发送邮件:https://www.vpsdawanjia.com/9613.html

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 谷歌邮箱SMTP服务器连接异常:MailConnectException: Couldn‘t connect to host, port: smtp.gmail.com, 587
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!