【问题标题】:mail is not sending from azure portal webapp using sendgrid邮件未使用 sendgrid 从 azure 门户 webapp 发送
【发布时间】:2015-12-17 06:54:03
【问题描述】:

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

“无法从传输连接中读取数据:net_io_connectionclosed。” 我用来发送邮件的 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("john@example.com", "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="mysendgriduserid@azure.com" password="mysendgridpassword" />

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

SendGridMessage myMessage = new SendGridMessage();
myMessage.AddTo(userId);
myMessage.From = new MailAddress("john@example.com", "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

【问题讨论】:

  • 你能提供你说的图片吗?
  • 你检查 25 端口打开了吗?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我认为你可能会错误地使用 API,例如将错误的参数传递给函数

    我创建了一个空的 Asp.net MVC 应用程序,并且能够从 Azure 应用服务通过 SendGrid 发送电子邮件

    这里是回购:https://github.com/shrimpy/SendGridSample

    下面是来自https://github.com/shrimpy/SendGridSample/blob/master/SendGridSample/Controllers/EmailController.cs的代码片段

    namespace SendGridSample.Controllers
    {
        public class EmailController : ApiController
        {
            [HttpGet]
            [Route("api/SendMail")]
            public string SendMail(string email, string username, string password)
            {
    
                // Create the email object first, then add the properties.
                var myMessage = new SendGridMessage();
    
                // Add the message properties.
                myMessage.From = new MailAddress("hello@hello.com");
    
                // Add multiple addresses to the To field.
                List<String> recipients = new List<String> { email };
    
                myMessage.AddTo(recipients);
    
                myMessage.Subject = "Testing the SendGrid Library";
    
                //Add Text bodies
                myMessage.Text = "Hello World from " + Environment.MachineName;
    
                // Create a Web transport, using API Key
                NetworkCredential nc = new NetworkCredential(username, password);
                var transportWeb = new Web("This string is a SendGrid API key", nc, TimeSpan.FromSeconds(15));
    
                // Send the email.
                transportWeb.DeliverAsync(myMessage);
    
                return "done";
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果使用 SmtpClient 类,请尝试将您的 Web 配置设置为(将 defaultCredentials 从 true 更改为 false):

      <network defaultCredentials="false" host="smtp.sendgrid.net" port="25" userName="mysendgriduserid@azure.com" password="mysendgridpassword" />
      

      如果尝试 SendGrid 类尝试等待 DeliverAsync 调用

      await transportWeb.DeliverAsync(myMessage);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-02
        • 1970-01-01
        • 2023-02-18
        相关资源
        最近更新 更多