ทำงานกับโฟลเดอร์บนเซิร์ฟเวอร์ IMAP
ดึงข้อมูลโฟลเดอร์
การดึงข้อมูลโฟลเดอร์จากเซิร์ฟเวอร์ IMAP ง่ายมากด้วย Aspose.Email เรียก listFolders() เมธอดของ ImapClient คลาส มันคืนอ็อบเจ็กต์ของ ImapFolderInfoCollection ประเภท วนซ้ำผ่านคอลเลกชันนี้และดึงข้อมูลเกี่ยวกับโฟลเดอร์แต่ละรายการในลูป เมธอดมีการโอเวอร์โหลด คุณสามารถส่งชื่อโฟลเดอร์เป็นพารามิเตอร์เพื่อรับรายการโฟลเดอร์ย่อย ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดึงข้อมูลโฟลเดอร์จากเซิร์ฟเวอร์ IMAP โดยใช้เมธอด Aspose.Email ที่อธิบายไว้.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get all folders in the currently subscribed folder
ImapFolderInfoCollection folderInfoColl = client.listFolders();
// Iterate through the collection to get folder info one by one
for (ImapFolderInfo folderInfo : (Iterable<ImapFolderInfo>) folderInfoColl) {
// Folder name and get New messages in the folder
System.out.println("Folder name is " + folderInfo.getName());
ImapFolderInfo folderExtInfo = client.getFolderInfo(folderInfo.getName());
System.out.println("New message count: " + folderExtInfo.getNewMessageCount());
System.out.println("Is it readonly? " + folderExtInfo.getReadOnly());
System.out.println("Total number of messages " + folderExtInfo.getTotalMessageCount());
}
การลบและเปลี่ยนชื่อโฟลเดอร์
โฟลเดอร์บนเซิร์ฟเวอร์ IMAP สามารถลบหรือเปลี่ยนชื่อด้วยบรรทัดเดียวโดยใช้ Aspose.Email:
- นี้ deleteFolder() เมธอดรับชื่อโฟลเดอร์เป็นพารามิเตอร์.
- สำหรับ renameFolder() เมธอด คุณต้องส่งชื่อโฟลเดอร์ปัจจุบันและชื่อโฟลเดอร์ใหม่ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีลบโฟลเดอร์จากเซิร์ฟเวอร์ IMAP และวิธีเปลี่ยนชื่อโฟลเดอร์ การทำงานแต่ละอย่างทำได้ด้วยบรรทัดโค้ดหนึ่งบรรทัด
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Delete a folder and Rename a folder
client.deleteFolder("foldername");
client.renameFolder("foldername", "newfoldername");
การเพิ่มข้อความใหม่ในโฟลเดอร์
คุณสามารถเพิ่มข้อความใหม่ในโฟลเดอร์โดยใช้ MailMessage และ ImapClient คลาสแรก สร้าง MailMessage อ็อบเจ็กต์โดยระบุหัวเรื่อง, ผู้รับและผู้ส่ง จากนั้นสมัครสมาชิกโฟลเดอร์และเพิ่มข้อความลงในนั้น ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มข้อความใหม่ในโฟลเดอร์.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create a message
MailMessage msg = new MailMessage("user@domain1.com", "user@domain2.com", "subject", "message");
// Subscribe to the Inbox folder and Append the newly created message
client.selectFolder(ImapFolderInfo.IN_BOX);
client.subscribeFolder(client.getCurrentFolder().getName());
client.appendMessage(client.getCurrentFolder().getName(), msg);
เพิ่มหลายข้อความด้วยการสนับสนุน MultiConnection
คุณสามารถเพิ่มหลายข้อความโดยใช้ appendMessages เมธอดที่ให้โดย ImapClient คลาส. เมธอด appendMessages เมธอดรับรายการของ MailMessage และเพิ่มเข้ากับโฟลเดอร์ปัจจุบันหากไม่ได้ระบุโฟลเดอร์เป็นพารามิเตอร์ ImapClient ยังรองรับโหมด MultiConnection สำหรับการทำงานที่หนัก ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มหลายข้อความโดยใช้โหมด MultiConnection.
// 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);
List<MailMessage> messages = new ArrayList<MailMessage>();
for (int i = 0; i < 20; i++) {
MailMessage message = new MailMessage("<EMAIL ADDRESS>", "<EMAIL ADDRESS>", "Test Message - " + UUID.randomUUID().toString(), "IMAP Group Append with MultiConnection");
messages.add(message);
}
imapClient.setConnectionsQuantity(5);
imapClient.setUseMultiConnection(MultiConnectionMode.Enable);
imapClient.appendMessages(messages);
ย้ายข้อความไปยังโฟลเดอร์กล่องจดหมายอื่น
Aspose.Email สำหรับ Java อนุญาตให้ย้ายข้อความจากโฟลเดอร์กล่องจดหมายหนึ่งไปยังอีกโฟลเดอร์หนึ่งโดยใช้ ImapClient API. การ moveMessage วิธีการนี้ใช้รหัสข้อความที่ไม่ซ้ำและชื่อโฟลเดอร์ปลายทางสำหรับการย้ายข้อความไปยังโฟลเดอร์ปลายทาง โค้ดตัวอย่างต่อไปนี้แสดงวิธีการย้ายข้อความไปยังโฟลเดอร์กล่องจดหมายอื่น
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// This example shows how to move a message from one folder of a mailbox to another one using the ImapClient API of Aspose.Email for Java
// Available from Aspose.Email for Java onwards
// -------------- Available API Overload Members --------------
// void ImapClient.moveMessage(IConnection iConnection, int sequenceNumber, String folderName, boolean commitDeletions)
// void ImapClient.moveMessage(IConnection iConnection, String uniqueId, String folderName, boolean commitDeletions)
// void ImapClient.moveMessage(int sequenceNumber, String folderName, boolean commitDeletions)
// void ImapClient.moveMessage(String uniqueId, String folderName, boolean commitDeletions)
// void ImapClient.moveMessage(IConnection iConnection, int sequenceNumber, String folderName)
// void ImapClient.moveMessage(IConnection iConnection, String uniqueId, String folderName)
// void ImapClient.moveMessage(int sequenceNumber, String folderName)
// void ImapClient.moveMessage(String uniqueId, String folderName)
final ImapClient client = new ImapClient("host.domain.com", 993, "username", "password");
try {
String folderName = "EMAILNET-35151";
if (!client.existFolder(folderName))
client.createFolder(folderName);
try {
MailMessage message = new MailMessage("from@domain.com", "to@domain.com", "EMAILNET-35151 - " + UUID.randomUUID(),
"EMAILNET-35151 ImapClient: Provide option to Move Message");
client.selectFolder(ImapFolderInfo.IN_BOX);
// Append the new message to Inbox folder
String uniqueId = client.appendMessage(ImapFolderInfo.IN_BOX, message);
ImapMessageInfoCollection messageInfoCol1 = client.listMessages();
System.out.println(messageInfoCol1.size());
// Now move the message to the folder EMAILNET-35151
client.moveMessage(uniqueId, folderName);
client.commitDeletes();
// Verify that the message was moved to the new folder
client.selectFolder(folderName);
messageInfoCol1 = client.listMessages();
System.out.println(messageInfoCol1.size());
// Verify that the message was moved from the Inbox
client.selectFolder(ImapFolderInfo.IN_BOX);
messageInfoCol1 = client.listMessages();
System.out.println(messageInfoCol1.size());
} finally {
try {
client.deleteFolder(folderName);
} catch (java.lang.RuntimeException e) {
}
}
} finally {
if (client != null)
client.dispose();
}
คัดลอกข้อความไปยังโฟลเดอร์กล่องจดหมายอื่น
Aspose.Email API ให้ความสามารถในการคัดลอกข้อความจากโฟลเดอร์กล่องเมลหนึ่งไปยังอีกโฟลเดอร์หนึ่ง มันอนุญาตให้คัดลอกข้อความเดี่ยวหรือหลายข้อความโดยใช้ copyMessage และ copyMessages เมธอด. copyMessages เมธอดให้ความสามารถในการคัดลอกหลายข้อความจากโฟลเดอร์ต้นทางของกล่องเมลไปยังโฟลเดอร์กล่องเมลปลายทาง ตัวอย่างโค้ดต่อไปนี้แสดงวิธีคัดลอกข้อความไปยังโฟลเดอร์กล่องเมลอื่น.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
final ImapClient client = new ImapClient("exchange.aspose.com", "username", "password");
try {
// Create the destination folder
String folderName = "EMAILNET-35242";
if (!client.existFolder(folderName))
client.createFolder(folderName);
try {
// Append a couple of messages to the server
MailMessage message1 = new MailMessage("asposeemail.test3@aspose.com", "asposeemail.test3@aspose.com", "EMAILNET-35242 - " + UUID.randomUUID(),
"EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation.");
String uniqueId1 = client.appendMessage(message1);
MailMessage message2 = new MailMessage("asposeemail.test3@aspose.com", "asposeemail.test3@aspose.com", "EMAILNET-35242 - " + UUID.randomUUID(),
"EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation.");
String uniqueId2 = client.appendMessage(message2);
// Verify that the messages have been added to the mailbox
client.selectFolder(ImapFolderInfo.IN_BOX);
ImapMessageInfoCollection msgsColl = client.listMessages();
for (ImapMessageInfo msgInfo : msgsColl)
System.out.println(msgInfo.getSubject());
// Copy multiple messages using the CopyMessages command and Verify that messages are copied to the destination folder
client.copyMessagesByUids(Arrays.asList(uniqueId1, uniqueId2), folderName);
client.selectFolder(folderName);
msgsColl = client.listMessages();
for (ImapMessageInfo msgInfo : msgsColl)
System.out.println(msgInfo.getSubject());
} finally {
try {
client.deleteFolder(folderName);
} catch (java.lang.RuntimeException e) {
}
}
} finally {
if (client != null)
client.dispose();
}
ทำงานกับโฟลเดอร์กล่องเมลการใช้พิเศษ
บางที่เก็บข้อความ IMAP มีกล่องเมลที่ใช้พิเศษ เช่น ที่ใช้เก็บข้อความร่างหรือข้อความที่ส่งแล้ว ลูกค้าเมลหลายตัวอนุญาตให้ผู้ใช้กำหนดตำแหน่งที่ควรเก็บร่างหรือข้อความที่ส่งแล้ว แต่การกำหนดค่าต้องให้ผู้ใช้รู้ว่ากล่องเมลใดที่เซิร์ฟเวอร์ได้แยกไว้สำหรับวัตถุประสงค์เหล่านี้ Aspose.Email สามารถระบุกล่องเมลพิเศษเหล่านี้โดยใช้ ImapMailboxInfo คลาสเพื่อให้ง่ายต่อการทำงานกับพวกมัน ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีเข้าถึงกล่องเมลพิเศษเหล่านี้โดยใช้ ImapMailboxInfo คลาส.
// 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);
ImapMailboxInfo mailboxInfo = imapClient.getMailboxInfo();
System.out.println(mailboxInfo.getInbox());
System.out.println(mailboxInfo.getDraftMessages());
System.out.println(mailboxInfo.getJunkMessages());
System.out.println(mailboxInfo.getSentMessages());
System.out.println(mailboxInfo.getTrash());