Пример #1
0
        static void Main(string[] args)
        {
            string _client_id     = "1046123799103-7mk8g2iok1dv9fphok8v2kv82hiqb0q6.apps.googleusercontent.com";
            string _client_secret = "GeE-cD7PtraV0LqyoxqPnOpv";

            GAAutentication Autentication = GAAutentication.Autenticate(_client_id, _client_secret);
            int             i             = 1;
        }
        public static GAAutentication Autenticate(string _client_id, string _client_secret)
        {
            GAAutentication result = new GAAutentication();
           
            try
            {
               UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = _client_id, ClientSecret = _client_secret },
                                                                         new[] {"https://mail.google.com/ email" },
                                                                         "Lindau",
                                                                         CancellationToken.None,
                                                                         new FileDataStore("Analytics.Auth.Store")).Result;
            }
            catch (Exception ex)
            {

                int i = 1;
            }

            if (result.credential != null)
            {

                result.service = new PlusService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = result.credential,
                    ApplicationName = "Google mail",
                });

        
                Google.Apis.Plus.v1.Data.Person me = result.service.People.Get("me").Execute();

                Google.Apis.Plus.v1.Data.Person.EmailsData myAccountEmail =  me.Emails.Where(a => a.Type == "account").FirstOrDefault();

                // Connect to the IMAP server. The 'true' parameter specifies to use SSL
                // which is important (for Gmail at least)
                ImapClient ic = new ImapClient("imap.gmail.com", myAccountEmail.Value, result.credential.Token.AccessToken,
                                ImapClient.AuthMethods.SaslOAuth, 993, true);
                // Select a mailbox. Case-insensitive
                ic.SelectMailbox("INBOX");
                Console.WriteLine(ic.GetMessageCount());
                // Get the first *11* messages. 0 is the first message;
                // and it also includes the 10th message, which is really the eleventh ;)
                // MailMessage represents, well, a message in your mailbox
                MailMessage[] mm = ic.GetMessages(0, 10);
                foreach (MailMessage m in mm)
                {
                    Console.WriteLine(m.Subject + " " + m.Date.ToString());
                }
                // Probably wiser to use a using statement
                ic.Dispose();
            }
            return result;

        }
        public static GAAutentication Autenticate(string _client_id, string _client_secret)
        {
            GAAutentication result = new GAAutentication();

            try
            {
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
                    ClientId = _client_id, ClientSecret = _client_secret
                },
                                                                                        new[] { "https://mail.google.com/ email" },
                                                                                        "Lindau",
                                                                                        CancellationToken.None,
                                                                                        new FileDataStore("Analytics.Auth.Store")).Result;
            }
            catch (Exception ex)
            {
                int i = 1;
            }

            if (result.credential != null)
            {
                result.service = new PlusService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = result.credential,
                    ApplicationName       = "Google mail",
                });


                Google.Apis.Plus.v1.Data.Person me = result.service.People.Get("me").Execute();

                Google.Apis.Plus.v1.Data.Person.EmailsData myAccountEmail = me.Emails.Where(a => a.Type == "account").FirstOrDefault();

                // Connect to the IMAP server. The 'true' parameter specifies to use SSL
                // which is important (for Gmail at least)
                ImapClient ic = new ImapClient("imap.gmail.com", myAccountEmail.Value, result.credential.Token.AccessToken,
                                               ImapClient.AuthMethods.SaslOAuth, 993, true);
                // Select a mailbox. Case-insensitive
                ic.SelectMailbox("INBOX");
                Console.WriteLine(ic.GetMessageCount());
                // Get the first *11* messages. 0 is the first message;
                // and it also includes the 10th message, which is really the eleventh ;)
                // MailMessage represents, well, a message in your mailbox
                MailMessage[] mm = ic.GetMessages(0, 10);
                foreach (MailMessage m in mm)
                {
                    Console.WriteLine(m.Subject + " " + m.Date.ToString());
                }
                // Probably wiser to use a using statement
                ic.Dispose();
            }
            return(result);
        }
        public static GAAutentication Autenticate(string _client_id, string _client_secret)
        {
            GAAutentication result = new GAAutentication();

            //If you want to test new gmail account,
            //Go to the browser, log off, log in with the new account,
            //and then change this 'tmpUser'
            var tmpUser = "******";

            try
            {
                result.credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
                    ClientId = _client_id, ClientSecret = _client_secret
                },
                                                                                new[] { "https://mail.google.com/ email" },
                                                                                tmpUser,
                                                                                CancellationToken.None,
                                                                                new FileDataStore("Analytics.Auth.Store")).Result;
            }
            catch (Exception ex)
            {
            }

            if (result.credential != null)
            {
                result.service = new PlusService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = result.credential,
                    ApplicationName       = "Google mail",
                });


                Google.Apis.Plus.v1.Data.Person me = result.service.People.Get("me").Execute();

                Google.Apis.Plus.v1.Data.Person.EmailsData myAccountEmail = me.Emails.Where(a => a.Type == "account").FirstOrDefault();

                // Connect to the IMAP server. The 'true' parameter specifies to use SSL
                // which is important (for Gmail at least)
                ImapClient ic = new ImapClient("imap.gmail.com", myAccountEmail.Value, result.credential.Token.AccessToken,
                                               ImapClient.AuthMethods.SaslOAuth, 993, true);

                var listOfMainboxes = ic.ListMailboxes(string.Empty, "*");
                Console.WriteLine("ListMailboxes");
                foreach (Mailbox mb in listOfMainboxes)
                {
                    Console.WriteLine(mb.Name);
                    var original = Console.ForegroundColor;

                    var examine = ic.Examine(mb.Name);
                    if (examine != null)
                    {
                        //Count?
                        ic.SelectMailbox(mb.Name);
                        Console.WriteLine(" - Count: " + ic.GetMessageCount());

                        //Get One Email per folder
                        MailMessage[] mm = ic.GetMessages(0, 0);
                        Console.ForegroundColor = ConsoleColor.Blue;
                        foreach (MailMessage m in mm)
                        {
                            Console.WriteLine(" - " + m.Subject + " " + m.Date);
                        }
                        Console.ForegroundColor = original;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Error.WriteLine("* Folder doesn't exists: " + mb.Name);
                        Console.ForegroundColor = original;
                    }
                }

                /*
                 * // Select a mailbox. Case-insensitive
                 * ic.SelectMailbox("INBOX");
                 *
                 * Console.WriteLine(ic.GetMessageCount());
                 * // Get the first *11* messages. 0 is the first message;
                 * // and it also includes the 10th message, which is really the eleventh ;)
                 * // MailMessage represents, well, a message in your mailbox
                 * MailMessage[] mm = ic.GetMessages(0, 10);
                 * foreach (MailMessage m in mm)
                 * {
                 *  Console.WriteLine(m.Subject + " " + m.Date.ToString());
                 * }
                 * // Probably wiser to use a using statement
                 */
                ic.Dispose();
            }
            return(result);
        }