Sendgrid-csharp: Can't seem to get things started with Microsoft's Tutorial due to API change

Created on 18 Jun 2016  路  5Comments  路  Source: sendgrid/sendgrid-csharp

I'm not sure what's causing this, but I keep on getting a BadRequest when I try and send an email. Any ideas?

Issue Summary

Not able to send mail after following directions. Combining new API + Microsoft's existing tutorial.

Steps to Reproduce

Follow Microsoft's steps up till the SendEmailAsync method. Replace that with the following:

    public Task SendEmailAsync(string email, string subject, string message)
    {
        var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
        dynamic sendGrid = new SendGridAPIClient(apiKey);

        Email from = new Email("[email protected]");
        Email to = new Email(email);
        Content content = new Content("text/html", message);
        Mail mail = new Mail(from, subject, to, content);

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

        return Task.FromResult(0);
    }

Results

response.StatusCode = BadRequest
response.Headers {Connection: keep-alive
    X-Frame-Options: DENY
    Date: Sat, 18 Jun 2016 19:24:02 GMT
    Server: nginx
}   System.Net.Http.Headers.HttpResponseHeaders

Technical details:

  • sendgrid-csharp Version: Sendgrid 7.0.2
  • .NET Version: 4.6
question

Most helpful comment

I can confirm this works:

public Task SendEmailAsync(string email, string subject, string message)
{
    var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
    dynamic sendGrid = new SendGridAPIClient(apiKey);

    Email from = new Email("[email protected]");
    Email to = new Email(email);
    Content content = new Content("text/html", message);
    Mail mail = new Mail(from, subject, to, content);

    var mailGetResult = mail.Get();

    dynamic response = sendGrid.client.mail.send.post(requestBody: mailGetResult.Replace("'", "\\\""));

    return Task.FromResult(0);
}

Is there some place I could find bad input data in the documentation?

Going to go ahead and close since the original question got answered.

Thanks!

All 5 comments

Hello @charles-smith,

Before you do the POST, could you print out the value of mail.Get()?

Thanks!

Here ya go:

mailGetResult = "{\"from\":{\"email\":\"[email protected]\"},\"subject\":\"Confirm your account\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"Please confirm your account by clicking this link: <a href='https://localhost:44381/Account/ConfirmEmail?userId=136f33aa-62c2-40c5-9b63-f5eb6042ab19&code=CfDJ8A5ORVBZiNBKsxddZkx8SA5qO7Neyc1j9CkXLXy%2BdOFabvXplQt2CwoQ2t2JcFfaMVb1FNlW3nZNVapkjJ81Bxltc9vAljHlha8iKzwic%2B5d2uCLjGGTuu9xUoCIarmcm6lClrmJcOi3x4qeHSTNVSZIuRz%2B8ATUROVVkI1o00oSHArSdL6IobdPaAVz%2BqcS514ob5uVvfij8ETQWB1W%2Fe1SGgKN9yGgMePxrYd%2Fy3HoRz51l240fFL2WPpt2hDeMg%3D%3D'>link</a>\"}]}"

@charles-smith,

Please replace ' with \"

So <a href='https://localhost:44381/Account/ConfirmEmail?userId=136f33aa-62c2-40c5-9b63-f5eb6042ab19&code=CfDJ8A5ORVBZiNBKsxddZkx8SA5qO7Neyc1j9CkXLXy%2BdOFabvXplQt2CwoQ2t2JcFfaMVb1FNlW3nZNVapkjJ81Bxltc9vAljHlha8iKzwic%2B5d2uCLjGGTuu9xUoCIarmcm6lClrmJcOi3x4qeHSTNVSZIuRz%2B8ATUROVVkI1o00oSHArSdL6IobdPaAVz%2BqcS514ob5uVvfij8ETQWB1W%2Fe1SGgKN9yGgMePxrYd%2Fy3HoRz51l240fFL2WPpt2hDeMg%3D%3D'>

becomes

<a href=\"https://localhost:44381/Account/ConfirmEmail?userId=136f33aa-62c2-40c5-9b63-f5eb6042ab19&code=CfDJ8A5ORVBZiNBKsxddZkx8SA5qO7Neyc1j9CkXLXy%2BdOFabvXplQt2CwoQ2t2JcFfaMVb1FNlW3nZNVapkjJ81Bxltc9vAljHlha8iKzwic%2B5d2uCLjGGTuu9xUoCIarmcm6lClrmJcOi3x4qeHSTNVSZIuRz%2B8ATUROVVkI1o00oSHArSdL6IobdPaAVz%2BqcS514ob5uVvfij8ETQWB1W%2Fe1SGgKN9yGgMePxrYd%2Fy3HoRz51l240fFL2WPpt2hDeMg%3D%3D\">

Please let me know if that works for you (appears it works for me) and I'll ask our team if we get a better error message.

Thanks!

I can confirm this works:

public Task SendEmailAsync(string email, string subject, string message)
{
    var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
    dynamic sendGrid = new SendGridAPIClient(apiKey);

    Email from = new Email("[email protected]");
    Email to = new Email(email);
    Content content = new Content("text/html", message);
    Mail mail = new Mail(from, subject, to, content);

    var mailGetResult = mail.Get();

    dynamic response = sendGrid.client.mail.send.post(requestBody: mailGetResult.Replace("'", "\\\""));

    return Task.FromResult(0);
}

Is there some place I could find bad input data in the documentation?

Going to go ahead and close since the original question got answered.

Thanks!

I have created a ticket to solve this issue through the library here: https://github.com/sendgrid/sendgrid-csharp/issues/247

Documentation for the v3 /mail/send endpoint is:
https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html

Thank you for your support!

Was this page helpful?
0 / 5 - 0 ratings