Getting SendGrid Server Results in ASP.Net Core

373 views Asked by At

Here's my code to send email via SendGrid.

public Task<Result> Execute(string apiKey, string subject, string message, string email)
{
    var client = new SendGridClient(apiKey);
    string senderEmail = this.Configuration["Email:Address"];
    string senderName = this.Configuration["Email:Name"];

    var msg = new SendGridMessage()
    {
        From = new EmailAddress(senderEmail, senderName),
        Subject = subject,
        PlainTextContent = message,
        HtmlContent = message
    };
    msg.AddTo(new EmailAddress(email));
    var response = client.SendEmailAsync(msg);

    return response;
}

It is able to send email confirmation and password reset emails, when invoked by ASP.Net Core Identity code.

However, when I try to send email using my own controller action (for a Contact Us feature), nothing happens.

This is the controller action.

public async Task<JsonResult> SendContactEmail(int id, string message)
{
    Page page = await this.Get(id);
    var result = _emailSender.SendEmailAsync(page.Email, page.Subject, message);

    return new JsonResult(result);
}

Is there a way to troubleshoot this? I don't get any exceptions and the response object doesn't seem to have any useful property like a message from the SendGrid server.

UPDATE:

SendGrid's API does return an informative result. I have updated my code to return this.

https://github.com/sendgrid/sendgrid-csharp/blob/master/src/SendGrid/Response.cs

0

There are 0 answers