Working with Message Flags on Server

Changing the Message Flags

You can change message flags by using the changeMessageFlags() method. This method takes two parameters.

  1. The message sequence number or unique ID.
  2. MessageFlag.

The following flags can be set:

Setting Message Flags

The following code snippet shows you how to change message flags on an IMAP server with Aspose.Email.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Mark the message as read
client.changeMessageFlags(1, ImapMessageFlags.isRead());

Removing Message Flags

Message flags can also be removed with the removeMessageFlags() method. Usage is similar to that of the changeMessageFlags() method. It takes a sequence number or unique message ID and MessageFlag. The following code snippet shows you how to remove message flags.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Remove the message flag
client.removeMessageFlags(1, ImapMessageFlags.isRead());

Setting Custom Flags

You can also set custom flags to a message using the ImapClient of the API. The ImapClient AddMessageFlags provides the ability to set custom flags on messages.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create a message
MailMessage message = new MailMessage("user@domain1.com", "user@domain2.com", "subject", "message");

// Append the message to mailbox
String uid = client.appendMessage(ImapFolderInfo.IN_BOX, message);

// Add custom flags to the added messge
client.addMessageFlags(uid, com.aspose.email.ImapMessageFlags.op_BitwiseOr(ImapMessageFlags.keyword("custom1"), ImapMessageFlags.keyword("custom1_0")));

// Retreive the messages for checking the presence of custom flag
client.selectFolder(ImapFolderInfo.IN_BOX);

ImapMessageInfoCollection messageInfos = client.listMessages();
for (ImapMessageInfo inf : messageInfos) {
    ImapMessageFlags[] flags = inf.getFlags().split();

    if (inf.containsKeyword("custom1"))
        System.out.println("Keyword found");
}