using System; using System.Collections.Specialized; using MailBee; using MailBee.ImapMail; using DotNetOpenAuth.OAuth2; class Sample { static string GetAccessToken(string clientID, string clientSecret, string key) { UserAgentClient consumer; IAuthorizationState grantedAccess; // Second: Fetch Access Token StringDictionary parameters2 = new StringDictionary(); parameters2.Add(OAuth2.RedirectUriKey, "urn:ietf:wg:oauth:2.0:oob"); string uri = "https://accounts.google.com/o/oauth2/token"; AuthorizationServerDescription server = new AuthorizationServerDescription(); server.TokenEndpoint = new Uri(uri); server.ProtocolVersion = ProtocolVersion.V20; consumer = new UserAgentClient(server, clientID, clientSecret); consumer.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(clientSecret); IAuthorizationState authorizationState = new AuthorizationState(null); if (parameters2.ContainsKey(OAuth2.RedirectUriKey)) { authorizationState.Callback = new Uri(parameters2[OAuth2.RedirectUriKey]); } // Build a generic URL containing the auth code. // This is done here as we cannot modify the DotNetOpenAuth library // and the underlying method only allows parsing an URL as a method // of retrieving the AuthorizationState. string url = "http://example.com/?code=" + key; try { grantedAccess = consumer.ProcessUserAuthorization(new Uri(url), authorizationState); } catch (DotNetOpenAuth.Messaging.ProtocolException e) { Console.WriteLine(e.Message); return null; } return grantedAccess.AccessToken; } static void Main(string[] args) { MailBee.Global.LicenseKey = "MN700-..."; // Set these as described at https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2 string clientID = "1234567890.apps.googleusercontent.com"; string clientSecret = "1a2b3c4d5e6f7h"; string userEmail = "john.doe@gmail.com"; OAuth2 myOAuth2 = new OAuth2(clientID, clientSecret); StringDictionary parameters1 = new StringDictionary(); parameters1.Add(OAuth2.Scope, "https://mail.google.com/"); parameters1.Add(OAuth2.RedirectUriKey, "urn:ietf:wg:oauth:2.0:oob"); parameters1.Add(OAuth2.ResponseTypeKey, "code"); // First: Redirect to Authorization and Fire up the browser System.Diagnostics.Process.Start( myOAuth2.AuthorizeToken("https://accounts.google.com/o/oauth2/auth", parameters1)); Console.Write("Please enter the key: "); string key = Console.ReadLine().Trim(); // Second: Fetch Access Token string accessToken = GetAccessToken(clientID, clientSecret, key); if (accessToken == null) { return; } // Get XOAuth key for IMAP. string imapXOAuthKey = myOAuth2.GetXOAuthKey(userEmail, accessToken); Imap imp = new Imap(); // In the log, you can find the actual search queries sent to the server. imp.Log.Filename = "C:\\Temp\\log.txt"; imp.Log.Enabled = true; imp.Log.Clear(); imp.Connect("imap.gmail.com", 993); imp.Login(null, imapXOAuthKey, AuthenticationMethods.SaslOAuth2, AuthenticationOptions.None, null); imp.SelectFolder("INBOX"); // Processing of INBOX emails here. //... imp.Disconnect(); } }