【问题标题】:How can i list some of my emails from my inbox in gmail using google api v3.0?如何使用 google api v3.0 在 gmail 中列出我的收件箱中的一些电子邮件?
【发布时间】:2023-03-21 16:36:01
【问题描述】:

此代码显示标签,我不确定它的作用或作用。 代码正在运行。

但现在我想在 listBox1 中列出我的 gmail listBox 中的最后 50 封电子邮件。 我已经创建了一个 client_secrets.json 文件,在该文件中我有我的 gmail 帐户...my@gmail.com 以及客户端密码和客户端 ID。

这是我尝试过的,但出现了一些错误:

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(GmailService,)


        }

        private GmailService GmailService()
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.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,
            });

            // Define parameters of request.
            UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");

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

        }
    }
}

第一个错误就行了:

static string[] Scopes = { GmailService.Scope.GmailReadonly };

在 GmailService 上我收到错误:

错误 1“Google_Gmail.Form1.GmailService()”是一个“方法”,在给定的上下文中无效

第二个这是我在调用方法 ListMessages 作为用户 ID 和查询时应该在构造函数中添加什么? userid 是我在 json 文件中的客户端 ID 吗?应该是什么查询?

【问题讨论】:

  • 好的发现我错误地调用了我的方法GmailService将其更改为GmailServices解决了所有错误。现在工作。

标签: c# .net winforms google-api


【解决方案1】:

经过一番谷歌搜索,我找到了这个

首先你必须获得list of messages 如您所见,here Message 对象包含消息的不可变 ID,之后使用带有 foreach 循环的 get method 和消息列表来检索最后 50 个电子邮件地址。

所有三个链接都有适当的 .net 文档

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2017-02-11
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2011-10-11
    • 2021-09-24
    • 2018-03-24
    • 2019-10-24
    相关资源
    最近更新 更多