title: AspNet MVC Identity - SendAsync HttpStatusCode
body: <p>Using ASP.NET MVC5 with Microsoft.AspNet.Identity v2.2.1 (current latest release)</p>
  
  <p>Is it possible to return the HttpStatusCode from this SendAsync email method?</p>
  
  <p>It runs fine, sending an email.  The problem is when the service fails with a non 200 HttpStatusCode, it is swallowed. I would like the user to be notified if the email cannot be delivered.</p>
  
  <pre><code>public class EmailService : IIdentityMessageService
  {
      public async Task SendAsync(IdentityMessage message)
      {
          var client = new RestClient
          {
              BaseUrl = new Uri("https://api.mailgun.net/v3"),
              Authenticator = new HttpBasicAuthenticator("api", GetMailGunKey())
          };
          var request = new RestRequest();
          request.AddParameter("domain", "mg.davestopmusic.com", ParameterType.UrlSegment);
          request.Resource = "{domain}/messages";
          request.AddParameter("from", "Dave Mateer &lt;dave@davestopmusic.com&gt;");
          request.AddParameter("to", message.Destination);
          request.AddParameter("subject", message.Subject);
          request.AddParameter("text", message.Body);
          request.AddParameter("html", message.Body);
          request.Method = Method.POST;
  
          var response = await client.ExecuteTaskAsync(request);
          int sc = (int) response.StatusCode;
          if (response.StatusCode != HttpStatusCode.OK)
          {
              // display the status code to the user
          }
      }
  </code></pre>
  
  <p>Email confirmation and password reset functionality modifications from here:</p>
  
  <p><a href="http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset" rel="nofollow">http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset</a></p>
  
  <p>The temptation is to go to another Identity provider in hope it is more extensible:
  <a href="https://github.com/brockallen/BrockAllen.MembershipReboot" rel="nofollow">https://github.com/brockallen/BrockAllen.MembershipReboot</a>
  <a href="https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application" rel="nofollow">https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application</a></p>
  
  <p>I do not need external based authenticaiton.</p>
  
  <p>Perhaps I need another method that returns the StatusCode and somehow wire this up instead of SendAsync.</p>
  
  <pre><code>public Task&lt;int&gt; SendAsync2(IdentityMessage message)
  {
      // blah
      int sc = (int) response.StatusCode;
      return Task.FromResult(sc);
  }
  </code></pre>