.NET Core中SMTP 5.7.0认证错误修复求助
解决Gmail SMTP认证失败问题
错误信息
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
核心原因及解决步骤
1. 处理两步验证(优先方案)
如果你的Gmail账户开启了两步验证,直接使用原账户密码会触发认证失败,必须生成应用专用密码:
- 登录Google账户,进入「安全」设置页面
- 在「登录Google」板块找到「应用专用密码」选项
- 生成一个新的应用专用密码,替换代码中
*******的位置
2. 低安全性应用访问(临时方案,不推荐)
若未开启两步验证,可临时启用低安全性应用访问(Google已逐步淘汰此功能):
- 登录Gmail账户,进入「账户设置」→「安全性」
- 找到「低安全性应用访问」并开启
3. 核对SMTP配置参数
你的代码已包含正确配置,但再次确认以下参数:
- SMTP服务器:
smtp.gmail.com - 端口:587(必须配合
EnableSsl = true) EnableSsl必须设置为true
4. 解除Google安全拦截
Google可能会拦截陌生设备的登录请求,需登录Gmail账户查看安全通知,允许当前设备的访问权限
修改后的代码示例
using System; using System.Net.Mail; namespace ERP.Senders { public class SendEmail { public static void Send(string to, string subject, string body) { // 使用using语句自动释放资源 using (MailMessage mail = new MailMessage()) using (SmtpClient smtpServer = new SmtpClient("smtp.gmail.com")) { mail.From = new MailAddress("noreply.erpsystemmu@gmail.com", "سامانه مدیریت منابع انسانی ERP"); mail.To.Add(to); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = true; smtpServer.Port = 587; // 替换为应用专用密码或正确的账户密码 smtpServer.Credentials = new System.Net.NetworkCredential("noreply.erpsystemmu@gmail.com", "你的应用专用密码"); smtpServer.EnableSsl = true; smtpServer.Send(mail); } } } }
内容的提问来源于stack exchange,提问作者Ashkan Amjad
相关产品推荐
相关产品推荐

