ทำงานกับข้อความจากเซิร์ฟเวอร์ IMAP
แสดงรายการ ID ข้อความ MIME จากเซิร์ฟเวอร์
ImapMessageInfo ให้บริการ MIME MessageId สำหรับการระบุข้อความโดยไม่ต้องดึงข้อความเต็ม โค้ดตัวอย่างต่อไปนี้แสดงวิธีแสดงรายการ MIME messageId
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapClient client = new ImapClient();
client.setHost("domain.com");
client.setUsername("username");
client.setPassword("password");
try {
ImapMessageInfoCollection messageInfoCol = client.listMessages("Inbox");
for (ImapMessageInfo info : messageInfoCol) {
// Display MIME Message ID
System.out.println("Message Id = " + info.getMessageId());
}
} catch (java.lang.RuntimeException ex) {
System.out.println(ex.getMessage());
}
แสดงรายการข้อความจากเซิร์ฟเวอร์
Aspose.Email มีรูปแบบ overload 2 สมาชิกของ listMessages() เพื่ดึงจำนวนข้อความที่ระบุตามคำค้น โค้ดตัวอย่างต่อไปนี้แสดงวิธีแสดงรายการข้อความ
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an imapclient with host, user and password
ImapClient client = new ImapClient("localhost", "user", "password");
// Select the inbox folder and Get the message info collection
ImapQueryBuilder builder = new ImapQueryBuilder();
MailQuery query = builder
.or(builder.or(builder.or(builder.or(builder.getSubject().contains(" (1) "), builder.getSubject().contains(" (2) ")), builder.getSubject().contains(" (3) ")),
builder.getSubject().contains(" (4) ")), builder.getSubject().contains(" (5) "));
ImapMessageInfoCollection messageInfoCol4 = client.listMessages(query, 4);
System.out.println((messageInfoCol4.size() == 4) ? "Success" : "Failure");
แสดงรายการข้อความจากเซิร์ฟเวอร์แบบเรียงลำดับ
โปรโตคอล IMAP รองรับการแสดงรายการข้อความแบบเรียงลำดับจากโฟลเดอร์กล่องจดหมาย ซึ่งช่วยให้แสดงข้อความจากโฟลเดอร์ย่อยของโฟลเดอร์ได้ด้วย โค้ดตัวอย่างต่อไปนี้แสดงวิธีแสดงรายการข้อความแบบเรียงลำดับ
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an imapclient with host, user and password
ImapClient client = new ImapClient();
client.setHost("domain.com");
client.setUsername("username");
client.setPassword("password");
client.selectFolder("InBox");
ImapMessageInfoCollection msgsColl = client.listMessages(true);
System.out.println("Total Messages: " + msgsColl.size());
Listing Messages with MultiConnection
ImapClient ให้การ UseMultiConnection คุณสมบัติที่สามารถใช้สร้างการเชื่อมต่อหลาย ๆ ครั้งสำหรับงานที่มีภาระหนัก. คุณยังสามารถตั้งค่าจำนวนการเชื่อมต่อที่จะใช้ในโหมดหลายการเชื่อมต่อโดยใช้ ImapClient.ConnectionsQuantity. โค้ดตัวอย่างต่อไปนี้แสดงการใช้โหมดหลายการเชื่อมต่อสำหรับการแสดงรายการข้อความและเปรียบเทียบประสิทธิภาพกับโหมดการเชื่อมต่อเดี่ยว
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapClient imapClient = new ImapClient();
imapClient.setHost("<HOST>");
imapClient.setPort(993);
imapClient.setUsername("<USERNAME>");
imapClient.setPassword("<PASSWORD>");
imapClient.setSupportedEncryption(EncryptionProtocols.Tls);
imapClient.setSecurityOptions(SecurityOptions.SSLImplicit);
imapClient.selectFolder("Inbox");
imapClient.setConnectionsQuantity(5);
imapClient.setUseMultiConnection(MultiConnectionMode.Enable);
long multiConnectionModeStartTime = System.currentTimeMillis();
ImapMessageInfoCollection messageInfoCol1 = imapClient.listMessages(true);
long multiConnectionModeTimeSpan = System.currentTimeMillis() - multiConnectionModeStartTime;
imapClient.setUseMultiConnection(MultiConnectionMode.Disable);
long singleConnectionModeStartTime = System.currentTimeMillis();
ImapMessageInfoCollection messageInfoCol2 = imapClient.listMessages(true);
long singleConnectionModeTimeSpan = System.currentTimeMillis() - singleConnectionModeStartTime;
double performanceRelation = singleConnectionModeTimeSpan / multiConnectionModeTimeSpan;
System.out.println("Performance Relation: " + performanceRelation);
รับข้อความในลำดับลดลง
Aspose.Email ให้บริการ ImapClient.listMessagesByPage เมธอดที่แสดงรายการข้อความพร้อมการรองรับการแบ่งหน้า การโอเวอร์โหลดบางส่วนของ ImapClient.listMessagesByPage รับ PageSettings เป็นพารามิเตอร์. PageSettings ให้การ AscendingSorting คุณสมบัติที่เมื่อตั้งค่าเป็น false จะคืนค่าอีเมลในลำดับลดลง.
โค้ดตัวอย่างต่อไปนี้สาธิตการใช้ของ AscendingSorting คุณสมบัติของ PageSettings คลาสเพื่อเปลี่ยนออเดอร์ของอีเมล.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapClient imapClient = new ImapClient();
imapClient.setHost("<HOST>");
imapClient.setPort(993);
imapClient.setUsername("<USERNAME>");
imapClient.setPassword("<PASSWORD>");
imapClient.setSupportedEncryption(EncryptionProtocols.Tls);
imapClient.setSecurityOptions(SecurityOptions.SSLImplicit);
PageSettings pageSettings = new PageSettings();
pageSettings.setAscendingSorting(false);
ImapPageInfo pageInfo = imapClient.listMessagesByPage(5, pageSettings);
ImapMessageInfoCollection messages = pageInfo.getItems();
for (ImapMessageInfo message : messages) {
System.out.println(message.getSubject() + " -> " + message.getDate().toString());
}
ดึงข้อความจากเซิร์ฟเวอร์และบันทึกลงดิสก์
นี้ ImapClient คลาสสามารถดึงข้อความจากเซิร์ฟเวอร์ IMAP และบันทึกข้อความในรูปแบบ EML ลงดิสก์ท้องถิ่น ขั้นตอนต่อไปนี้จำเป็นเพื่อนำข้อความบันทึกลงดิสก์:
- สร้างอินสแตนซ์ของ ImapClient คลาส.
- ระบุชื่อโฮสต์, พอร์ต, ชื่อผู้ใช้, และรหัสผ่านใน ImapClient คอนสตรัคเตอร์.
- เลือกโฟลเดอร์โดยใช้ selectFolder() เมธอด.
- เรียกใช้ listMessages เมธอดเพื่อดึง ImapMessageInfoCollection อ็อบเจ็กต์.
- วนซ้ำผ่าน ImapMessageInfoCollection คอลเล็กชัน เรียก saveMessage() เมธอดและกำหนดเส้นทางเอาต์พุตและชื่อไฟล์.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดึงข้อความอีเมลจากเซิร์ฟเวอร์และบันทึกลง
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Select the inbox folder and Get the message info collection
client.selectFolder(ImapFolderInfo.IN_BOX);
ImapMessageInfoCollection list = client.listMessages();
// Download each message
for (int i = 0; i < list.size(); i++) {
// Save the EML file locally
client.saveMessage(list.get_Item(i).getUniqueId(), dataDir + list.get_Item(i).getUniqueId() + ".eml");
}
บันทึกข้อความในรูปแบบ MSG
ในตัวอย่างข้างต้น, อีเมลจะถูกบันทึกในรูปแบบ EML หากต้องการบันทึกอีเมลในรูปแบบ MSG, ให้ ImapClient.fetchMessage() ต้องเรียกเมธอดนี้ มันจะคืนข้อความในอินสแตนซ์ของ MailMessage คลาส. เมธอด MailMessage.save() เมธอดนั้นสามารถเรียกเพื่อบันทึกข้อความเป็นไฟล์ MSG ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกข้อความในรูปแบบ MSG.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the file directory.
String dataDir = "data/";
// Create an imapclient with host, user and password
ImapClient client = new ImapClient("localhost", "user", "password");
// Select the inbox folder and Get the message info collection
client.selectFolder(ImapFolderInfo.IN_BOX);
ImapMessageInfoCollection list = client.listMessages();
// Download each message
for (int i = 0; i < list.size(); i++) {
// Save the message in MSG format
MailMessage message = client.fetchMessage(list.get_Item(i).getUniqueId());
message.save(dataDir + list.get_Item(i).getUniqueId() + "_out.msg", SaveOptions.getDefaultMsgUnicode());
}
การดึงข้อความแบบกลุ่ม
ImapClient ให้การ fetchMessages เมธอดที่รับ iterable ของลำดับหมายเลขหรือ Unique ID และคืนค่าเป็นรายการของ MailMessage. โค้ดสแนปต่อไปนี้แสดงการใช้ fetchMessages เมธอดเพื่อดึงข้อความตามลำดับหมายเลขและ ID ที่ไม่ซ้ำกัน.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapClient imapClient = new ImapClient();
imapClient.setHost("<HOST>");
imapClient.setPort(993);
imapClient.setUsername("<USERNAME>");
imapClient.setPassword("<PASSWORD>");
imapClient.setSupportedEncryption(EncryptionProtocols.Tls);
imapClient.setSecurityOptions(SecurityOptions.SSLImplicit);
ImapMessageInfoCollection messageInfoCol = imapClient.listMessages();
System.out.println("ListMessages Count: " + messageInfoCol.size());
List<Integer> sequenceNumberAr = new ArrayList<Integer>();
List<String> uniqueIdAr = new ArrayList<String>();
for (ImapMessageInfo mi : messageInfoCol) {
sequenceNumberAr.add(mi.getSequenceNumber());
uniqueIdAr.add(mi.getUniqueId());
}
for (MailMessage m : imapClient.fetchMessagesBySequences(sequenceNumberAr)) {
System.out.println("FetchMessages-sequenceNumberAr : " + m.getSubject());
}
for (MailMessage m : imapClient.fetchMessagesByUids(uniqueIdAr)) {
System.out.println("FetchMessages-uniqueIdAr : " + m.getSubject());
}
การจัดเธรดอีเมล/จัดอีเมลเป็นการสนทนา
Aspose.Email ทำให้สามารถจัดกลุ่มข้อความต่อไปนี้ (ต่อ, ตอบ, ตอบทั้งหมด) ที่เกี่ยวข้องกับการสนทนาเดียวกันในแบบลำดับชั้น ได้ โดยพื้นฐานแล้วโปรโตคอล IMAP อาจรองรับความสามารถ THREAD ตามที่กำหนดใน RFC-5256 นอกจากนี้ยังมีส่วนขยาย IMAP อีกส่วนที่ Gmail ให้มาและอธิบายเป็น X-GM-EXT-1.
คุณลักษณะการจัดเธรดอีเมลต่อไปนี้พร้อมใช้งาน:
- getMessageThreads method - รับเธรดของข้อความโดย ImapClient.
- boolean getGmExt1Supported - รับข้อมูลว่ามีการรองรับส่วนขยาย Gmail X-GM-EXT-1 หรือไม่.
- boolean getThreadSupported - รับข้อมูลว่ามีการรองรับส่วนขยาย THREAD หรือไม่.
- String[] getThreadAlgorithms - รับอัลกอริธม์ THREAD ที่รองรับ.
ตัวอย่างโค้ดต่อไปนี้แสดงการใช้คุณลักษณะเหล่านี้เพื่อดึงเธรดอีเมลจาก Gmail:
ImapClient client = new ImapClient("imap.gmail.com", 993, "username", "password", SecurityOptions.SSLImplicit);
try {
client.selectFolder(ImapFolderInfo.IN_BOX);
// get a list of messages that we'll group by conversation
ImapMessageInfoCollection messages = client.listMessages();
// make sure the IMAP server supports X-GM-EXT-1 extension
if (client.getGmExt1Supported()) {
// gets unique conversationId for our example
Set<String> conversationIds = new HashSet<String>();
for (ImapMessageInfo messageInfo : messages) {
if (messageInfo.getConversationId() != null)
conversationIds.add(messageInfo.getConversationId());
}
for (String conversationId : conversationIds) {
// create the necessary search conditions for a thread
XGMThreadSearchConditions conditions = new XGMThreadSearchConditions();
conditions.setConversationId(conversationId);
conditions.setUseUId(true);
// get results
List<MessageThreadResult> conversation = client.getMessageThreads(conditions);
// print the email conversation in hierarchically manner
printConversaton("", conversation, messages);
System.out.println("--------------------");
}
}
} finally {
client.dispose();
}
/**
* <p>
* Prints the email conversation in hierarchically manner
* </p>
*/
public static void printConversaton(String indent, Iterable<MessageThreadResult> conversation,
Iterable<ImapMessageInfo> messages) {
for (MessageThreadResult thread : conversation) {
for (ImapMessageInfo messageInfo : messages) {
if (thread.getUniqueId().equals(messageInfo.getUniqueId())) {
System.out.println(indent + " (" + thread.getUniqueId() + ") " + messageInfo.getSubject());
break;
}
}
if (thread.getChildMessages().size() != 0) {
printConversaton(indent += "-", thread.getChildMessages(), messages);
}
}
}
โค้ดจะแตกต่างเล็กน้อยหากเซิร์ฟเวอร์ IMAP รองรับความสามารถ THREAD:
ตรวจสอบว่าเซิร์ฟเวอร์ IMAP รองรับส่วนขยาย THREAD หรือไม่:
if (client.getThreadSupported())
สร้างเงื่อนไขการค้นหาที่เหมาะสมสำหรับเธรด:
ThreadSearchConditions conditions = new ThreadSearchConditions();
conditions.setAlgorithm(client.getThreadAlgorithms()[0]);
conditions.setUseUId(true);
รายการข้อความพร้อมการสนับสนุนการแบ่งหน้า
ในสถานการณ์ที่เซิร์ฟเวอร์อีเมลมีจำนวนข้อความมากในกล่องจดหมาย บางครั้งต้องการแสดงหรือดึงข้อความพร้อมการสนับสนุนการแบ่งหน้า Aspose.Email API ImapClient ทำให้คุณดึงข้อความจากเซิร์ฟเวอร์พร้อมการสนับสนุนการแบ่งหน้า.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// This example shows the paging support of ImapClient for listing messages from the server
// Available in Aspose.Email for Java and onwards
final ImapClient client = new ImapClient("host.domain.com", 993, "username", "password");
try {
try {
int messagesNum = 12;
int itemsPerPage = 5;
MailMessage message = null;
// Create some test messages and append these to server's inbox
for (int i = 0; i < messagesNum; i++) {
message = new MailMessage("from@domain.com", "to@domain.com", "EMAILNET-35157 - " + UUID.randomUUID(),
"EMAILNET-35157 Move paging parameters to separate class");
client.appendMessage(ImapFolderInfo.IN_BOX, message);
}
// List messages from inbox
client.selectFolder(ImapFolderInfo.IN_BOX);
ImapMessageInfoCollection totalMessageInfoCol = client.listMessages();
// Verify the number of messages added
System.out.println(totalMessageInfoCol.size());
////////////////// RETREIVE THE MESSAGES USING PAGING SUPPORT////////////////////////////////////
List<ImapPageInfo> pages = new ArrayList<ImapPageInfo>();
PageSettings pageSettings = new PageSettings();
ImapPageInfo pageInfo = client.listMessagesByPage(itemsPerPage, 0, pageSettings);
System.out.println(pageInfo.getTotalCount());
pages.add(pageInfo);
while (!pageInfo.getLastPage()) {
pageInfo = client.listMessagesByPage(itemsPerPage, pageInfo.getNextPage().getPageOffset(), pageSettings);
pages.add(pageInfo);
}
int retrievedItems = 0;
// foreach to while statements conversion
for (ImapPageInfo folderCol : pages) {
retrievedItems += folderCol.getItems().size();
}
System.out.println(retrievedItems);
} finally {
}
} finally {
if (client != null)
client.dispose();
}
ดึงข้อมูลโฟลเดอร์และอ่านข้อความอย่างเป็นลำดับชั้น
ในบทความนี้ ส่วนใหญ่ของ ImapClient คุณลักษณะเหล่านี้ใช้สร้างแอปพลิเคชันที่แสดงรายการโฟลเดอร์และโฟลเดอร์ย่อยทั้งหมดอย่างเป็นลำดับชั้นจากเซิร์ฟเวอร์ IMAP อีกทั้งยังบันทึกข้อความในแต่ละโฟลเดอร์และโฟลเดอร์ย่อยในรูปแบบ MSG บนดิสก์ท้องถิ่น บนดิสก์ โฟลเดอร์และข้อความจะถูกสร้างและบันทึกในโครงสร้างลำดับชั้นเดียวกับบนเซิร์ฟเวอร์ IMAP ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดึงข้อมูลข้อความและโฟลเดอร์ย่อยอย่างเป็นลำดับชั้น.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() throws Exception {
// Create an instance of the ImapClient class
ImapClient client = new ImapClient();
// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("imap.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(993);
client.setSecurityOptions(SecurityOptions.Auto);
try {
// The root folder (which will be created on disk) consists of host and username
String rootFolder = client.getHost() + "-" + client.getUsername();
// Create the root folder and List all the folders from IMAP server
new File(rootFolder).mkdirs();
ImapFolderInfoCollection folderInfoCollection = client.listFolders();
for (ImapFolderInfo folderInfo : folderInfoCollection) {
// Call the recursive method to read messages and get sub-folders
listMessagesInFolder(folderInfo, rootFolder, client);
}
// Disconnect to the remote IMAP server
client.dispose();
} catch (java.lang.RuntimeException ex) {
System.out.println(ex);
}
System.out.println("Downloaded messages recursively from IMAP server.");
}
/// Recursive method to get messages from folders and sub-folders
private static void listMessagesInFolder(ImapFolderInfo folderInfo, String rootFolder, ImapClient client) {
// Create the folder in disk (same name as on IMAP server)
String currentFolder = "data/";
new File(currentFolder).mkdirs();
// Read the messages from the current folder, if it is selectable
if (folderInfo.getSelectable()) {
// Send status command to get folder info
ImapFolderInfo folderInfoStatus = client.getFolderInfo(folderInfo.getName());
System.out.println(folderInfoStatus.getName() + " folder selected. New messages: " + folderInfoStatus.getNewMessageCount() + ", Total messages: "
+ folderInfoStatus.getTotalMessageCount());
// Select the current folder and List messages
client.selectFolder(folderInfo.getName());
ImapMessageInfoCollection msgInfoColl = client.listMessages();
System.out.println("Listing messages....");
for (ImapMessageInfo msgInfo : msgInfoColl) {
// Get subject and other properties of the message
System.out.println("Subject: " + msgInfo.getSubject());
System.out.println("Read: " + msgInfo.isRead() + ", Recent: " + msgInfo.getRecent() + ", Answered: " + msgInfo.getAnswered());
// Get rid of characters like ? and :, which should not be included in a file name and Save the message in MSG format
String fileName = msgInfo.getSubject().replace(":", " ").replace("?", " ");
MailMessage msg = client.fetchMessage(msgInfo.getSequenceNumber());
msg.save(currentFolder + "\\" + fileName + "-" + msgInfo.getSequenceNumber() + ".msg", SaveOptions.getDefaultMsgUnicode());
}
System.out.println("============================\n");
} else {
System.out.println(folderInfo.getName() + " is not selectable.");
}
try {
// If this folder has sub-folders, call this method recursively to get messages
ImapFolderInfoCollection folderInfoCollection = client.listFolders(folderInfo.getName());
for (ImapFolderInfo subfolderInfo : folderInfoCollection) {
listMessagesInFolder(subfolderInfo, rootFolder, client);
}
} catch (java.lang.RuntimeException e) {
}
}
รับ UID หรือหมายเลขลำดับของข้อความ
Aspose.Email Public API มีคุณลักษณะดังต่อไปนี้เพื่อรับข้อมูลการระบุตัวของข้อความ เช่น UID หรือหมายเลขลำดับ ซึ่งอาจจำเป็นเมื่อต้องประมวลผลข้อความที่ได้รับจากเซิร์ฟเวอร์:
MailboxInfo คลาส - แสดงข้อมูลการระบุของข้อความในกล่องจดหมาย.
- getSequenceNumber() method - รับลำดับเลขของข้อความ.
- getUniqueId() method - รับ ID ที่ไม่ซ้ำของข้อความ.
MailMessage class - แสดงข้อความอีเมลและอนุญาตให้เข้าถึงคุณสมบัติของข้อความ เช่น เรื่อง, เนื้อหา, ผู้ส่งและผู้รับ ฯลฯ
- getItemId method - แสดงข้อมูลการระบุตัวตนของข้อความในกล่องจดหมาย.
ตัวอย่างโค้ดด้านล่างแสดงวิธีดึงและแสดงรายละเอียดของข้อความจากโฟลเดอร์ "INBOX" บนเซิร์ฟเวอร์ IMAP โดยใช้ ImapClient คลาส:
try (ImapClient client = new ImapClient(imapHost, port, emailAddress, password, securityOption)) {
ImapMessageInfoCollection msgs = client.listMessages("INBOX");
List<Integer> seqIds = new ArrayList<>();
for (ImapMessageInfo msg : msgs) {
seqIds.add(msg.getSequenceNumber());
}
Iterable<MailMessage> msgsViaFetch = client.fetchMessagesBySequences(seqIds);
for (MailMessage thisMsg : msgsViaFetch) {
System.out.println("Message ID: " + thisMsg.getItemId().getUniqueId()
+ " SequenceNumber: " + thisMsg.getItemId().getSequenceNumber()
+ " Subject: " + thisMsg.getSubject());
}
}
ดึงพารามิเตอร์เพิ่มเติมเป็นข้อมูลสรุป
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
final ImapClient client = new ImapClient("host.domain.com", "username", "password");
try {
MailMessage message = new MailMessage("from@domain.com", "to@doman.com", "EMAILNET-38466 - " + UUID.randomUUID().toString(),
"EMAILNET-38466 Add extra parameters for UID FETCH command");
// append the message to the server
String uid = client.appendMessage(message);
// wait for the message to be appended
Thread.sleep(5000);
// Define properties to be fetched from server along with the message
List<String> messageExtraFields = Arrays.asList("X-GM-MSGID", "X-GM-THRID");
// retreive the message summary information using it's UID
ImapMessageInfo messageInfoUID = client.listMessage(uid, messageExtraFields);
// retreive the message summary information using it's sequence number
ImapMessageInfo messageInfoSeqNum = client.listMessage(1, messageExtraFields);
// List messages in general from the server based on the defined properties
ImapMessageInfoCollection messageInfoCol = client.listMessages(messageExtraFields);
ImapMessageInfo messageInfoFromList = messageInfoCol.get_Item(0);
// verify that the parameters are fetched in the summary information
for (String paramName : messageExtraFields) {
System.out.println(messageInfoFromList.getExtraParameters().containsKey(paramName));
System.out.println(messageInfoUID.getExtraParameters().containsKey(paramName));
System.out.println(messageInfoSeqNum.getExtraParameters().containsKey(paramName));
}
} finally {
if (client != null)
client.dispose();
}
รับข้อมูลหัวข้อ List-Unsubscribe
หัวข้อ List-Unsubscribe มี URL สำหรับยกเลิกการสมัครรับรายชื่ออีเมล เช่น โฆษณา, จดหมายข่าว ฯลฯ เพื่อรับหัวข้อ List-Unsubscribe ให้ใช้ listUnsubscribe คุณสมบัติของ ImapMessageInfo คลาส ตัวอย่างต่อไปนี้แสดงการใช้ของ listUnsubscribe คุณสมบัติสำหรับรับหัวข้อ List-Unsubscribe.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapClient imapClient = new ImapClient();
imapClient.setHost("<HOST>");
imapClient.setPort(993);
imapClient.setUsername("<USERNAME>");
imapClient.setPassword("<PASSWORD>");
imapClient.setSupportedEncryption(EncryptionProtocols.Tls);
imapClient.setSecurityOptions(SecurityOptions.SSLImplicit);
ImapMessageInfoCollection messageInfoCol = imapClient.listMessages();
for (ImapMessageInfo imapMessageInfo : messageInfoCol) {
System.out.println("ListUnsubscribe Header: " + imapMessageInfo.getListUnsubscribe());
}