title: Microsoft Graph ASP.NET MVC 5 Send Mail throwing unknown error
body: <p>Been working on an ASP.NET MVC 5 application that interacts with the MS Graph API. </p>
  
  <p>The controller gets a graph client for the current user and then using that client sends an email.</p>
  
  <pre><code>public class EmailController : Controller
  {
      // GET: Email
      public async Task&lt;ActionResult&gt; Index()
      {
          var client = await MSGraphServiceClient.GetGraphServiceClientAsync();
          await InvitationHelper.SendEmail(client);
  
  
          return Content("");
      }
  }
  
  public static async Task&lt;GraphServiceClient&gt; GetGraphServiceClientAsync()
      {
          string appId = ConfigurationManager.AppSettings["ida:ClientId"];
          string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
          string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
          // Get Signed in user
          string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
          // Get app and user id claims from Azure
          string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
          string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
          // Specify Graph resource URL
          string graphResourceID = "https://graph.microsoft.com";
  
          // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
          ClientCredential clientcred = new ClientCredential(appId, appKey);
          // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
          AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
          AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
  
          // use delegate to create auth provider using async auth result
          var delegateAuthProvider = new DelegateAuthenticationProvider((requestMessage) =&gt;
          {
              requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);
              return Task.FromResult(0);
          });
  
          // return the graph service client
          return new GraphServiceClient(delegateAuthProvider);
      }
  
      public static async Task&lt;bool&gt; SendEmail(GraphServiceClient graphServiceClient)
      {
  
              var message = new Message()
              {
                  Subject = "Test",
                  Body = new ItemBody()
                  {
                      ContentType = BodyType.Text,
                      Content = "This is a test"
                  },
                  ToRecipients = new List&lt;Recipient&gt;() { new Recipient() { EmailAddress = new EmailAddress() { Address = "jonathan.sweetland@gmail.com" } } }
              };
              var request = graphServiceClient.Me.SendMail(message, true).Request();
              await request.PostAsync();
  
              return true;
  }
  </code></pre>
  
  <p>It is not a permissions error because on AAD I have granted all the permissions for this app. I have other graph calls working so I know it authenticates correctly using the same code.</p>
  
  <p><a href="https://i.stack.imgur.com/ZLj83.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZLj83.png" alt="Error"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MNRVr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MNRVr.png" alt="Stack trace"></a></p>