/// <summary>
        /// Execute the request to get a list of threads
        /// </summary>
        /// <param name="service">The GmailService to use</param>
        /// <returns>A list of threads from the users inbox</returns>
        private static IList <Thread> GetThreads(GmailService service)
        {
            UsersResource.ThreadsResource.ListRequest request = service.Users.Threads.List("me");
            var threads = request.Execute().Threads;

            return(threads);
        }
示例#2
0
        public ListThreadsResponse GetThreadList(Initializer initializer, string userId)
        {
            if (String.IsNullOrEmpty(userId))
            {
                return(null);
            }

            GmailService svc = new GmailService(CreateInitializer(initializer));

            UsersResource.ThreadsResource.ListRequest lRequest = svc.Users.Threads.List(userId);
            return(lRequest.Execute());
        }
示例#3
0
    // ...

    /// <summary>
    /// List all message threads of the user's mailbox.
    /// </summary>
    /// <param name="service">Gmail API service instance.</param>
    /// <param name="userId">User's email address. The special value "me"
    /// can be used to indicate the authenticated user.</param>
    private static List <Thread> ListThreads(GmailService service, String userId, int numMessages)
    {
        List <Thread> result = new List <Thread>();

        UsersResource.ThreadsResource.ListRequest request = service.Users.Threads.List(userId);
        request.MaxResults = numMessages;
        request.LabelIds   = "INBOX";
        ListThreadsResponse response = request.Execute();

        result.AddRange(response.Threads);

        return(result);
    }
示例#4
0
        public List <Thread> ListThreads(GmailService service, String userId)
        {
            List <Thread> result = new List <Thread>();

            UsersResource.ThreadsResource.ListRequest request = service.Users.Threads.List(userId);

            do
            {
                try
                {
                    ListThreadsResponse response = request.Execute();
                    result.AddRange(response.Threads);
                    request.PageToken = response.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                }
            } while (!String.IsNullOrEmpty(request.PageToken));

            return(result);
        }
示例#5
0
        public static Dictionary <Google.Apis.Gmail.v1.Data.Thread, string> GetGmailThreads(string query, int returnAmount)
        {
            AuthorizeGmail();

            UsersResource.ThreadsResource.ListRequest request = gmailService.Users.Threads.List("me");
            request.Q          = query;
            request.MaxResults = returnAmount;
            ListThreadsResponse response = request.Execute();
            List <Google.Apis.Gmail.v1.Data.Thread> threads = response.Threads.ToList();
            List <string> threadSubjects = GetThreadSubjects(threads);

            Dictionary <Google.Apis.Gmail.v1.Data.Thread, string> threadDictionary = new Dictionary <Google.Apis.Gmail.v1.Data.Thread, string>();

            for (int i = 0; i < threads.Count; i++)
            {
                threadDictionary.Add(threads[i], threadSubjects[i]);
            }



            return(threadDictionary);
        }
示例#6
0
        public static int CountMails(int KindOfScan)
        {
            string Label = string.Empty;

            if (KindOfScan == 1)
            {
                Label = "INBOX";
            }
            else if (KindOfScan == 2)
            {
                Label = "SENT";
            }
            else if (KindOfScan == 3)
            {
                Label = "DRAFTS";
            }
            // Check connection
            if (CheckConnection.Check() == false)
            {
                return(0);
            }
            // Define parameter for request
            UsersResource.ThreadsResource.ListRequest ThreadRequest = Connect.service.Users.Threads.List("me");
            ThreadRequest.LabelIds = Label;
            // Get mail include spam trash?
            ThreadRequest.MaxResults       = int.MaxValue;
            ThreadRequest.IncludeSpamTrash = false; // NO
            // Make a request
            try
            {
                return(ThreadRequest.ExecuteAsync().Result.Threads.Count());
            }
            catch (Exception)
            {
                return(0);
            }
        }
示例#7
0
        List <Threads> obtenerCorreos()
        {
            List <Threads> correos = new List <Threads>();
            UserCredential credential;

            using (var stream =
                       new FileStream(HttpContext.Current.Server.MapPath("~/credentials2.json"), FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = HttpContext.Current.Server.MapPath("~/token2.json");
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Gmail API service.
            var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            List <String> xd = new List <String>();

            xd.Add("0");
            xd.Add("1");
            // Define parameters of request
            UsersResource.ThreadsResource.ListRequest request = service.Users.Threads.List("me");
            request.MaxResults = 5;
            // List labels.
            var labels = request.Execute().Threads;

            //Console.WriteLine("Labels:");
            if (labels != null && labels.Count > 0)
            {
                foreach (var labelItem in labels)
                {
                    //labelItem.ToString();
                    Threads correo = new Threads();
                    correo.desc    = labelItem.Snippet;
                    correo.subject = "";

                    correo.from = "";
                    correo.date = "";
                    try
                    {
                        var messagesx = service.Users.Messages.Get("me", labelItem.Id).Execute();

                        if (messagesx != null)
                        {
                            //correo.date = Convert.ToString(messagesx.InternalDate.Value);
                            foreach (var headers in messagesx.Payload.Headers)
                            {
                                if (headers.Name == "Subject")
                                {
                                    correo.subject = headers.Value;
                                }
                                if (headers.Name == "From")
                                {
                                    correo.from = headers.Value;
                                }
                                if (headers.Name == "Date")
                                {
                                    correo.date = headers.Value;
                                }
                            }
                        }
                    }catch (Exception e)
                    {
                    }
                    correos.Add(correo);
                    //correos.Add
                    //   Console.WriteLine("{0}", labelItem.Name);
                }
            }
            else
            {
                Threads correo = new Threads();
                correo.desc = "no hay correos";
                correos.Add(correo);

                //   Console.WriteLine("No labels found.");
            }
            return(correos);
        }