【发布时间】: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 访问委托或共享电子邮件。