使用Gmail的API有办法将邮件标记为已读吗?以下是API链接:

huangapple go评论132阅读模式
英文:

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标签,它将更新邮件的状态。在评论中已经提到了joshmerandaLinda Lawton - DaImTo,但我想提供一个使用Java的示例代码。

这将通过交换消息的状态从READUNREAD或反之亦然来更新消息,使用给定的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();
        }

参考链接:

英文:

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 = &quot;A_USER_ID&quot;; //&lt;- use &quot;me&quot; to use the authenticated user&#39;s ID.
        String messageId = &quot;A_MESSAGE_ID&quot;;
        Message message = service.users().messages().get(user, messageId).execute();

        if (message.getLabelIds().contains(&quot;UNREAD&quot;)) {
            ModifyMessageRequest modifyRequest = new ModifyMessageRequest()
                    .setRemoveLabelIds(Arrays.asList(new String[] { &quot;UNREAD&quot; }));
            Message modifiedMessage = service.users().messages().modify(user, messageId, modifyRequest).execute();
        } else {
            ModifyMessageRequest modifyRequest = new ModifyMessageRequest()
                    .setAddLabelIds(Arrays.asList(new String[] { &quot;UNREAD&quot; }));
            Message modifiedMessage = service.users().messages().modify(user, messageId, modifyRequest).execute();
        }

Reference:

huangapple
  • 本文由 发表于 2023年4月6日 23:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定