c# - 邮件未使用 sendgrid 从 azure 门户 Web 应用程序发送

标签 c# email azure azure-web-app-service sendgrid

我已经创建了 azure sendgrid 帐户。实际上,我可以使用我的 sendgrid 凭据在 azureVM 中发送邮件,并且我们在 azure 门户中发布了该站点。在 azure webapp 中,同一电子邮件无法发送,错误如下:

“无法从传输连接读取数据:net_io_connection已关闭。” 我用来发送邮件的 C# 代码:

起初我尝试使用 smtp

var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
var mail = new MailMessage();
var smtpServer = new SmtpClient(smtpSection.Network.Host);
mail.From = new MailAddress(smtpSection.Network.UserName);
mail.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d3d6d1d7f9dcc1d8d4c9d5dc97dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>", "John Smith");
mail.To.Add(userId);
mail.Subject = subject;
mail.Body = body;
smtpServer.Port = smtpSection.Network.Port;
smtpServer.Credentials = new System.Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
smtpServer.EnableSsl = false;
smtpServer.Send(mail);

网页配置内容:

<network defaultCredentials="true" host="smtp.sendgrid.net" port="25" userName="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09d8983959e9497829994858395829994b0918a858295de939f9d" rel="noreferrer noopener nofollow">[email protected]</a>" password="mysendgridpassword" />

它在 azure webapp 中失败,但在 azureVM 中运行良好。然后我尝试使用下面的 sendgrid api:

SendGridMessage myMessage = new SendGridMessage();
myMessage.AddTo(userId);
myMessage.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="167c797e7856736e777b667a733875797b" rel="noreferrer noopener nofollow">[email protected]</a>", "John Smith");
myMessage.Subject = subject;
myMessage.Text = body;
var transportWeb = new Web("myapikey");
transportWeb.DeliverAsync(myMessage);

这在 azure webapp 中也失败了,但在 azureVM 中工作正常。两者都显示与图像中相同的错误。

我完全无能为力。请帮助我消除这个错误。

谢谢。

我很抱歉。我无法上传图片。我只是分享图像中的堆栈跟踪:

Server Error in '/' Application.

Unable to read data from the transport connection: net_io_connectionclosed.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[IOException: Unable to read data from the transport connection: net_io_connectionclosed.]
   System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) +1829453
   System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) +227
   System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) +16
   System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) +54
   System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) +36
   System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) +88
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1661

[SmtpException: Failure sending mail.]
   Dme.Application.Repositories.<Dme-Application-Repositories-IIdentityRepository-SendEmailAsync>d__22.MoveNext() +259
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) +25
   Dme.Application.Services.<Dme-Shared-Contracts-IIdentityService-CreateOrUpdateUserAsync>d__14.MoveNext() +5421
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) +25
   Dme.Web.UI.Areas.Admin.Controllers.<SaveOrUpdate>d__13.MoveNext() +962
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +50
   System.Web.Mvc.Async.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.114.0

最佳答案

这通常是您自己的内部网络中的错误。您尝试发送的端口可能已被防火墙或您的 ISP 关闭。 telnet smtp.sendgrid.net 25 适合您吗?或者,您可以尝试使用端口 465(SSL) 或 587(TLS/普通连接)。

关于c# - 邮件未使用 sendgrid 从 azure 门户 Web 应用程序发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328381/

相关文章:

c# - WCF 服务代理生成在通过 F5 负载平衡器公开时引发错误

C#、WPF、将 List<string> 绑定(bind)到 DataGrid

c# - 检查实体(属性和集合)是否脏

php mail() 给我一个 "no disk"错误

azure - 从传统的用户名/密码身份验证迁移到 WIF

azure - 为什么我可以在 Azure Cloud Shell Bash 中创建新文件和目录,但不能在 PowerShell 中创建?

C# Winforms Designer 调用基本属性而不是 'new' 之一?

c# - C#如何将文件附加到电子邮件中

c# - 使用带有 Gmail OAuth 的 MailKit 发送邮件

c# - 命名空间 'BackupSchedule' 中的“元素” 'http://schemas.microsoft.com/windowsazure' 不是预期的。需要元素 'BackupName'