title: Unable to send email via Microsoft Graph API with Delegated Permission
body: <p>I created a C# console application to send email using Microsoft Graph API. On adding Mail.Send <strong>Application</strong> Permission, it works fine. But, because of company requirements, I was asked to use Mail.Send <strong>Delegated</strong> Permission instead and with that permission I don't see it working and I see this error:</p>
  <p><a href="https://i.stack.imgur.com/fqEZp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fqEZp.png" alt="enter image description here" /></a></p>
  <p>Are there any steps I should consider doing after adding Mail.Send <strong>Delegated</strong> Permission in order to get this working?</p>
  <p><a href="https://i.stack.imgur.com/YXKOc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YXKOc.png" alt="enter image description here" /></a></p>
  <p>Here is my code:</p>
  <pre><code>    static void Main(string[] args)
      {
          // Azure AD APP
          string clientId = &quot;&lt;client Key Here&gt;&quot;;
          string tenantID = &quot;&lt;tenant key here&gt;&quot;;
          string clientSecret = &quot;&lt;client secret here&gt;&quot;;
  
          Task&lt;GraphServiceClient&gt; callTask = Task.Run(() =&gt; SendEmail(clientId, tenantID, clientSecret));
          // Wait for it to finish
          callTask.Wait();
          // Get the result
          var astr = callTask;
      }
  
      public static async Task&lt;GraphServiceClient&gt; SendEmail(string clientId, string tenantID, string clientSecret)
      {
  
          var confidentialClientApplication = ConfidentialClientApplicationBuilder
              .Create(clientId)
              .WithTenantId(tenantID)
              .WithClientSecret(clientSecret)
              .Build();
  
          var authProvider = new ClientCredentialProvider(confidentialClientApplication);       
  
          var graphClient = new GraphServiceClient(authProvider);
  
          var message = new Message
              {
                  Subject = subject,
                  Body = new ItemBody
                  {
                      ContentType = BodyType.Text,
                      Content = content
                  },
                  ToRecipients = new List&lt;Recipient&gt;()
                  {
                      new Recipient
                      {
                          EmailAddress = new EmailAddress { Address = recipientAddress }
                      }
                  }
              };
  
           var saveToSentItems = true;
  
           await _graphClient.Users[&lt;userprincipalname&gt;]
                                  .SendMail(message, saveToSentItems)
                                  .Request()
                                  .PostAsync();
  
          return graphClient;
  
      }
  </code></pre>
  <p>UPDATE:</p>
  <p>Based on below answer, I updated code as follows:</p>
  <pre><code>var publicClientApplication = PublicClientApplicationBuilder
              .Create(&quot;&lt;client-id&gt;&quot;)
              .WithTenantId(&quot;&lt;tenant-id&gt;&quot;)
              .Build();
  
              var authProvider = new UsernamePasswordProvider(publicClientApplication);
  
  </code></pre>
  <pre><code>var secureString = new NetworkCredential(&quot;&quot;, &quot;&lt;password&gt;&quot;).SecurePassword;
  
  User me = await graphClient.Me.Request()
                  .WithUsernamePassword(&quot;&lt;username&gt;&quot;, secureString)
                  .GetAsync();
  </code></pre>
  <p>I enabled &quot;Allow public client flows&quot; to fix an exception.</p>
  <p><a href="https://i.stack.imgur.com/2j95t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2j95t.png" alt="enter image description here" /></a></p>
  <p>And now I see another exception: Insufficient privileges to complete the operation.</p>
  <p>What am I missing?</p>
  <p>UPDATE: Currently I see this exception with no changes in the code:</p>
  <p><a href="https://i.stack.imgur.com/FfyOl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FfyOl.png" alt="enter image description here" /></a></p>