title: Send/Receive Email with Google OAuth 2 and ASP.Net MVC
body: <p>I have used the following in a class:</p>
  
  <pre><code>public class AppFlowMetaData : FlowMetadata
  {
      private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
      {
          ClientSecrets = new ClientSecrets
          {
              ClientId = "nnnnnnnnnnnnn.apps.googleusercontent.com",
              ClientSecret = "nnnnnnn-nnnnnnnnnnnnnnn"
          },
          Scopes = new[] { CalendarService.Scope.Calendar },
          DataStore = new FileDataStore("Calendar.Api.Auth.Store")
      });
  
      public override string GetUserId(System.Web.Mvc.Controller controller)
      {
          var user = controller.Session["user"];
          if (user == null)
          {
              user = Guid.NewGuid();
              controller.Session["user"] = user;
          }
          return user.ToString();
      }
  
      public override IAuthorizationCodeFlow Flow
      {
          get { return flow; }
      }
  }
  </code></pre>
  
  <p>And the following on a controller:</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; TestAsync(CancellationToken token)
      {
          var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetaData()).AuthorizeAsync(token);   
          if(result.Credential != null)
          {
              var service = new CalendarService(new BaseClientService.Initializer
              {
                  HttpClientInitializer = result.Credential,
                  ApplicationName = "Calendar Test"
              });
              IList&lt;CalendarListEntry&gt; results = service.CalendarList.List().Execute().Items;
              List&lt;Event&gt; model = new List&lt;Event&gt;();
              foreach (var calendar in results)
              {
                  Google.Apis.Calendar.v3.EventsResource.ListRequest request = service.Events.List(calendar.Id);
                  var events = request.Execute().Items;
  
                  foreach (var e in events)
                  {
                      model.Add(e);
                  }
              }
              return View(model);
          }
          else
          {
              return new RedirectResult(result.RedirectUri);
          }
      }
  </code></pre>
  
  <p>This works well and I wanted to be able to send mails using gmail using the same process but I cannot find a scope or any details in the Google Api Documentation (which quite frankly is awful) to do this based on the same process.  I have seen 3rd party libraries using Imap elsewhere but this relies on passing the username/password which I don't want to do.  </p>
  
  <p>Can anyone help me to alter the above to be used for Gmail?</p>