Ejemplo n.º 1
0
        public async Task <IActionResult> GetListEmail(string labelId, string nameLabel)
        {
            string            userEmail    = User.FindFirst(u => u.Type == ClaimTypes.Email).Value;
            GmailService      service      = GetService();
            List <My_Message> listMessages = new List <My_Message>();
            List <Message>    result       = new List <Message>();
            var emailListRequest           = service.Users.Messages.List(userEmail);

            emailListRequest.LabelIds         = labelId;
            emailListRequest.IncludeSpamTrash = false;
            emailListRequest.MaxResults       = 1000;
            ListMessagesResponse emailListResponse = await emailListRequest.ExecuteAsync();

            if (emailListResponse != null && emailListResponse.Messages != null)
            {
                foreach (Message email in emailListResponse.Messages)
                {
                    var emailInfoRequest  = service.Users.Messages.Get(userEmail, email.Id);
                    var emailInfoResponse = await emailInfoRequest.ExecuteAsync();

                    if (emailInfoResponse != null)
                    {
                        message         = new My_Message();
                        message.Id      = listMessages.Count + 1;
                        message.EmailId = email.Id;

                        foreach (var mPart in emailInfoResponse.Payload.Headers)
                        {
                            if (mPart.Name == "Date")
                            {
                                message.Date_Received = mPart.Value;
                            }
                            else if (mPart.Name == "From")
                            {
                                message.From = mPart.Value;
                                emailTo      = mPart.Value;
                            }
                            else if (mPart.Name == "Subject")
                            {
                                message.Title = mPart.Value;
                            }
                        }

                        listMessages.Add(message);
                    }
                }
            }

            ViewBag.Message = nameLabel;
            return(View("~/Views/Home/Index.cshtml", listMessages));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetAttachments(string LabelId, string nameLabel)
        {
            string            UserEmail    = User.FindFirst(c => c.Type == ClaimTypes.Email).Value;
            var               service      = GetService();
            List <My_Message> listMessages = new List <My_Message>();
            List <Message>    result       = new List <Message>();

            My_Message messages = new My_Message();

            // Define parameters of request.
            var emailListRequest = service.Users.Messages.List(UserEmail);

            emailListRequest.IncludeSpamTrash = false;
            emailListRequest.LabelIds         = LabelId;
            emailListRequest.MaxResults       = 1000;
            emailListRequest.Q = "has:attachment";

            // List messages.
            ListMessagesResponse emailListResponse = await emailListRequest.ExecuteAsync();

            if (emailListResponse != null && emailListResponse.Messages != null)
            {
                foreach (var email in emailListResponse.Messages)
                {
                    var emailInfoRequest  = service.Users.Messages.Get(UserEmail, email.Id);
                    var emailInfoResponse = await emailInfoRequest.ExecuteAsync();

                    if (emailInfoResponse != null)
                    {
                        My_Message message = new My_Message();
                        message.Id      = listMessages.Count + 1;
                        message.EmailId = email.Id;

                        foreach (var mParts in emailInfoResponse.Payload.Headers)
                        {
                            if (mParts.Name == "Date")
                            {
                                message.Date_Received = mParts.Value;
                            }
                            else if (mParts.Name == "From")
                            {
                                message.From = mParts.Value;
                            }
                            else if (mParts.Name == "Subject")
                            {
                                message.Title = mParts.Value;
                            }

                            IList <MessagePart> parts = emailInfoResponse.Payload.Parts;
                            foreach (MessagePart part in parts)
                            {
                                message.Attachment_Count = int.Parse(part.PartId);
                            }
                        }

                        listMessages.Add(message);
                        emailListRequest.PageToken = emailListResponse.NextPageToken;
                    }
                }
            }

            ViewBag.Message = nameLabel;
            return(View("~/Views/Home/Index.cshtml", listMessages));
        }