gmail-api 标记为 "read"

gmail-api marking as "read"

使用 gmail-api 我正在尝试将消息设为 "read" 如下所示,但它不起作用。

 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();
}

这是我使用的一个(在使用 ListMessagesResponse 获取 messageId 之后):

public void modifyMessage(String userId, String messageId) throws IOException {
        List<String> lblIDRemove=new ArrayList<String>();
        lblIDRemove.add("UNREAD");
        ModifyMessageRequest mods = new ModifyMessageRequest()
                .setAddLabelIds(null)
                .setRemoveLabelIds(lblIDRemove);
        Message message = mService.users().messages().modify(userId, messageId, mods).execute();
    }