Пример #1
0
        public async Task <ActionResult> Inbox(int page = 1)
        {
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata(_authTokenService)).
                         AuthorizeAsync(CancellationToken.None);

            if (result.Credential == null)
            {
                return(new RedirectResult(result.RedirectUri));
            }
            else
            {
                var service = new GmailService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = "Gmail webapp"
                });

                List <string> labels = new List <string>()
                {
                    "INBOX"
                };

                var listReq = service.Users.Messages.List("me");
                listReq.LabelIds = new Google.Apis.Util.Repeatable <string>(labels);
                var response = await listReq.ExecuteAsync();

                int pageSize = 10;

                List <string> metadata = new List <string>()
                {
                    "Subject", "To", "From"
                };
                List <EmailViewModel> emailModels = new List <EmailViewModel>();
                for (int i = (page - 1) * 10; i < pageSize + page * 10 && i < response.Messages.Count(); i++)
                {
                    var msg    = response.Messages[i];
                    var getReq = service.Users.Messages.Get("me", msg.Id);
                    getReq.Format          = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
                    getReq.MetadataHeaders = new Google.Apis.Util.Repeatable <string>(metadata);
                    Message message = getReq.ExecuteAsync().Result;

                    emailModels.Add(EmailViewModel.FromEntity(GmailParser.FromGmailApi(message)));
                }

                return(View("Inbox", new InboxViewModel
                {
                    Emails = emailModels,
                    PagerList = new PagedList.PagedList <Message>(response.Messages.ToList(), page, pageSize)
                }));
            }
        }
Пример #2
0
        public async Task <ActionResult> Detail(string mailId)
        {
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata(_authTokenService)).
                         AuthorizeAsync(CancellationToken.None);

            if (result.Credential == null)
            {
                return(new RedirectResult(result.RedirectUri));
            }
            else
            {
                var service = new GmailService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = "Gmail webapp"
                });

                var getReq = service.Users.Messages.Get("me", mailId);
                getReq.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
                Message message = getReq.ExecuteAsync().Result;

                return(View(EmailViewModel.FromEntity(GmailParser.FromGmailApi(message))));
            }
        }