Làm việc với các cờ tin nhắn trên máy chủ

Thay đổi Cờ Tin Nhắn

Bạn có thể thay đổi cờ tin nhắn bằng cách sử dụng changeMessageFlags() phương thức. Phương thức này nhận hai tham số.

  1. Số thứ tự tin nhắn hoặc ID duy nhất.
  2. MessageFlag.

Các cờ sau có thể được đặt:

Đặt cờ tin nhắn

Đoạn mã mẫu sau cho bạn thấy cách thay đổi cờ tin nhắn trên máy chủ IMAP với 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());

Xóa cờ tin nhắn

Các cờ tin nhắn cũng có thể bị xóa bằng removeMessageFlags() phương thức. Cách sử dụng tương tự như của changeMessageFlags() phương thức. Nó nhận một số thứ tự hoặc ID tin nhắn duy nhất và MessageFlag. Đoạn mã mẫu sau cho bạn thấy cách xóa cờ tin nhắn.

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

Đặt cờ tùy chỉnh

Bạn cũng có thể đặt cờ tùy chỉnh cho một tin nhắn bằng cách sử dụng ImapClient của API. ImapClient AddMessageFlags cung cấp khả năng đặt cờ tùy chỉnh trên các tin nhắn.

// 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");
}