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

.NET应用MailKit/.NET邮件库发SMTP邮件失败,Limilabs可正常发送

问题:Cloud Run上.NET应用通过Google SMTP发件失败(Limilabs可正常发送)

我有一个运行在Cloud Run实例(已配置VPC)上的.NET应用,尝试通过Google SMTP服务器发送邮件。我的公网IP已在中继端加入IP白名单,用telnet测试中继功能正常,但使用MailKit和.NET原生邮件库发送邮件均失败。已知该服务器无需认证,仅依赖IP白名单,白名单IP可不用TLS但推荐使用。

MailKit实现及报错

我的MailKit代码实现如下:

using (var client = new SmtpClient())
{
    await client.ConnectAsync("my-host.gmail.com", 587, SecureSocketOptions.StartTls).ConfigureAwait(false);
    await client.SendAsync(emailMessage).ConfigureAwait(false);
    await client.DisconnectAsync(true).ConfigureAwait(false);
}

我还尝试设置client.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;,以及切换端口465和所有TLS选项,但始终报错:

An exception occurred while sending the email: MailKit.Net.Smtp.SmtpProtocolException: The SMTP server has unexpectedly disconnected. 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpStream.ReadAhead(CancellationToken cancellationToken) 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken) 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpStream.SendCommand(String command, CancellationToken cancellationToken) 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpClient.SendEhlo(Boolean connecting, String helo, CancellationToken cancellationToken) 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpClient.Ehlo(Boolean connecting, CancellationToken cancellationToken) 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpClient.PostConnect(Stream stream, String host, Int32 port, SecureSocketOptions options, Boolean starttls, CancellationToken cancellationToken) 2024-04-12 18:09:29.676 CEST at MailKit.Net.Smtp.SmtpClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken)

.NET原生邮件库实现及报错

使用.NET原生邮件库时,我尝试了多种设置组合及端口587、25、465,示例代码如下:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

var client = new System.Net.Mail.SmtpClient("my-host.gmail.com", 587)
{
    DeliveryMethod = SmtpDeliveryMethod.Network,
    TargetName = "my-host.gmail.com",
};

var mailMessage = new MailMessage
{
    From = new MailAddress("my-email@email.com"),
    To = { new MailAddress("to-email@email.com","email") },
    Body = message,
    Subject = subject,
    IsBodyHtml = true
};

报错信息为:

An exception occurred while sending the email: System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: 4.7.0 Try again later, closing connection. (EHLO) 2024-04-12 15:56:50.993 CEST at System.Net.Mail.EHelloCommand.CheckResponse(LineInfo[] lines)

Limilabs可正常运行的代码

Limilabs.Mail可以正常发送邮件,但由于需要授权许可,我不想采用该方案。其配置代码如下:

using (Smtp smtp = new Smtp())
{
    smtp.Connect("my-host.gmail.com");

    smtp.StartTLS();
    
    MailBuilder builder = new MailBuilder();
    builder.Text = "text";
    builder.From.Add(new MailBox("my-email@email.com"));
    builder.To.Add(new MailBox("sender-email@email.com"));

    IMail email = builder.Create();

    smtp.SendMessage(email);

    smtp.Close();
}

核心疑问

我想了解Limilabs的处理方式有何不同,以及我遗漏了哪些配置。需要说明的是,我无法控制邮件服务器,无法查看其设置或进行端侧调试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 00:08:12