gpt4 book ai didi

android - gmail-api 标记为 "read"

转载 作者:行者123 更新时间:2023-11-29 00:06:43 26 4
gpt4 key购买 nike

我正尝试使用 gmail-api 将邮件设为“已读”,如下所示,但它不起作用。

 ModifyMessageRequest mods = new ModifyMessageRequest()
.setRemoveLabelIds(new ArrayList<String>(Arrays.asList("UNREAD")));
com.google.api.services.gmail.model.Message message = null;
try {
message = mService.users().messages().modify(acct.sEmail, gmsailID, mods).execute();
} catch (IOException e) {
e.printStackTrace();
}
}

最佳答案

setRemoveLabelIds() 的参数不是标签字符串,而是标签 ID。因此,从下面的代码中获取标签 ID 并在您的函数中使用它

    List<String> labels = new ArrayList<String>();
ListLabelsResponse listResponse = service.users().labels().list(userId).execute();
for (Label label : listResponse.getLabels()) {
Label countLabel = service.users().labels().get(userId, label.getId()).execute();
if (countLabel.getName().equalsIgnoreCase("UNREAD"))
unreadId = countLabel.getId();
}
List<String> add = new ArrayList<String>();
List<String> remove = new ArrayList<String>();
remove.add(unreadId);
modifyMessage(service, userId, message.getId(), add, remove);


public static void modifyMessage(Gmail service, String userId, String messageId,
List<String> labelsToAdd, List<String> labelsToRemove) throws IOException {
ModifyMessageRequest mods = new ModifyMessageRequest().setAddLabelIds(labelsToAdd)
.setRemoveLabelIds(labelsToRemove);
Message message = service.users().messages().modify(userId, messageId, mods).execute();
}

关于android - gmail-api 标记为 "read",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com