title: Sending mail using GraphServiceClient
body: <p>I wrote a dll using .NET C# that was supposed to send emails using graph API.
  When I'm using the dll from a console application - everything works as expected: if the user is logged in the mail is sent, and if not - a screen pops up to connect.</p>
  <p>But, when I try to use the same dll in WinForms, the program stuck.
  Any idea why?</p>
  <p>This is my code:</p>
  <pre><code>var options = new PublicClientApplicationOptions {
    ClientId = clientId,
    TenantId = tenantId,
    RedirectUri = &quot;https://login.microsoftonline.com/common/oauth2/nativeclient&quot;,
  };
  
  if (application == null) {
    application = PublicClientApplicationBuilder.CreateWithApplicationOptions(options).WithAuthority(AzureCloudInstance.AzurePublic, ClientSecretOrTenantId).Build();
  }
  
  string token = &quot;&quot;;
  
  GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async(requestMessage) =&gt;{
    token = await GetToken();
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, token);
  }));
  
  Recipient recipient = new Recipient();
  recipient.EmailAddress = new EmailAddress();
  recipient.EmailAddress.Address = toAddress;
  
  List &lt; Recipient &gt; recipients = new List &lt; Recipient &gt; ();
  recipients.Add(recipient);
  
  var message = new Message {
    Subject = subject,
    Body = new ItemBody {
      ContentType = isHtml ? BodyType.Html: BodyType.Text,
      Content = bodyText,
    },
    ToRecipients = recipients,
  };
  
  try {
    await graphServiceClient.Me.SendMail(message, false).Request().PostAsync(); // get stuck here
  } catch(ServiceException) {
    graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async(requestMessage) =&gt;{
      token = await GetToken();
      requestMessage.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, token);
    }));
    await graphServiceClient.Me.SendMail(message, false).Request().PostAsync();
  }
  </code></pre>