【问题标题】:Gmail API to access, read/modify the delegated emails or shared gmail using C#使用 C# 访问、读取/修改委派电子邮件或共享 gmail 的 Gmail API
【发布时间】:2019-09-20 02:43:07
【问题描述】:

我可以使用其凭据通过 Gmail API 从我自己的 Gmail 帐户中读取/修改 gmail。

using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;

但现在,IT 运营部门为我提供了一个共享的 Gmail 或委托的 Gmail 帐户。所以我想访问、读取/修改该委托电子邮件

如何使用 Gmail API 访问和阅读委托 gmail? 有例子吗?

Gmail API 网站中的大部分代码都是 Python 和 Java,我不明白。

请点亮一些灯。

我尝试过的:

我可以使用 Gmail API 访问、阅读自己的邮件 -

private static string[] Scopes = { GmailService.Scope.GmailModify };
private UserCredential _credential;
private string credPath = "token.json";

 public UserCredential GetCredential()
 {
 using (var stream =
  new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
     {
         _credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
             GoogleClientSecrets.Load(stream).Secrets,
             Scopes,
             "user",
             CancellationToken.None,
             new FileDataStore(credPath, true)).Result;
     }

     return _credential;
 }

GmailCredentials Info = new GmailCredentials();
      private static string ApplicationName = "xxxxx";
      
        var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = GetCredential(),
                ApplicationName = ApplicationName,
            });
            
            UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me");
            request.Q = ConfigurationManager.AppSettings["filter"];
            request.MaxResults = Convert.ToInt64(ConfigurationManager.AppSettings["maxCount"]);  //5;
            messages = request.Execute().Messages;
            List<string> lstRemove = new List<string>() { "UNREAD" };
            
            /// Read the individual message and output as List<Email>
            for (int index = 0; index < messages.Count; index++)
            {
            //... Do the code...
            }

【问题讨论】:

  • 您是否考虑过使用 MailKit 连接到 Gmail?
  • 感谢您的回复。我没有使用 MailKit。但我只能使用 Gmail API 来阅读电子邮件。我可以通过 Gmail API 访问和阅读自己的帐户,但我不知道如何使用 C# 代码从 Gmail API 访问委托或共享电子邮件。

标签: c# asp.net gmail-api


【解决方案1】:

Gmail API 不适用于委派邮箱。只能访问用户自己的邮箱。要访问 G Suite 域中的所有用户邮件,请尝试使用服务帐号和 domain-wide delegation

【讨论】:

  • 感谢您的回复。是否有任何示例代码可用于在 C# 中使用服务帐户。
猜你喜欢
  • 2017-06-19
  • 2014-10-29
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 2019-03-08
相关资源
最近更新 更多