Exemplo n.º 1
0
        static public List <string> GetGmailMessages(DateTime date)
        {
            List <string> messagesList = new List <string>();

            var service = GetService();

            UsersResource.MessagesResource.ListRequest request = RequestForGmailMessages(service);

            try
            {
                var response = request.Execute();

                if (response.Messages != null)
                {
                    foreach (var mail in response.Messages)
                    {
                        var mailId   = mail.Id;
                        var threadId = mail.ThreadId;

                        var messageRequest = service.Users.Messages.Get("me", mailId);
                        messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
                        var message = messageRequest.Execute();

                        var payload = message.Payload;
                        var parts   = payload.Parts;

                        if (parts != null && parts.Count > 0)
                        {
                            foreach (var part in parts)
                            {
                                var body = part.Body.Data;

                                if (body != null)
                                {
                                    var decodeBody = Base64UrlEncoder.Decode(body);
                                    decodeBody = EmailParser.ParseMailsByDay(decodeBody, date);
                                    Console.WriteLine(decodeBody);

                                    if (decodeBody != null)
                                    {
                                        messagesList.Add(decodeBody);
                                    }
                                }
                            }
                        }
                        else
                        {
                            var body = payload.Body.Data;

                            if (body != null)
                            {
                                var decodeBody = Base64UrlEncoder.Decode(body);
                                decodeBody = EmailParser.ParseMailsByDay(decodeBody, date);
                                Console.WriteLine(decodeBody);

                                if (decodeBody != null)
                                {
                                    messagesList.Add(decodeBody);
                                }
                            }
                        }
                    }
                }

                messagesList.Reverse();

                return(messagesList);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Exemplo n.º 2
0
        static public List <string> GetGmailMessages(int number = 5)
        {
            List <string> messagesList = new List <string>();

            var service = GetService();

            var request = RequestForGmailMessages(service);

            try
            {
                var response = request.Execute();

                if (response.Messages != null)
                {
                    int  count = 0;
                    bool all   = false;

                    foreach (var mail in response.Messages)
                    {
                        var mailId   = mail.Id;
                        var threadId = mail.ThreadId;

                        var messageRequest = service.Users.Messages.Get("me", mailId);
                        messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
                        var message = messageRequest.Execute();

                        var payload = message.Payload;
                        var parts   = payload.Parts;

                        if (parts == null && parts.Count > 0)
                        {
                            foreach (var part in parts)
                            {
                                var body = part.Body.Data;

                                if (body != null)
                                {
                                    var decodeBody = Base64UrlEncoder.Decode(body);
                                    decodeBody = EmailParser.ParseMail(decodeBody);
                                    Console.WriteLine(decodeBody);

                                    if (decodeBody != null)
                                    {
                                        messagesList.Add(decodeBody);
                                        count++;
                                    }
                                }

                                if (count >= number)
                                {
                                    all = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            var body = payload.Body.Data;

                            if (body != null)
                            {
                                var decodeBody = Base64UrlEncoder.Decode(body);
                                decodeBody = EmailParser.ParseMail(decodeBody);
                                Console.WriteLine(decodeBody);

                                if (decodeBody != null)
                                {
                                    messagesList.Add(decodeBody);
                                }
                            }
                        }

                        if (all)
                        {
                            break;
                        }
                    }
                }

                messagesList.Reverse();

                return(messagesList);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(new List <string>());
            }
        }