我如何阅读/获取每个邮件正文文本内容以及每封邮件的标题以及来自?

时间:2015-08-22 23:24:26

标签: c# .net winforms google-api gmail-api

我的意思是获得一个像这种格式构建的List:

从消息

例如,在第一个索引的列表中,我将看到:

来自:Daniel To:Jhon消息:你好世界

在此代码中我现在使用所有我在结果变量中输入1445条消息,并且在我的gmail.com帐户中有点奇怪我看到我在收件箱中有374封电子邮件,所以为什么变量结果一直返回1445?我尝试将JSON文件更改为我的gmail帐户,然后更改为我的朋友gmail帐户,两种情况下都返回1445.如何根据JASON文件中的gmail帐户返回电子邮件数量?

并且在结果列表变量中,例如在索引0上,当我站在鼠标上时,我可以获得的是消息Id和消息ThreadId但是如何获取From和To以及Message body / text ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using System.Threading;
using System.IO;
using Google.Apis.Util.Store;

namespace Google_Gmail
{
    public partial class Form1 : Form
    {
        static string[] Scopes = { GmailService.Scope.GmailReadonly };
        static string ApplicationName = "Youtube Uploader";

        public Form1()
        {
            InitializeComponent();

            ListMessages(GmailServices(), "me", "");


        }

        private GmailService GmailServices()
        {
            UserCredential credential;

            using (var stream =
                new FileStream(@"C:\jason file\client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials");

                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,
            });

            return service;
        }

        public static List<Google.Apis.Gmail.v1.Data.Message> ListMessages(GmailService service, String userId, String query)
        {
            List<Google.Apis.Gmail.v1.Data.Message> result = new List<Google.Apis.Gmail.v1.Data.Message>();
            UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId);
            request.Q = query;

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

            return result;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

在我的gmail.com帐户中,我看到收件箱中有374封电子邮件,为什么呢   变量结果一直返回1445?

Users.Messages.List(userId)会返回您帐户中所有邮件的ID,而不仅仅是带有INBOX标签的邮件。尝试查询Q = "in:INBOX"

  

在结果列表变量里面,例如在索引0时,我   用鼠标站在我身上我得到的就是消息Id和   消息ThreadId,但我如何获取From和To以及Message   体/文本?

Messages.List只会显示邮件的ID及其所属的主题。您需要对每个Id使用Messages.Get请求来获取实际消息。

然后,您必须查看邮件的From - 标题,To - 标题和Body以获取所需信息。

相关问题