【问题标题】:Get no emails with sendgrid and no error messages使用 sendgrid 不会收到电子邮件,也不会收到错误消息
【发布时间】:2015-12-11 08:40:44
【问题描述】:

我有一个新的 MVC 项目,并按照这篇文章在 sendgrid 注册时发送邮件。 http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset

在项目的根目录中创建了一个新文件夹“manager”。在该文件夹中,我创建了一个新文件EmailService.cs,代码如下。

public class EmailService : IIdentityMessageService
{
   public async Task SendAsync(IdentityMessage message)
   {
      await configSendGridasync(message);
   }

   // Use NuGet to install SendGrid (Basic C# client lib) 
   private async Task configSendGridasync(IdentityMessage message)
   {
      var myMessage = new SendGridMessage();
      myMessage.AddTo(message.Destination);
      myMessage.From = new System.Net.Mail.MailAddress(
                          "mymail@gmail.com", "Joe S.");
      myMessage.Subject = message.Subject;
      myMessage.Text = message.Body;
      myMessage.Html = message.Body;

      var credentials = new NetworkCredential(
                 ConfigurationManager.AppSettings["mailAccount"],
                 ConfigurationManager.AppSettings["mailPassword"]
                 );

      // Create a Web transport for sending email.
      var transportWeb = new Web(credentials);

      // Send the email.
      if (transportWeb != null)
      {
         await transportWeb.DeliverAsync(myMessage);
      }
      else
      {
         Trace.TraceError("Failed to create Web transport.");
         await Task.FromResult(0);
      }
   }
}

使用正确的值将其添加到 web.config

  <add key="mailAccount" value="xyz" />
  <add key="mailPassword" value="password" />

并用这个修改了我的注册控制器

        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
        var callbackUrl = Url.Action("ConfirmEmail", "Account", 
           new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
        await UserManager.SendEmailAsync(user.Id, 
           "Confirm your account", "Please confirm your account by clicking <a href=\"" 
           + callbackUrl + "\">here</a>");

我什么也没做。我可以注册。没有错误,也没有电子邮件。为什么我需要在项目启动时将EmailService 添加到项目中?

【问题讨论】:

  • 只是让你知道你并不孤单,我在使用 SendGrid 时遇到了同样的问题:没有收到电子邮件,SendGrid 发送计数在仪表板中没有增加,也没有抛出异常。如果/当我找到一个答案时,我会发布一个答案:)

标签: c# asp.net asp.net-mvc sendgrid


【解决方案1】:

将我的代码添加到 identityconfig,现在它可以工作了。

【讨论】:

    【解决方案2】:

    我有同样的问题。事实证明,我的问题是在使用 SendGrid.Helpers.Mail.MailHelper 类生成消息时,我为纯文本和 html 内容传递了空字符串。一旦我添加了这些消息,就会成功发送。

    【讨论】:

    • 我们应该同时定义 htmlContent 和 plainContent 吗?实际上我只定义了 htmlContent 并没有收到任何电子邮件。我需要很长时间才能在 Azure 上再次发布,所以如果你有答案:)
    【解决方案3】:

    我以前没有使用过 Sendgrid,但您似乎缺少他们在第 14 行 here 列出的 API KEY:

    var credentials = new NetworkCredential(api_user, api_key);

    【讨论】:

      【解决方案4】:

      对于那些可能会为了让他们的第一封 SendGrid 电子邮件送达而费尽心思的其他人,以下是我的经验的简要说明:

      我能够通过 .NET SmtpClient 发送电子邮件,而不会收到任何错误消息。然而,这些电子邮件似乎在 SendGrid 中永久丢失,因为它们既没有送达,也没有出现在 SendGrid 仪表板上的“今天发送的电子邮件”计数器中。我没有看到任何迹象表明我的帐户正在被配置。但是,当我第二天再次尝试时,我的电子邮件已收到。

      【讨论】:

      • 我相信这是因为 SendGrid 故意延迟新创建的帐户以避免垃圾邮件发送者滥用。
      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 2020-01-04
      • 2021-07-09
      • 2019-07-22
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多