ASP.NET Core Mailkit [英] Asp.net core mailkit

查看:95
本文介绍了ASP.NET Core Mailkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Asp.net核心Web应用程序,并且可以使用Mailkit发送电子邮件:

I have Asp.net core web application and for sending email using 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时,却失败,并显示以下消息:

While running application localy on VS it's sending fine. But when i upload it to Azure App Service on sending email it's failing with such message:

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)

有人知道如何解决吗?

推荐答案

Microsoft Azure不允许端口25上的出站连接,并且也阻止所有出站SMTP,但连接到SendGrid(第三方SMTP提供程序)时除外.单一的Microsoft Azure的受祝福"出站电子邮件提供商.

Microsoft Azure does not allow for outbound connections on port 25 and also blocks all outbound SMTP except for when connecting to SendGrid, a third-party SMTP provider who are the single "blessed" outbound email provider for Microsoft Azure.

(我认为这很奇怪,因为您会认为Microsoft希望在Azure中炫耀其Exchange服务,并与AWS的电子邮件服务竞争)

(I think this is odd because you'd think Microsoft would want to show-off their Exchange services in Azure, as well as competing with AWS' e-mail service)

SendGrid是免费的(幸运的是,否则,这将是勒索),每月最多20,000封电子邮件,之后它们仍然非常便宜.

SendGrid is free (fortunately - otherwise that would be extortion) up to 20,000 e-mails a month, after then they're still pretty cheap.

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

You can create a SendGrid account from within Azure Portal directly, then click the "Manage" button to open your SendGrid account details and get their SMTP details, including username and password.

SendGrid提供了两个API:第一个只是原始的哑" SMTP服务,尽管默认情况下,它们会将您的电子邮件延迟10分钟(以缓解垃圾邮件),直到您的帐户被视为受信任"为止.他们的第二个API是您可以使用的HTTP Web服务-在重载情况下更好,因为HTTP连接比SMTP连接便宜.

SendGrid offers two APIs: the first is just a raw "dumb" SMTP service, though by default they will delay your emails by 10 minutes (to mitigate spam) until your account is deemed "trusted". Their second API is a HTTP web-service you can use - this is better for heavy-load scenarios because HTTP connections are cheaper than SMTP connections.

这篇关于ASP.NET Core Mailkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆