title: Subscription to Message through Microsoft Graph API Without User Interaction
body: <p>I am trying to create an application that subscribes to mail events (New mails, Create,update) in office 365 using Microsoft Graph APIs without user interaction. If there is a new email I want to send a notification to my app which has been built using C# MVC5 and .Net 4.5. An example I am following is in Github here (using Daemon Service
  <a href="https://github.com/Azure-Samples/active-directory-dotnet-daemon-v2" rel="nofollow noreferrer">Github project Link</a>)
  but it has been built in .Net Core. I came across another application which uses WebJobs and is very close to my requirement<br>
  <a href="https://github.com/microsoftgraph/webjobs-webhooks-sample" rel="nofollow noreferrer">Azure webjobs</a> but it has not been updated in the last 2 years. I followed the steps but most of them do not match with my Azure Developer Account (free). Can some one help me on this please?</p>
  
  <p>App settings : </p>
  
  <p>App Settings: </p>
  
  <pre><code>&lt;add key="tenantId" value="[Tenant_ID]" /&gt;
      &lt;add key="clientId" value="[AppID]" /&gt;
      &lt;add key="clientSecret" value="[Client Secret]" /&gt;
      &lt;add key="webjobs" value="DefaultEndpointsProtocol=https;AccountName=onvidawebjob1aa1b;AccountKey=Z3xewALbEc43gE/dL06HxCGIjVtpaiTkIY4ZolkjZt9Z86xHIZLzMwXBlujNI8/PcTJbM1LIs5KNceVTGgESmA==" /&gt;
      &lt;add key="subscriptionblobname" value="subscriptions/subscription1" /&gt;
      &lt;add key="notificationurl" value="https://onvidawebjob1.azurewebsites.net/api/HttpTrigger1?code=H7liZOlcQc5qjcZOkRXEb0x4JVqTzoXnrl6mAswsEKl4LlmAYm/ACw==" /&gt;
  </code></pre>
  
  <p>i tried to recreate the app got below issue Trace</p>
  
  <pre><code>Development settings applied
  Found the following functions:
  WebHooksSample.Functions.ManageSubscriptions
  WebHooksSample.Functions.OnNotificationReceived
  Singleton lock acquired (cd016b3464fa4d8aa8f41f884a064168/WebHooksSample.Functions.ManageSubscriptions.Listener)
  Function 'WebHooksSample.Functions.ManageSubscriptions' is configured to run on startup. Executing now.
  Executing 'Functions.ManageSubscriptions' (Reason='Timer fired at 2018-11-16T19:05:27.4182054+05:30', Id=155f7f8a-c352-427f-a7c3-a55ab3518c9a)
  Exception while executing function: Functions.ManageSubscriptions
  Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ManageSubscriptions ---&gt; System.Net.Http.HttpR
  equestException: Response status code does not indicate success: 400 (Bad Request).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at WebHooksSample.Functions.&lt;ManageSubscriptions&gt;d__14.MoveNext() in D:\354132\office365_sample_app\webjobs\webjobs-webhooks-sample-master\WebHooks
  Sample\Functions.cs:line 128
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.&lt;InvokeAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;InvokeAsync&gt;d__22.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithWatchersAsync&gt;d__21.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithLoggingAsync&gt;d__19.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithLoggingAsync&gt;d__13.MoveNext()
     --- End of inner exception stack trace ---
  Exception while executing function: Functions.ManageSubscriptions
  Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ManageSubscriptions ---&gt; System.Net.Http.HttpR
  equestException: Response status code does not indicate success: 400 (Bad Request).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at WebHooksSample.Functions.&lt;ManageSubscriptions&gt;d__14.MoveNext() in D:\354132\office365_sample_app\webjobs\webjobs-webhooks-sample-master\WebHooks
  Sample\Functions.cs:line 128
  --- End of stack trace from previous location where exception was thrown ---
  
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.&lt;InvokeAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;InvokeAsync&gt;d__22.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithWatchersAsync&gt;d__21.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at `enter code here`System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithLoggingAsync&gt;d__19.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithLoggingAsync&gt;d__13.MoveNext()
     --- End of inner exception stack trace ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;ExecuteWithLoggingAsync&gt;d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.&lt;TryExecuteAsync&gt;d__10.MoveNext()
  The next 5 occurrences of the schedule will be:
  11/16/2018 7:06:03 PM
  11/16/2018 7:06:33 PM
  11/16/2018 7:07:03 PM
  11/16/2018 7:07:33 PM
  11/16/2018 7:08:03 PM
  Job host started
  Executing 'Functions.ManageSubscriptions' (Reason='Timer fired at 2018-11-16T19:05:57.4122054+05:30', Id=405fe97b-2b8a-48bb-a761-7c46a293622c)
  Exception while executing function: Functions.ManageSubscriptions
  Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ManageSubscriptions ---&gt; System.Net.Http.HttpR
  equestException: Response status code does not indicate success: 400 (Bad Request).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at WebHooksSample.Functions.&lt;ManageSubscriptions&gt;d__14.MoveNext() in D:\354132\office365_sample_app\webjobs\webjobs-webhooks-sample-master\WebHooks
  Sample\Functions.cs:line
  </code></pre>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I was able to accomplish receiving emails (without a user logging in) using outlook connector and office 365 webhook API (using default connector available in azure logic App) similar to what is described <a href="https://docs.microsoft.com/en-us/azure/event-grid/publish-iot-hub-events-to-logic-apps" rel="nofollow noreferrer">here</a>. However, outlook connector polls for every x seconds (custom value set by me) for any change in the mailbox, but I want the service to automatically trigger the Webhook Event without polling. If I understand it correctly, it should be like this - </p>
  
  <ol>
  <li><p>Create a webhook that is registered to watch for events </p></li>
  <li><p>Create an outlook connector that uses above webhook.</p></li>
  <li><p>Outlook connector notifies application (REST API call) as part of workflow.</p></li>
  </ol>