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

求助:使用SmtpClient通过465端口发送邮件失败

Troubleshooting SmtpClient Port 465 Failure with Gmail

I’ve run into this exact issue before with Gmail and SmtpClient—port 465 can be tricky even when you set EnableSsl = true. Let’s break down the most common fixes:

1. Verify Gmail Account Security Settings

  • If you have 2-Step Verification (2FA) enabled on your Gmail account, you can’t use your regular password with SmtpClient. You need to generate an App Password instead:
    • Go to your Google Account > Security > App Passwords (only visible if 2FA is active).
    • Create a new app password for "Mail" and use that password in your SmtpClient configuration.
  • Even without 2FA, Google no longer supports "Less secure app access"—so App Passwords are still the recommended approach for authenticated SMTP access.

2. Correct SmtpClient Configuration for Port 465

SmtpClient’s default SSL protocol might not align with Gmail’s requirements (Gmail mandates TLS 1.2 or higher). Try explicitly setting the SSL protocol:

using (var client = new SmtpClient("smtp.gmail.com", 465))
{
    client.EnableSsl = true;
    // Explicitly enforce TLS 1.2 (required by Gmail)
    client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
    client.Credentials = new NetworkCredential("your-email@gmail.com", "your-app-password");
    
    var message = new MailMessage(
        "sender@gmail.com",
        "recipient@gmail.com",
        "Test Subject",
        "Test Body Content"
    );
    
    client.Send(message);
}

For older .NET versions where SslProtocols isn’t directly available on SmtpClient, set it globally first:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

3. Rule Out Firewall/Antivirus Blocking

Port 465 is sometimes blocked by corporate firewalls or antivirus tools. Test sending from a different network (like home Wi-Fi) to confirm if this is the issue.

The built-in SmtpClient is obsolete in .NET Core/.NET 5+. MailKit is a more reliable alternative that handles SSL/TLS negotiation automatically, reducing configuration headaches:

using MailKit.Net.Smtp;
using MimeKit;

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender Name", "sender@gmail.com"));
message.To.Add(new MailboxAddress("Recipient Name", "recipient@gmail.com"));
message.Subject = "Test Email via MailKit";
message.Body = new TextPart("plain") { Text = "This works seamlessly with port 465!" };

using (var client = new SmtpClient())
{
    client.Connect("smtp.gmail.com", 465, true); // True enables SSL
    client.Authenticate("your-email@gmail.com", "your-app-password");
    client.Send(message);
    client.Disconnect(true);
}

Content of the question originates from Stack Exchange, question author Alaa'

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:34:49