العمل مع المجلدات على خادم 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 تشمل صناديق بريد ذات استخدام خاص، مثل تلك المستخدمة لحفظ مسودات الرسائل أو الرسائل المرسلة. many clients allow users to specify where the draft or sent messages should be put, but configuring them requires that the user knows which mailboxes the server has set aside for these purposes. 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());