I am using SendGrid version: <package id="SendGrid.CSharp.HTTP.Client" version="3.0.0" targetFramework="net461" />
dynamic response = sg.client.mail.send.post(requestBody: requestBody);
Log.Info(string.Format("Posted Email:{0} requestBody:{1}", toEmail, requestBody));
Log.Info(response.StatusCode);
This code started to give exceptions after 3-4 months of using it. The issue was that the dynamic response didn't contain StatusCode. Let me know if I am doing something that is not correct. I think someone from the team sow that there is a new version of the sendgrid api and updated it then this exception was introduced started to break.
Hello @velchev,
Thanks for reaching out to us!
I was able to duplicate this issue with this code (the GET Collection worked as expected, but the crash happened when trying to POST):
using System;
using SendGrid.CSharp.HTTP.Client;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
Execute().Wait();
}
static async Task Execute()
{
string apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
String host = "https://api.sendgrid.com";
Dictionary<String, String> globalRequestHeaders = new Dictionary<String, String>();
globalRequestHeaders.Add("Authorization", "Bearer " + apiKey);
globalRequestHeaders.Add("Content-Type", "application/json");
String version = "v3";
dynamic client = new Client(host: host, requestHeaders: globalRequestHeaders, version: version);
// GET Collection
string queryParams = @"{
'limit': 100
}";
Dictionary<String, String> requestHeaders = new Dictionary<String, String>();
requestHeaders.Add("X-Test", "test");
dynamic response = await client.api_keys.get(queryParams: queryParams, requestHeaders: requestHeaders);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
Console.ReadLine();
string data = @"{
'personalizations': [
{
'to': [
{
'email': '[email protected]'
}
],
'subject': 'Hello World from the SendGrid C# Library!'
}
],
'from': {
'email': '[email protected]'
},
'content': [
{
'type': 'text/plain',
'value': 'Hello, Email!'
}
]
}";
Object json = JsonConvert.DeserializeObject<Object>(data);
response = client.mail.send.post(requestBody: json.ToString());
Console.WriteLine(response.StatusCode);
Console.ReadLine();
}
}
}
I have added this to our backlog for further investigation.
Alternatively, you can use the official SendGrid SDK: https://www.nuget.org/packages/SendGrid (it depends on SendGrid.CSharp.HTTP.Client).
With Best Regards,
Elmer
驴Can you please stop using dynamics? They are hard to maintain and debug. Very bad practice. As an angry customer, I won't migrate to V3 because of those painful dynamics objects out of control.
@cpsaez-Lendsum please have a look at StrongGrid, it's a strongly typed client for SendGrid's API which means that it does not use dynamic at all. Hopefully you'll find it helpful.
Very helpfull Jericho. Your library should be the official library. Stronger than the official.
The library is useful if you are constantly looking at the SendGrid WebAPI... and predicting what should be the next thing you add - crazy. sg.client.mail then what?! Will check Jericho's suggestion but for now no time to change to a new api wrapper.
Amazon SES charges $30 to send 300,000 emails and SendGrid charges $199.95. I don't know how SendGrid expects to stay in business if they can't provide proper SDKs for their "enhanced" features.
@cpsaez-Lendsum, @velchev,
The next version of this library (see the v9beta branch) has removed the dynamics and is now strongly typed. The beta is planned to be released within weeks. Thanks!
@Jericho's solution is solid and a good alternative until then.
@cpsaez-Lendsum, @velchev,
Have a chance to give the v9 SDK (now the official current version) a spin?
I have a VS2013 environment trying to add Sendgrid because of Azure, it is causing a lot of pain and the build breaking. I am trying my luck with Sendgrid 6.3.0 (already had to upgrade .net from 4.5.0. to 4.5.2 to because of it) but I can get neither SendGrid.Helpers.Mail [Namespace Helpers does not exist]; nor the SendGridClient() variable to resolve. All I need is a quick way to send out emails under Azure!
Also do I need to install SendGridClient -Version 1.1.0 in addition to SendGrid 6.3.0 or is the former obsolete? The many many many different versions (and few working examples) for sending emails are super confusing.
Why can I not use:
using SendGrid.Helpers.Mail;
How do I send mails with the "plain" libraryt (6.3.0)?
Hello @RealRaven2000,
Could you use the latest version of this SDK (v9)? v6.3.0 is no longer supported.
With Best Regards,
Elmer
I cannot upgrade my Visual Studio Ultimate it is stuck at 2013. Surely there must be a working example of Sendgrid that works with that? The library is around for a while I worked my way backwards from 8 and 7 and found 6.3.0 is barely supported in VS2013.
Can I use SendGridMail.dll for sending mail?
@RealRaven2000,
Moving this conversation here: https://github.com/sendgrid/sendgrid-csharp/issues/420
@RealRaven2000,
If you just want to stick with v6.3.0, here is the documentation: https://github.com/sendgrid/sendgrid-csharp/tree/b27983a8f3d84a9d28972f2720cca0315ad9fe32
Most helpful comment
驴Can you please stop using dynamics? They are hard to maintain and debug. Very bad practice. As an angry customer, I won't migrate to V3 because of those painful dynamics objects out of control.