Sendgrid-csharp: Error when setting category on SendGrid.NetCore

Created on 28 Aug 2016  路  6Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

When using SetCategory, the request fails and the email isn't being sent.
It's like that at least since when RC2 of ASP.NET was published, I was confident you're aware of it and fixing it, but it seems I was wrong.

Steps to Reproduce

   public Task SendEmailAsync(string userId, string email, string subject, string message, string category, string notificationId)
   {
       var emailAddresses = email.Split(';').Where(x => x.Contains("@")).Select(x => x.Trim()).ToList();
       var myMessage = new SendGrid.SendGridMessage();
       myMessage.AddTo(emailAddresses);
       myMessage.From = new System.Net.Mail.MailAddress("[email protected]", "aaa");
       myMessage.Subject = subject;
       myMessage.Text = message;
       myMessage.Html = message;
       // bug Currently throws an exception... :(
       myMessage.SetCategory(category);
       var credentials = new System.Net.NetworkCredential(Options.SendGridUser, Options.SendGridKey);
       var transportWeb = new SendGrid.Web(credentials);
       // Send the email.
       return await transportWeb.DeliverAsync(myMessage);
}

The generated request(obtained with Fiddler):

--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=from

[email protected]
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=fromname

aaa
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=subject

Feedback on aaa
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=text

Name:ejwkql
Email:[email protected]

email content
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=html

Name:ejwkql
Email:[email protected]

email content
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=x-smtpapi

{Newtonsoft.Json.JsonTextWriter : Newtonsoft.Json.JsonTextWriter}
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=api_user

[email protected]
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=api_key

xyzxyzxyz
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="to[]"

[email protected]
--a1288644-d798-473c-9fe3-06c5bdda2ad0
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="to[]"

[email protected]
--a1288644-d798-473c-9fe3-06c5bdda2ad0--

Sensitive info was removed.

And the response is 400:

HTTP/1.1 400 Bad Request
Server: nginx
Date: Sun, 28 Aug 2016 19:20:52 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 148
Connection: keep-alive
X-Frame-Options: DENY

errorJSON in x-smtpapi could not be parsed

Technical details:

  • "SendGrid.NetCore": "1.0.0-rtm-00002",
  • .NET Version: 4.6.1 (Windows)

Please let me know if you need additional information.

help wanted question

All 6 comments

Hello @gdoron,

You are using an older version of this library that is no longer supported. Please check out the latest version of this library here on GitHub and on Nuget.

Note that we do not yet have official support for .NET Core, but we will soon. Please follow this ticket for progress: https://github.com/sendgrid/csharp-http-client/pull/3

Thanks!

@thinkingserious Hi,

I'm pretty sure I got that package from reading Azure's docs...?
Anyway, I changed to Version 8.0.4 and I must admit, I'm shocked, it's all dynamic and not using C#'s naming conventions (Async, and PascalCase) and you can't "pipe" callbacks.

// Doesn't compile
sg.client.mail.send.post(requestBody: mail.Get()).ContinueWith(r =>{});

I have no idea why you are no longer support the old library, it looked way way better.

I searched this repro for issues, and I already saw that many people reported these issues.
It just looks bad and unprofessional :cry:.

Thanks for the quick response though!

@gdoron,

We moved from the old library to support the new v3 Web API.

The issues that you mentioned are being resolved soon (thanks for taking the time to let us know!), you can follow along the progress here: https://github.com/sendgrid/sendgrid-csharp/issues/303

I will add your vote to that thread.

However, there is one issue you mentioned that I have not seen reported: "you can't "pipe" callbacks." --- do you mind elaborating on that one so we can get that one resolved too?

Thank you!

@thinkingserious

Consider this is an example:

var emailAddresses = email.Split(';').Where(x => x.Contains("@")).Select(x => x.Trim()).ToList();
var myMessage = new SendGrid.SendGridMessage();
myMessage.AddTo(emailAddresses);
myMessage.From = new System.Net.Mail.MailAddress("[email protected]", "aaa");
myMessage.Subject = subject;
myMessage.Text = message;
myMessage.Html = message;
// bug Currently throws an exception... :(
//myMessage.SetCategory(category);

var credentials = new System.Net.NetworkCredential(Options.SendGridUser, Options.SendGridKey);
var transportWeb = new SendGrid.Web(credentials);
// Send the email.

return transportWeb.DeliverAsync(myMessage).ContinueWith(r =>
{
    if (userId == null || notificationId == null)
        return;

    var notificationLog = new NotificationLog
                              {
                                  IsSuccess = !r.IsFaulted,
                                  Method = "Email",
                                  NotificationId = notificationId,
                                  UserId = userId
                              };
    _context.NotificationLogs.Add(notificationLog);
    _context.SaveChangesAsync();
});

If it's all dynamic, I can't register a callback with ContinueWith since the returned type of Task is unknown.

While we are on it, there should be an IsSuccessStatusCodeproperty on SendGrid.CSharp.HTTP.Client.Response.
Which should be implemented like this:

public bool IsSuccessStatusCode
{
    get { return ((int)statusCode >= 200) && ((int)statusCode <= 299); }
}

Thank you for all of the great feedback @gdoron.

Could you please email us at [email protected] with your mailing address and T-shirt size? We would love to give you a small token of our appreciation!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lijaso picture lijaso  路  3Comments

thinkingserious picture thinkingserious  路  4Comments

jewpaltz picture jewpaltz  路  3Comments

mhagesfeld picture mhagesfeld  路  3Comments

lebamb2 picture lebamb2  路  4Comments