【问题标题】:How can i read/get each message body text content and also the title of each message and from and to?我如何阅读/获取每条消息正文的文本内容以及每条消息的标题以及来自和去往?
【发布时间】:2015-11-16 15:53:46
【问题描述】:

我的意思是得到一个将像这种格式构建的列表:

从到消息

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

发件人:Daniel 收件人:Jhon 消息:hello world

在我现在使用的这段代码中,我在结果变量中得到的只是 1445 条消息,这在我的 gmail.com 帐户中很奇怪,我看到收件箱中有 374 封电子邮件,所以为什么变量结果总是返回1445 ?我尝试将 JSON 文件更改为我的 gmail 帐户,然后更改为我朋友的 gmail 帐户,在这两种情况下它都返回 1445。我怎样才能让它根据 JASON 文件中的 gmail 帐户返回电子邮件数量?

在结果列表变量中,例如在索引 0 上,当我用鼠标站在它上面时,我只能得到消息 Id 和消息 ThreadId,但是我如何获取 From 和 To 以及消息正文/文本?

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)
        {

        }
    }
}

【问题讨论】:

  • 未标记答案。

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


【解决方案1】:

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

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

在结果列表变量中,例如在索引 0 上,当 i 用鼠标站在上面,我能得到的只是消息 ID 和 消息 ThreadId 但我如何获取 From 和 To 以及 Message 正文/文本?

Messages.List 只会给你消息的 ID 和它所属的线程。您需要对每个 Id 使用 Messages.Get 请求来获取实际消息。

然后,您必须查看消息的From-header、To-header 和Body,以获得您需要的信息。

【讨论】:

  • 我将查询字符串改为:ListMessages(GmailServices(), "me", "in:INBOX");现在变量结果给我 1059 它不像以前那样 1445 但仍然不是我在我的 gmail 帐户中看到的电子邮件数量。
  • 我现在尝试了“in:inbox”小写,还尝试将“me”更改为我的 gmail 帐户:“myccount@gmail.com”在所有情况下它都会在变量结果中返回 1059。
  • 您是指您在 Gmail 客户端中的 Inbox-标签旁边看到的数字吗?那是未读消息的计数器。试试in:INBOX AND is:unread
  • 在:INBOX AND is:unread 中执行查询,给我 950,如果我将它的结尾从未读更改为已读,则为 109。是的,我的意思是当我使用 chrome 时,我正在浏览 gmail.com进入我的帐户,然后在左侧点击收件箱,我现在看到 377。
  • 我现在在 gmail.com 网站上看到您可以选择主要或社交或承诺。所以在 Primary 我有 463 封关于 Social 331 和 Promotions 200 的电子邮件,也许这只是一个不确定的过滤器。在左侧,我看到红色收件箱(377)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多