کار با پوشهها در سرور 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();
}
کپی پیامها به پوشه دیگر صندوقپست
API Aspose.Email قابلیت کپی کردن یک پیام از یک پوشهٔ صندوقپست به پوشهٔ دیگری را فراهم میکند. این امکان را میدهد که یک پیام یا چندین پیام را با استفاده از 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());