0

Note: To the moderator that closed this, it's completely different from the generic nullref question. If you read my post, it's obviously specific to SendGrid.

I believe I'm following pretty close to documented SendGrid usage:

public async Task<string> SendEmailSendGrid(string emailTo, string subject, string body) {
  var apiKey = SafeTrim(ConfigurationManager.AppSettings["SendGridAPIKey"]);
  var client = new SendGridClient(apiKey);
  var from = new EmailAddress(SafeTrim(ConfigurationManager.AppSettings["SendGridEmail"]));
  var to = new EmailAddress(emailTo);
  var msg = MailHelper.CreateSingleEmail(from, to, subject, string.Empty, body);

  try {
    var response = await client.SendEmailAsync(msg);
    //return response;
    return "SUCCESS";
  } catch (Exception ex) {
    return "ERROR in SendEmailSendGrid(): " + ex.Message;
  }
}

And the caller:

var result = utils.SendEmailSendGrid(decodedEmail, "email test", "This is a test email using SendGrid.");

And the error I get every time EVEN THOUGH IT WORKS and the email actually sends and arrives in my inbox:

Object reference not set to an instance of an object.

I verified that all my variables are populated as expected - none are null or empty. I am passing an empty string to the plain text param (because I always want HTML contents), but I also tried passing that some content and it made no difference; same error.

A strange thing: this blows up so hard that my catch block is never entered. Instead, as soon as the exception is thrown, this full-screen window comes up in my VS2022:

enter image description here

So it is working and sending the email, but why the heavy crash? What am I doing wrong?

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • The exception is being thrown even before it reaches the `try` block which means that it is happening when you are initializing your variables. You should initialize your variables inside the try block and then see where the exception is happening. Maybe some key from configuration would be missing? – Rahul Sharma Jan 15 '22 at 19:59
  • Rahul: No, it doesn't crash until it gets to SendEmailAsync() – HerrimanCoder Jan 15 '22 at 20:01
  • When you debug this, what value do you get for `var client = new SendGridClient(apiKey);` ? – Rahul Sharma Jan 15 '22 at 20:02

1 Answers1

-2

Can you try this my approach. I am using a single instance approach, try it and lets see.

public async Task SendAsync(IdentityMessage message)
    {
        
        var apiKey = new MvcApplication().SENDGRID_APIKEY;
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("noreply@questersworld.net", "Questersworld Team"),
            Subject = message.Subject,
            
            HtmlContent = "<table width=\"80%\"><tr><td><img src=\"http://www.questersworld.net/Images/quester.png\" width=\"50\" height=\"50\"> Questers World <p><strong>Welcome, Questersworld Participant!</strong></p><br> We value your connection.<br><p>" + message.Body + "</p><p><a href=\"www.questersworld.net\">visit www.questersworld.net</a> </p><br><strong>&copy; Questersworld.net</strong></td></tr></table>"
        };


        msg.AddTo(new EmailAddress(message.Destination, "Questersworld Participant"));
        var response = await client.SendEmailAsync(msg);
    }

Don't mind my info. Just try this and see