问题
I have the code below to watch a mailbox. As seen, in the lableIds, I only have one label, INBOX because I want to listen to only new messages. However, when I run this, it receives notifications every 30s or so with different messageid. Yet no change occurred in the INBOX, no new item was added/removed. How do I set my watch body to only listen to the INBOX for incoming messages?
certificate= new X509Certificate2("file.p12"), "password",
X509KeyStorageFlags.Exportable);
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceaccount)
{
User = username,//username being impersonated
Scopes = scopes
}.FromCertificate(certificate));
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationame,
});
WatchRequest body = new WatchRequest()
{
TopicName = "projects/projectid/topics/topicname",
LabelIds = new[] {"INBOX"}
}
string userId = "me";
UsersResource.WatchRequest watchRequest = service.Users.Watch(body, userId);
WatchResponse test = watchRequest.Execute();
回答1:
Gmail apparently ignores the requested filter data. This looks to be a known bug inside gmail. Checkout https://issuetracker.google.com/issues/37300052 to monitor progress, although it seems to be pretty quiet. I'd recommend filling out the bug report so it's at least repeated.
For my use case, I'm looking only for emails with a specific, custom label. It's possible to store the history id for that user (see https://developers.google.com/gmail/api/v1/reference/users/history/list), and since that's an epoch, only call the gmail api every X minutes or so. This reduces the number of calls you're making to gmail, but isn't perfect.
来源:https://stackoverflow.com/questions/43330396/gmail-api-watch-inbox-label-only