c# - 使用 sendgrid 收不到电子邮件,也没有错误消息

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

我有一个新的 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(
                          "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="432e3a2e222a2f03242e222a2f6d202c2e" rel="noreferrer noopener nofollow">[email protected]</a>", "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" />

并用此修改了我的注册 Controller

        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 添加到项目中?

最佳答案

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

关于c# - 使用 sendgrid 收不到电子邮件,也没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219241/

相关文章:

c# - System.Reactive - 将 2 个 observables 组合成一个具有所有排列的流

c# - 使用 Shadow Copy 启动的 CefSharp 应用程序

asp.net - 在上传控件中显示excel文件的所有版本(asp.net)

asp.net - asp.net 应用程序中的内存泄漏 - W3WP 和 gen 2 堆持续增长,直到 AppPool 回收

asp.net-mvc - Linq to SQL 替换相关实体

c# - 从日志文件中提取特定字段并使用 C# 将其导出到 csv 文件

C# 从参数加载文件

c# - 将新的 aspx 页面添加到已发布的网站

jquery - 内联 JqGrid 保存按钮点击问题

asp.net-mvc - ASP.NET MVC : View sometimes returned as application/json instead of text/html