I'm not sure what's causing this, but I keep on getting a BadRequest when I try and send an email. Any ideas?
Not able to send mail after following directions. Combining new API + Microsoft's existing tutorial.
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);
}
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
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!
Most helpful comment
I can confirm this works:
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!