Anders Anders - 1 year ago 201
C# Question

SendGrid Tutorial resulting in Bad Request

I apologize if this is a dupe question, but I have not found any solid information about this issue either on this site or on others.

With that being said, I am working on an MVC 5 web application. I am following this tutorial over on

public async Task SendAsync(IdentityMessage message)
await configSendGridasync(message);

private async Task configSendGridasync(IdentityMessage message)
var myMessage = new SendGridMessage();
myMessage.From = new System.Net.Mail.MailAddress(
"", "Your Contractor Connection");
myMessage.Subject = message.Subject;
myMessage.Text = message.Body;
myMessage.Html = message.Body;

var credentials = new NetworkCredential(
Properties.Resources.SendGridURL // necessary?

// Create a Web transport for sending email.
var transportWeb = new Web(credentials);

// Send the email.
if (transportWeb != null)
await transportWeb.DeliverAsync(myMessage);
Trace.TraceError("Failed to create Web transport.");
await Task.FromResult(0);

Each time it gets to the
await transportWeb.SendAsync(myMessage)
line in the above method, this error shows up in the browser:

Server Error in '/' Application.

Bad Request

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Bad Request

Line 54: if (transportWeb != null)
Line 55: {
Line 56: await transportWeb.DeliverAsync(myMessage);
Line 57: }
Line 58: else
Line 59: {
Line 60: Trace.TraceError("Failed to create Web transport.");
Line 61: await Task.FromResult(0);
Line 62: }

I signed up for a free account over at, using the "Free Package Google", giving me 25,000 monthly credits. The account has been provisioned.

I have tried a bunch of things so far including: disabling SSL, putting username/password directly in the code instead of pulling them from the
file, specifying the SMTP server inside the
object, and also tried changing

I tried explicitly setting the
instead of using
, as this post suggested. I also tried
on the
generated in the
method as suggested here. Same results, unfortunately.

Does anyone happen to have some insight as to what might be causing this to not function correctly?

Answer Source

I ended up using the built-in SmtpClient to get this working. Here is the code that I am using:

private async Task configSendGridasync(IdentityMessage message)
    var smtp = new SmtpClient(Properties.Resources.SendGridURL,587);

    var creds = new NetworkCredential(Properties.Resources.SendGridUser, Properties.Resources.SendGridPassword);

    smtp.UseDefaultCredentials = false;
    smtp.Credentials = creds;
    smtp.EnableSsl = false;

    var to = new MailAddress(message.Destination);
    var from = new MailAddress("", "Your Contractor Connection");

    var msg = new MailMessage();

    msg.From = from;
    msg.IsBodyHtml = true;
    msg.Subject = message.Subject;
    msg.Body = message.Body;

    await smtp.SendMailAsync(msg);

Even though it doesn't use SendGrid's C# API, the messages still show up on my SendGrid dashboard.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download