title: Correct way to create ConfidentialClientApplication that allows retry for Windows Service
body: <p>I'm trying to create Windows Service that will allow me to send emails on behalf of specific users.</p>
  
  <p>Lates version of Graph Client allows specifying retries using <code>WithMaxRetry</code>.
  Unfortunately, there aren't any good examples showing "best practices" when creating <code>ConfidentialClientApplication</code>.</p>
  
  <p>Currently, I use the below code to send email without asking for login and password:</p>
  
  <pre><code>const string clientId = "foo...99a0";
  const string clientSecret = "#6A...cx#$a";
  const string tenant = "1c...7";
  const string azureAdInstance = "https://login.microsoftonline.com/{0}";
  var authority = string.Format(CultureInfo.InvariantCulture, azureAdInstance, tenant);
  string[] scopes = { "https://graph.microsoft.com/.default" };
  
  var clientCredentials = new ClientCredential(clientSecret);
  var confidentialClientApplication =
      new ConfidentialClientApplication(
          clientId,
          authority,
          "https://daemon",
          clientCredentials,
          null,
          new TokenCache());
  
  var graphClient =
      new GraphServiceClient("https://graph.microsoft.com/v1.0", 
      new DelegateAuthenticationProvider(
          async(requestMessage) =&gt;
          {
              var result = await confidentialClientApplication
                  .AcquireTokenForClientAsync(scopes);
              requestMessage.Headers.Authorization = 
                  new AuthenticationHeaderValue("bearer", result.AccessToken);
          }));
  
  var recipients = new List&lt;Recipient&gt;
  {
      new Recipient
      {
          EmailAddress = new Microsoft.Graph.EmailAddress
          {
              Address = "test@example.com"
          }
      }
  };
  
  var email = new Message
  {
      Body = new ItemBody
      {
          Content = "Works fine!",
          ContentType = BodyType.Html,
      },
      Subject = "Test",
      ToRecipients = recipients
  };
  
  await graphClient
      .Users["sender@example.onmicrosoft.com"]
      .SendMail(email, true)
      .Request()
      .PostAsync();
  </code></pre>
  
  <p>but I can't figure out how to create <code>ConfidentialClientApplication</code> based on recent changes from <a href="https://github.com/microsoftgraph/msgraph-sdk-dotnet/pull/367" rel="nofollow noreferrer">Request Context With Middleware Options</a>.</p>
  
  <p>Because I wasn't able to find up to date example my question is, how should I create <code>GraphServiceClient</code> to be able to send emails from Windows Service?</p>
  
  <p>This is code from above PR:</p>
  
  <pre><code>HttpProvider httpProvider = new HttpProvider();
  
  var graphClient = new GraphServiceClient(appOnlyProvider, httpProvider);
  graphClient.PerRequestAuthProvider = () =&gt; CreateDelegatedProvider();
  
  var me = await graphClient.Me.Request()
      .WithScopes(string[] { "User.Read" }) // adds auth scopes
      .WithMaxRetry(5) // specifies maximum number of retries
      .WithPerRequestAuthProvider()
      .GetAsync();
  </code></pre>
  
  <p>How should I adopt it per my requirement?
  I'm new to Graph so I want to avoid bad code.</p>