邮件不是使用sendgrid从azure portal webapp发送的

时间:2015-12-17 06:54:04

标签: c# email azure azure-web-sites sendgrid

我创建了azure sendgrid帐户。实际上,我可以使用sendgrid凭据在azureVM中发送邮件,然后我们在azure portal中发布了该站点。在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

4 个答案:

答案 0 :(得分:1)

您是否检查过端口25是否已打开?

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

我创建了一个空的Asp.net MVC应用程序,并且能够从Azure App Service通过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 :(得分:0)

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

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

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

await transportWeb.DeliverAsync(myMessage);