title: SendGrid Tutorial resulting in Bad Request
body: <p>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.</p>
  
  <p>With that being said, I am working on an MVC 5 web application. I am following <a href="http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset" rel="nofollow noreferrer">this tutorial</a> over on ASP.net. </p>
  
  <pre><code>public async Task SendAsync(IdentityMessage message)
  {
      await configSendGridasync(message);
  }
  
  private async Task configSendGridasync(IdentityMessage message)
  {
      var myMessage = new SendGridMessage();
      myMessage.AddTo(message.Destination);
      myMessage.From = new System.Net.Mail.MailAddress(
                          "info@ycc.com", "Your Contractor Connection");
      myMessage.Subject = message.Subject;
      myMessage.Text = message.Body;
      myMessage.Html = message.Body;
  
      var credentials = new NetworkCredential(
           Properties.Resources.SendGridUser,
           Properties.Resources.SendGridPassword,
           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);
      }
      else
      {
          Trace.TraceError("Failed to create Web transport.");
          await Task.FromResult(0);
      }
  }
  </code></pre>
  
  <p>Each time it gets to the <code>await transportWeb.SendAsync(myMessage)</code> line in the above method, this error shows up in the browser:</p>
  
  <hr>
  
  <h1>Server Error in '/' Application.</h1>
  
  <p><h2> <i>Bad Request</i> </h2>
  <b> Description: </b>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.</p>
  
  <p><b> Exception Details: </b>System.Exception: Bad Request</p>
  
  <pre><code>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:             }
  </code></pre>
  
  <hr>
  
  <p>I signed up for a free account over at <a href="https://sendgrid.com/" rel="nofollow noreferrer">https://sendgrid.com/</a>, using the "Free Package Google", giving me 25,000 monthly credits. The account has been provisioned.</p>
  
  <p>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 <code>Resources.resx</code> file, specifying the SMTP server inside the <code>NetworkCredential</code> object, and also tried changing <code>DeliverAsync(...)</code> to <code>Deliver()</code>.</p>
  
  <p>I tried explicitly setting the <code>subject</code> instead of using <code>message.Subject</code>, as <a href="https://stackoverflow.com/questions/21892185/bad-request-error-using-sendgrid-smtp-email-submission">this post</a> suggested. I also tried <code>HttpUtility.UrlEncode</code> on the <code>callbackUrl</code> generated in the <code>Account/Register</code> method as <a href="https://stackoverflow.com/questions/25212263/getting-bad-request-in-rick-andersons-code-for-password-recovery">suggested here</a>. Same results, unfortunately.</p>
  
  <p>Does anyone happen to have some insight as to what might be causing this to not function correctly?</p>