Anders
Anders

Reputation: 385

Get no emails with sendgrid and no error messages

I have a new MVC project and following this article to send mail on registration with sendgrid. http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset

Created a new folder "manager" in the root of the project. In that folder created I a new file EmailService.cs with the following code.

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(
                          "[email protected]", "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);
      }
   }
}

Added this to web.config with the correct values

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

And modified my registration controller with this

        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>");

I have done nothing else. I can register. Get no errors and no emails. Do I need to add EmailService to the project when it starts up in some why?

Upvotes: 2

Views: 3049

Answers (4)

nrpadilla2
nrpadilla2

Reputation: 11

I had the same issue. It turned out that my issue was that I was passing empty strings for the plain text and html content when using SendGrid.Helpers.Mail.MailHelper class to generate the message. Once I added those the message sent successfully.

Upvotes: 1

Vinney Kelly
Vinney Kelly

Reputation: 5095

For others out there who might be pulling their hair out trying to get their first SendGrid emails delivered, here's a brief account of my experience:

I was able to send email messages via the .NET SmtpClient without receiving any error messages. However, those emails seemed to get permanently lost inside of SendGrid as they were neither delivered nor did they appear in the "Emails Sent Today" counter on the SendGrid dashboard. I saw no indication that my account was being provisioned. However, when I tried again the next day, my emails were received.

Upvotes: 0

Anders
Anders

Reputation: 385

Added my code to identityconfig, and now it works.

Upvotes: 1

Jonathan Opperman
Jonathan Opperman

Reputation: 138

I haven't used Sendgrid before but it looks like you're missing an API KEY they listed on line 14 here:

var credentials = new NetworkCredential(api_user, api_key);

Upvotes: 0

Related Questions