Class EmailBundle

java.lang.Object
com.chilkatsoft.chilkat2.ChilkatObject
com.chilkatsoft.chilkat2.EmailBundle
All Implemented Interfaces:
AutoCloseable

public class EmailBundle extends ChilkatObject
<section class="emailbundle-summary">

Hold, search, sort, remove, save, and reload groups of email messages.

EmailBundle is a container for multiple Email objects. It is commonly returned by mail-related operations and can also be built manually by adding individual email messages. The class provides a simple way to work with groups of messages: retrieve emails by index, search for a matching email, sort by common email fields, remove messages, clear the bundle, and serialize or reload the collection as XML.

Email collection

Store multiple Email objects together after receiving, loading, filtering, or manually building a message set.

Retrieve messages

Use the message count and indexed access methods to retrieve individual emails for inspection, processing, saving, or sending.

Search within the bundle

Locate a specific email in the collection when application logic needs to find a matching message.

Sort messages

Sort the bundle by date, sender, recipient, or subject to organize messages before processing or display.

Remove or clear emails

Remove individual messages by object or index, or clear the entire bundle when the collection is no longer needed.

Save and reload

Serialize the bundle to XML and reload it later, or load task results that return an email bundle.

Common pattern: Receive or build a bundle of Email objects, loop from 0 to MessageCount - 1, retrieve each message with EmailAt, and then inspect, save, sort, remove, or process each email as needed. Use EmailBundle for the group and Email for the individual message contents.

For an extended overview, see EmailBundle Class Overview.

</section>

Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when done; an unclosed instance is freed when it becomes unreachable.

See Also:
Online reference
  • Constructor Details

    • EmailBundle

      public EmailBundle()
      Creates a new EmailBundle object.
  • Method Details

    • getMessageCount

      public int getMessageCount()
      Returns the number of Email objects currently contained in the bundle.

      Valid zero-based indexes range from 0 through MessageCount - 1.

      Returns:
      the value of the MessageCount property (read-only)
    • addEmail

      public void addEmail​(Email email)
      Adds an independent copy of the Email object in email to the end of this in-memory bundle.

      The bundle does not retain a reference to email and does not take ownership of the supplied object. Changes made to email after this method returns do not affect the copy stored in the bundle, and changes to the stored copy do not affect email.

      The same Email object may be added more than once. Each call adds another independent copy and increases the getMessageCount() property by one.

      Parameters:
      email - the email argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • clear

      public void clear()
      Removes and discards all emails from this in-memory bundle.

      After this method returns, getMessageCount() is 0. No messages are deleted from any mail server.

    • emailAt

      public void emailAt​(int index, Email email)
      Copies the email at the zero-based index in index into the Email object in email.

      Valid indexes range from 0 through getMessageCount() - 1. The first email is at index 0.

      email receives an independent copy. Changes made to email do not modify the email stored in the bundle.

      Note: This is an in-memory operation and does not communicate with a mail server.

      Parameters:
      index - the index argument
      email - the email argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • findEmail

      public void findEmail​(JsonObject json, Email email)
      Finds the first email in the bundle whose MIME header field matches the search criteria specified by the JsonObject in json.

      At this time, a search may specify only one MIME header field. Use the following JSON format:

      {
         "header": {
           "name": "Subject",
           "value": "Dinner on Wednesday?",
           "caseInsensitive": true
         }
       }

      header.name specifies the MIME header field name, and header.value specifies the complete field value to match. The field value must match exactly; this is not a substring, wildcard, or regular-expression search. Set header.caseInsensitive to true for a case-insensitive comparison or false for a case-sensitive comparison.

      When a match is found, email receives the first matching email.

      Parameters:
      json - the json argument (not null)
      email - the email argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getXml

      public String getXml()
      Serializes the entire email bundle to Chilkat XML and returns the XML document as a string.

      The XML contains the bundle and its individual Email objects. It can be loaded into another EmailBundle by calling loadXmlString(String).

      This is Chilkat's XML representation of an email bundle; it is not an RFC 822/MIME message.

      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • loadXml

      public void loadXml​(String path)
      Loads an email bundle from the local filesystem path specified by path. The path may be absolute or relative; a filename alone refers to a file in the current working directory.

      The file should contain Chilkat email-bundle XML previously produced by saveXml(String) or getXml().

      The emails loaded from path are appended to the emails already in the bundle. Existing emails are not removed or replaced.

      Parameters:
      path - the path argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • loadXmlString

      public void loadXmlString​(String xmlStr)
      Loads an email bundle from the Chilkat XML string in xmlStr.

      The XML is typically obtained from getXml() or read from a file created by saveXml(String).

      The emails loaded from xmlStr are appended to the emails already in the bundle. Existing emails are not removed or replaced.

      Parameters:
      xmlStr - the xmlStr argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • removeEmail

      public void removeEmail​(Email email)
      Removes the first email in this in-memory bundle that matches email.

      Chilkat first searches for an email having the same UIDL as email. If no email with a matching UIDL is found, it searches for the first email having the same Message-ID header field value. Only the first matching email is removed.

      email may be an independent copy obtained from emailAt(int, Email).

      Note: This method changes only the local bundle. It does not delete a message from a POP3 or IMAP server.

      Parameters:
      email - the email argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • removeEmailByIndex

      public void removeEmailByIndex​(int index)
      Removes the email at the zero-based index in index.

      Valid indexes range from 0 through getMessageCount() - 1.

      Note: This method changes only the local bundle. It does not delete a message from a mail server.

      Parameters:
      index - the index argument
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • saveXml

      public void saveXml​(String path)
      Serializes the bundle and each contained Email to Chilkat XML, then writes the XML document to the local filesystem path specified by path. The path may be absolute or relative; a filename alone writes to the current working directory.

      Reload the saved bundle by calling loadXml(String). Use getXml() when the XML is needed in memory rather than in a file.

      The saved file is a Chilkat email-bundle serialization, not an RFC 822/MIME message file.

      Parameters:
      path - the path argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • sortByDate

      public void sortByDate​(boolean ascending)
      Sorts the emails in this bundle by each Email object's EmailDate property.

      Set ascending to true to sort in ascending order, from oldest to newest. Set ascending to false to sort in descending order, from newest to oldest.

      The operation changes the order of the emails in the in-memory collection.

      Parameters:
      ascending - the ascending argument
    • sortByRecipient

      public void sortByRecipient​(boolean ascending)
      Sorts the emails in this bundle by the full value of each email's To header field.

      The comparisons are case-sensitive. Set ascending to true for ascending order or false for descending order. The operation changes the order of the emails in the in-memory collection.

      Parameters:
      ascending - the ascending argument
    • sortBySender

      public void sortBySender​(boolean ascending)
      Sorts the emails in this bundle by the full content of each email's From header field.

      Set ascending to true for ascending order or false for descending order. The operation changes the order of the emails in the in-memory collection.

      Parameters:
      ascending - the ascending argument
    • sortBySubject

      public void sortBySubject​(boolean ascending)
      Sorts the emails in this bundle by subject.

      Set ascending to true for ascending order or false for descending order. The operation changes the order of the emails in the in-memory collection.

      Parameters:
      ascending - the ascending argument