You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Gmail发送SMTP邮件遇5.7.0认证错误的解决方法(无低安全选项)

修复Gmail SMTP 5.7.0认证错误的方案

针对你遇到的「SMTP服务器要求安全连接或客户端未通过认证,服务器响应:5.7.0 Authentication Required」错误,结合Google已取消低安全应用选项的情况,可通过以下两种方案解决:

方案一:使用应用专用密码(需开启两步验证)

这是最简便的解决方法,前提是你的Google账号已开启两步验证:

  1. 登录Google账号,进入「安全」设置页面
  2. 确认已开启两步验证,若未开启需先完成配置
  3. 在安全设置中找到「应用专用密码」选项,生成一个专门用于邮件客户端的密码(选择「邮件」作为应用类型,设备名称可自定义)
  4. 将代码中NetworkCredential的password替换为这个生成的应用专用密码

方案二:使用OAuth2认证(更安全,支持未开启两步验证的账号)

如果未开启两步验证,或需要更安全的认证方式,可采用OAuth2协议实现:

.NET Framework 实现步骤

  1. 在NuGet中安装Microsoft.Identity.Client包
  2. 在Google Cloud控制台创建OAuth应用:
    • 选择「桌面应用」或「服务账号」作为应用类型
    • 启用Gmail API,并获取客户端ID和客户端密钥
  3. 替换代码为以下示例:
using Microsoft.Identity.Client;
using System.Net;
using System.Net.Mail;

// 替换为你的实际信息
string clientId = "你的OAuth客户端ID";
string clientSecret = "你的OAuth客户端密钥";
string fromEmail = "你的Gmail邮箱";
string toEmail = "收件人邮箱";
string htmlContent = "<h1>测试邮件内容</h1>";

var scopes = new[] { "https://mail.google.com/" };
var app = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithClientSecret(clientSecret)
    .WithTenantId("common") // 个人账号固定使用"common"
    .Build();

// 获取OAuth访问令牌
var authResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();

// 构建邮件
var message = new MailMessage(fromEmail, toEmail, "测试邮件", htmlContent);
message.IsBodyHtml = true;

// 发送邮件
using (var smtp = new SmtpClient("smtp.gmail.com", 587))
{
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    // 用OAuth访问令牌作为认证密码
    smtp.Credentials = new NetworkCredential(fromEmail, authResult.AccessToken);
    smtp.Send(message);
}

.NET Core/.NET 5+ 推荐方案

原生SmtpClient对OAuth2支持有限,推荐使用MailKit库实现:

  1. 安装MailKit和MimeKit NuGet包
  2. 同样在Google Cloud控制台获取OAuth客户端信息
  3. 示例代码:
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using Microsoft.Identity.Client;

string clientId = "你的OAuth客户端ID";
string clientSecret = "你的OAuth客户端密钥";
string fromEmail = "你的Gmail邮箱";
string toEmail = "收件人邮箱";

var scopes = new[] { "https://mail.google.com/" };
var app = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithClientSecret(clientSecret)
    .WithTenantId("common")
    .Build();

var authResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();

var message = new MimeMessage();
message.From.Add(new MailboxAddress("发件人名称", fromEmail));
message.To.Add(new MailboxAddress("收件人名称", toEmail));
message.Subject = "测试邮件";

message.Body = new TextPart("html")
{
    Text = "<h1>测试邮件内容</h1>"
};

using (var client = new SmtpClient())
{
    await client.ConnectAsync("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    await client.AuthenticateAsync(fromEmail, authResult.AccessToken);
    await client.SendAsync(message);
    await client.DisconnectAsync(true);
}

注意事项

  • 代码中UseDefaultCredentials必须设置为false,且要在设置Credentials之前执行,否则会覆盖认证信息
  • 方案二需要确保Gmail账号已启用Gmail API
  • 应用专用密码仅对开启两步验证的账号可用

内容的提问来源于stack exchange,提问作者Sandeep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:58:05