Nick Gallant
Nick Gallant

Reputation: 305

Sending mail with SendGrid v3 C# just hangs

I've upgraded from v2 to v3 and (C# implementation) of SendGrid. I can get it to send mail, but it just hangs in the code after doing so on the last line:

dynamic sendGridAPIClient = new SendGridAPIClient(SendGridApi);

Email from = new Email(EmailAddress, EmailName);
Email to = new Email(EmailAddress, EmailName);
Content content = new Content("text/html", message);
Mail mail = new Mail(from, subject, to, content);

dynamic response = sendGridAPIClient.client.mail.send.post(requestBody: mail.Get());

It's annoying because it goes into a black hole with that function and you can't debug it unlike v2 which worked just fine.

Upvotes: 1

Views: 1606

Answers (2)

Aaron Queenan
Aaron Queenan

Reputation: 921

If you can't use await, try this instead:

var response = sendGridAPIClient.client.mail.send.post(requestBody: mail.Get()) .GetAwaiter().GetResult();

Note that a response.StatusCode of OK (200), rather counter-intuitively, means SendGrid have thrown the mail message into the void.

You want a StatusCode of Accepted (202), which means it's been queued for delivery.

Upvotes: 1

Sakis Tsolos
Sakis Tsolos

Reputation: 1

It is an Asynchronous Call, so try this:

dynamic response = await sendGridAPIClient.client.mail.send.post(requestBody: mail.Get());

Upvotes: 0

Related Questions