英文:
Is there any way of marking an email as read/seen using Gmail's API? Link to the API below
问题
我尝试浏览整个内容,还阅读了Java客户端的参考文档,但似乎没有办法实现这一点,这很奇怪,因为使用IMAP和Gmail可以将电子邮件标记为已读。
似乎只有可能向消息添加/移除标签。
有人能确认吗?
英文:
I tried going over the the entire thing, read the Java client reference too, but it doesn't seem that there is a way to do it, which is strange as it is possible to mark an email as read using IMAP wth Gmail.
It seems it's only possible to add/remove labels from messages
Can anyone confirm this?
答案1
得分: 1
你可以添加/删除UNREAD
标签,它将更新邮件的状态。在评论中已经提到了joshmeranda和Linda Lawton - DaImTo,但我想提供一个使用Java的示例代码。
这将通过交换消息的状态从READ
到UNREAD
或反之亦然来更新消息,使用给定的messageId:
String user = "A_USER_ID"; //<- use "me" to use the authenticated user's ID.
String messageId = "A_MESSAGE_ID";
Message message = service.users().messages().get(user, messageId).execute();
if (message.getLabelIds().contains("UNREAD")) {
ModifyMessageRequest modifyRequest = new ModifyMessageRequest()
.setRemoveLabelIds(Arrays.asList(new String[] { "UNREAD" }));
Message modifiedMessage = service.users().messages().modify(user, messageId, modifyRequest).execute();
} else {
ModifyMessageRequest modifyRequest = new ModifyMessageRequest()
.setAddLabelIds(Arrays.asList(new String[] { "UNREAD" }));
Message modifiedMessage = service.users().messages().modify(user, messageId, modifyRequest).execute();
}
参考链接:
-
Gmail API Java Quickstart 获取
Gmail service
对象。
英文:
You can add/remove the UNREAD
label and it will update the email status. It was already mentioned in the comments by joshmeranda and Linda Lawton - DaImTo but I wanted to provide a code as an example using Java.
This would update a message using a given messageId by swapping its status from READ
to UNREAD
or vice-versa:
String user = "A_USER_ID"; //<- use "me" to use the authenticated user's ID.
String messageId = "A_MESSAGE_ID";
Message message = service.users().messages().get(user, messageId).execute();
if (message.getLabelIds().contains("UNREAD")) {
ModifyMessageRequest modifyRequest = new ModifyMessageRequest()
.setRemoveLabelIds(Arrays.asList(new String[] { "UNREAD" }));
Message modifiedMessage = service.users().messages().modify(user, messageId, modifyRequest).execute();
} else {
ModifyMessageRequest modifyRequest = new ModifyMessageRequest()
.setAddLabelIds(Arrays.asList(new String[] { "UNREAD" }));
Message modifiedMessage = service.users().messages().modify(user, messageId, modifyRequest).execute();
}
Reference:
-
Gmail API Java Quickstart to get the
Gmail service
object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论