Class EmailBundle
- All Implemented Interfaces:
AutoCloseable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an independent copy of theEmailobject inemailto the end of this in-memory bundle.voidclear()Removes and discards all emails from this in-memory bundle.voidCopies the email at the zero-based index inindexinto theEmailobject inemail.voidfindEmail(JsonObject json, Email email)Finds the first email in the bundle whose MIME header field matches the search criteria specified by theJsonObjectinjson.intReturns the number ofEmailobjects currently contained in the bundle.getXml()Serializes the entire email bundle to Chilkat XML and returns the XML document as a string.voidLoads an email bundle from the local filesystem path specified bypath.voidloadXmlString(String xmlStr)Loads an email bundle from the Chilkat XML string inxmlStr.voidremoveEmail(Email email)Removes the first email in this in-memory bundle that matchesemail.voidremoveEmailByIndex(int index)Removes the email at the zero-based index inindex.voidSerializes the bundle and each containedEmailto Chilkat XML, then writes the XML document to the local filesystem path specified bypath.voidsortByDate(boolean ascending)Sorts the emails in this bundle by eachEmailobject'sEmailDateproperty.voidsortByRecipient(boolean ascending)Sorts the emails in this bundle by the full value of each email'sToheader field.voidsortBySender(boolean ascending)Sorts the emails in this bundle by the full content of each email'sFromheader field.voidsortBySubject(boolean ascending)Sorts the emails in this bundle by subject.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
EmailBundle
public EmailBundle()Creates a newEmailBundleobject.
-
-
Method Details
-
getMessageCount
public int getMessageCount()Returns the number ofEmailobjects currently contained in the bundle.Valid zero-based indexes range from
0throughMessageCount - 1.- Returns:
- the value of the
MessageCountproperty (read-only)
-
addEmail
Adds an independent copy of theEmailobject inemailto the end of this in-memory bundle.The bundle does not retain a reference to
emailand does not take ownership of the supplied object. Changes made toemailafter this method returns do not affect the copy stored in the bundle, and changes to the stored copy do not affectemail.The same
Emailobject may be added more than once. Each call adds another independent copy and increases theproperty by one.getMessageCount()- Parameters:
email- theemailargument (notnull)- 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,
isgetMessageCount()0. No messages are deleted from any mail server. -
emailAt
Copies the email at the zero-based index inindexinto theEmailobject inemail.Valid indexes range from
0through. The first email is at indexgetMessageCount()- 10.emailreceives an independent copy. Changes made toemaildo 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- theindexargumentemail- theemailargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
findEmail
Finds the first email in the bundle whose MIME header field matches the search criteria specified by theJsonObjectinjson.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.namespecifies the MIME header field name, andheader.valuespecifies the complete field value to match. The field value must match exactly; this is not a substring, wildcard, or regular-expression search. Setheader.caseInsensitivetotruefor a case-insensitive comparison orfalsefor a case-sensitive comparison.When a match is found,
emailreceives the first matching email.- Parameters:
json- thejsonargument (notnull)email- theemailargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
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
Emailobjects. It can be loaded into anotherEmailBundleby 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
Loads an email bundle from the local filesystem path specified bypath. 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
orsaveXml(String).getXml()The emails loaded from
pathare appended to the emails already in the bundle. Existing emails are not removed or replaced.- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadXmlString
Loads an email bundle from the Chilkat XML string inxmlStr.The XML is typically obtained from
or read from a file created bygetXml().saveXml(String)The emails loaded from
xmlStrare appended to the emails already in the bundle. Existing emails are not removed or replaced.- Parameters:
xmlStr- thexmlStrargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
removeEmail
Removes the first email in this in-memory bundle that matchesemail.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 sameMessage-IDheader field value. Only the first matching email is removed.emailmay 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- theemailargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
removeEmailByIndex
public void removeEmailByIndex(int index)Removes the email at the zero-based index inindex.Valid indexes range from
0through.getMessageCount()- 1Note: This method changes only the local bundle. It does not delete a message from a mail server.
- Parameters:
index- theindexargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
saveXml
Serializes the bundle and each containedEmailto Chilkat XML, then writes the XML document to the local filesystem path specified bypath. The path may be absolute or relative; a filename alone writes to the current working directory.Reload the saved bundle by calling
. UseloadXml(String)when the XML is needed in memory rather than in a file.getXml()The saved file is a Chilkat email-bundle serialization, not an RFC 822/MIME message file.
- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
sortByDate
public void sortByDate(boolean ascending)Sorts the emails in this bundle by eachEmailobject'sEmailDateproperty.Set
ascendingtotrueto sort in ascending order, from oldest to newest. Setascendingtofalseto sort in descending order, from newest to oldest.The operation changes the order of the emails in the in-memory collection.
- Parameters:
ascending- theascendingargument
-
sortByRecipient
public void sortByRecipient(boolean ascending)Sorts the emails in this bundle by the full value of each email'sToheader field.The comparisons are case-sensitive. Set
ascendingtotruefor ascending order orfalsefor descending order. The operation changes the order of the emails in the in-memory collection.- Parameters:
ascending- theascendingargument
-
sortBySender
public void sortBySender(boolean ascending)Sorts the emails in this bundle by the full content of each email'sFromheader field.Set
ascendingtotruefor ascending order orfalsefor descending order. The operation changes the order of the emails in the in-memory collection.- Parameters:
ascending- theascendingargument
-
sortBySubject
public void sortBySubject(boolean ascending)Sorts the emails in this bundle by subject.Set
ascendingtotruefor ascending order orfalsefor descending order. The operation changes the order of the emails in the in-memory collection.- Parameters:
ascending- theascendingargument
-