Example #1
0
        public static List <History> ListHistory(GmailService service, String userId, ulong startHistoryId, List <Message> messages)
        {
            List <History> result = new List <History>();

            UsersResource.HistoryResource.ListRequest request = service.Users.History.List(userId);
            request.HistoryTypes   = UsersResource.HistoryResource.ListRequest.HistoryTypesEnum.MessageAdded;
            request.StartHistoryId = startHistoryId;
            request.LabelId        = "INBOX";


            do
            {
                try
                {
                    ListHistoryResponse response = request.Execute();
                    if (response.History != null)
                    {
                        result.AddRange(response.History);
                        //foreach (History history in response.History)
                        //{
                        //    foreach (HistoryMessageAdded messagesadded in history.MessagesAdded)
                        //        messages.Add(messagesadded.Message);

                        //}

                        foreach (HistoryMessageAdded messageadded in response.History.SelectMany(i => i.MessagesAdded))
                        {
                            if (!messageadded.Message.LabelIds.Contains("SENT"))
                            {
                                messages.Add(messageadded.Message);
                            }
                        }
                    }
                    request.PageToken = response.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                }
            } while (!String.IsNullOrEmpty(request.PageToken));

            return(result);
        }
Example #2
0
        /* Returns the list of messagesId since startHistoryId. Used in partial sync */
        public static List <string> ListHistory(GmailService service, String userId, ulong startHistoryId)
        {
            List <string> messageIdList = null;

            UsersResource.HistoryResource.ListRequest request = service.Users.History.List(userId);
            request.StartHistoryId = startHistoryId;
            request.HistoryTypes   = HistoryTypesEnum.MessageAdded;

            do
            {
                try {
                    ListHistoryResponse response = request.Execute();

                    //If there's a list of history records, then we need to store the messageId of each new record
                    if (response.History != null)
                    {
                        messageIdList = new List <string>();
                        foreach (var historyRecord in response.History)
                        {
                            string newMessageId = historyRecord.MessagesAdded[0].Message.Id;
                            messageIdList.Add(newMessageId);
                        }
                    }

                    //Then we update the historyId with the latest one
                    ulong retrievedHistId = (ulong)response.HistoryId;
                    UpdateHistoryId(retrievedHistId.ToString());

                    request.PageToken = response.NextPageToken;
                } catch (Exception e) {
                    Debug.WriteLine("An error occurred: " + e.Message);
                }
            } while (!String.IsNullOrEmpty(request.PageToken));

            return(messageIdList);
        }