【问题标题】:Asp.net core mailkitAsp.net 核心邮件包
【发布时间】:2017-08-11 05:28:46
【问题描述】:

我有 Asp.net 核心网络应用程序并使用 Mailkit 发送电子邮件:

            emailMessage.From.Add(new MailboxAddress("John", "john@outlook.com"));
            emailMessage.To.Add(new MailboxAddress("Doe", email));
            emailMessage.Subject = subject;
            emailMessage.Body = new TextPart("plain") { Text = message };

            using (var client = new SmtpClient())
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                client.Connect("smtp-mail.outlook.com", 587, false);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate("john@outlook.com", "password");
                client.Send(emailMessage);
                client.Disconnect(true);
            }

在 VS 上本地运行应用程序时,它发送正常。但是,当我在发送电子邮件时将其上传到 Azure App Service 时,会出现这样的消息:

SmtpProtocolException: The SMTP server has unexpectedly disconnected.

MailKit.Net.Smtp.SmtpStream.ReadAhead(CancellationToken cancellationToken)

MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken)
MailKit.Net.Smtp.SmtpClient.SendCommand(string command, CancellationToken cancellationToken)
MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
MailKit.MailService.Authenticate(string userName, string password, CancellationToken cancellationToken)

有人知道怎么解决吗?

【问题讨论】:

    标签: asp.net email azure


    【解决方案1】:

    Microsoft Azure 不允许端口 25 上的出站连接,并且还阻止所有出站 SMTP,但连接到 SendGrid 时除外,SendGrid 是第三方 SMTP 提供商,是 Microsoft Azure 的单一“受祝福”出站电子邮件提供商。

    (我认为这很奇怪,因为你会认为微软想在 Azure 中炫耀他们的 Exchange 服务,以及与 AWS 的电子邮件服务竞争)

    SendGrid 是免费的(幸运的是 - 否则那将是勒索)每月最多 20,000 封电子邮件,之后它们仍然相当便宜。

    您可以直接在 Azure 门户中创建一个 SendGrid 帐户,然后单击“管理”按钮打开您的 SendGrid 帐户详细信息并获取其 SMTP 详细信息,包括用户名和密码。

    SendGrid 提供两个 API:第一个只是一个原始的“哑”SMTP 服务,但默认情况下,它们会将您的电子邮件延迟 10 分钟(以减少垃圾邮件),直到您的帐户被视为“受信任”。他们的第二个 API 是您可以使用的 HTTP Web 服务 - 这对于负载较重的场景更好,因为 HTTP 连接比 SMTP 连接便宜。

    【讨论】:

    • 嗨,戴,感谢您的解释。那么这是否意味着我们不能在 Azure 上使用 Mailkit for ASP.NET Core?
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多