EmailBundle SQL Server Reference Documentation

EmailBundle

Current Version: 11.6.0

EmailBundle

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.

Object Creation

DECLARE @hr int
DECLARE @emailBundle int
EXEC @hr = sp_OACreate 'Chilkat.EmailBundle', @emailBundle OUT
IF @hr <> 0
BEGIN
    PRINT 'Failed to create ActiveX component'
    RETURN
END

-- ... use @emailBundle ...

EXEC @hr = sp_OADestroy @emailBundle

T-SQL uses the Chilkat ActiveX through the OLE Automation stored procedures. They must be enabled once on the server (EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;), and the Chilkat ActiveX registered must match the bitness of the SQL Server instance (64-bit for a 64-bit SQL Server). To bind to a specific major version of Chilkat, append the major version number to the ProgID, such as sp_OACreate 'Chilkat.EmailBundle.11' for Chilkat v11.*.*.

sp_OACreate returns an object token (an int) that is passed as the first argument of every sp_OAMethod, sp_OAGetProperty and sp_OASetProperty call, and released with sp_OADestroy. Objects returned by methods (such as an HttpResponse or JsonObject) are also tokens received through an int OUT parameter; they must likewise be destroyed, and the OUT parameter is NULL when the method fails to return an object. Objects passed as arguments are passed by their token. When an OLE Automation procedure itself fails (non-zero @hr), sp_OAGetErrorInfo describes the error.

Data types: strings are nvarchar(4000); integers, booleans (1 or 0) and object tokens are int; dates are datetime. In the signatures on this page, @success, @iResult, @sResult and the like are the OUT variables receiving a method's return value, @iValue / @sValue receive or supply a property value, and the remaining @ variables are the method's arguments in order.

A string returned through an OUT parameter is limited to 4000 characters. For longer values, retrieve the result as a result set into a table variable instead of an OUT parameter, for example DECLARE @tmp TABLE (outputLine ntext) followed by INSERT INTO @tmp EXEC sp_OAGetProperty @emailBundle, 'LastErrorText'. See string length limitations for strings returned by sp_OAMethod calls.

Methods that pass or return raw byte arrays are not shown on this page, because varbinary(max) values cannot be exchanged through sp_OAMethod (see varbinary(max) limitation). Use the BinData-based alternatives (methods ending in Bd) or the base64 / hex string-encoded variants instead. Binary properties (such as LastBinaryResult) can be retrieved as a result set into a table variable, as shown in their signatures. Asynchronous (*Async) methods and event callbacks are not available from SQL Server.

Properties

DebugLogFilePath
EXEC sp_OAGetProperty @emailBundle, 'DebugLogFilePath', @sValue OUT
EXEC sp_OASetProperty @emailBundle, 'DebugLogFilePath', @sValue

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
LastBinaryResult
INSERT INTO @tmp EXEC sp_OAGetProperty @emailBundle, 'LastBinaryResult'

This property is mainly used in SQL Server stored procedures to retrieve binary data from the last method call that returned binary data. It is only accessible if Chilkat.Global.KeepBinaryResult is set to 1. This feature allows for the retrieval of large varbinary results in an SQL Server environment, which has restrictions on returning large data via method calls, though temp tables can handle binary properties.

top
LastErrorHtml
EXEC sp_OAGetProperty @emailBundle, 'LastErrorHtml', @sValue OUT

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
EXEC sp_OAGetProperty @emailBundle, 'LastErrorText', @sValue OUT

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
EXEC sp_OAGetProperty @emailBundle, 'LastErrorXml', @sValue OUT

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
EXEC sp_OAGetProperty @emailBundle, 'LastMethodSuccess', @iValue OUT
EXEC sp_OASetProperty @emailBundle, 'LastMethodSuccess', @iValue

Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
LastStringResult
EXEC sp_OAGetProperty @emailBundle, 'LastStringResult', @sValue OUT

In SQL Server stored procedures, this property holds the string return value of the most recent method call that returns a string. It is accessible only when Chilkat.Global.KeepStringResult is set to TRUE. SQL Server has limitations on string lengths returned from methods and properties, but temp tables can be used to access large strings.

top
LastStringResultLen
EXEC sp_OAGetProperty @emailBundle, 'LastStringResultLen', @iValue OUT

The length, in characters, of the string contained in the LastStringResult property.

top
MessageCount
EXEC sp_OAGetProperty @emailBundle, 'MessageCount', @iValue OUT

Returns the number of Email objects currently contained in the bundle.

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

top
VerboseLogging
EXEC sp_OAGetProperty @emailBundle, 'VerboseLogging', @iValue OUT
EXEC sp_OASetProperty @emailBundle, 'VerboseLogging', @iValue

If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
EXEC sp_OAGetProperty @emailBundle, 'Version', @sValue OUT

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AddEmail
EXEC sp_OAMethod @emailBundle, 'AddEmail', @success OUT, @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 MessageCount property by one.

Returns 1 for success, 0 for failure.

top
Clear
EXEC sp_OAMethod @emailBundle, 'Clear', NULL
Introduced in version 11.1.0

Removes and discards all emails from this in-memory bundle.

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

top
EmailAt
EXEC sp_OAMethod @emailBundle, 'EmailAt', @success OUT, @index, @email
Introduced in version 11.0.0

Copies the email at the zero-based index in index into the Email object in email.

Valid indexes range from 0 through MessageCount - 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.

Returns 1 for success, 0 for failure.

top
FindEmail
EXEC sp_OAMethod @emailBundle, 'FindEmail', @success OUT, @json, @email
Introduced in version 11.0.0

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.

Returns 1 for success, 0 for failure.

top
GetXml
EXEC sp_OAMethod @emailBundle, 'GetXml', @sResult OUT

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.

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

Returns NULL on failure

top
LoadXml
EXEC sp_OAMethod @emailBundle, 'LoadXml', @success OUT, @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 or GetXml.

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

Returns 1 for success, 0 for failure.

top
LoadXmlString
EXEC sp_OAMethod @emailBundle, 'LoadXmlString', @success OUT, @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.

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

Returns 1 for success, 0 for failure.

top
RemoveEmail
EXEC sp_OAMethod @emailBundle, 'RemoveEmail', @success OUT, @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.

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

Returns 1 for success, 0 for failure.

top
RemoveEmailByIndex
EXEC sp_OAMethod @emailBundle, 'RemoveEmailByIndex', @success OUT, @index

Removes the email at the zero-based index in index.

Valid indexes range from 0 through MessageCount - 1.

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

Returns 1 for success, 0 for failure.

top
SaveXml
EXEC sp_OAMethod @emailBundle, 'SaveXml', @success OUT, @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. 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.

Returns 1 for success, 0 for failure.

top
SortByDate
EXEC sp_OAMethod @emailBundle, 'SortByDate', NULL, @ascending

Sorts the emails in this bundle by each Email object's EmailDate property.

Set ascending to 1 to sort in ascending order, from oldest to newest. Set ascending to 0 to sort in descending order, from newest to oldest.

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

top
SortByRecipient
EXEC sp_OAMethod @emailBundle, 'SortByRecipient', NULL, @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 1 for ascending order or 0 for descending order. The operation changes the order of the emails in the in-memory collection.

top
SortBySender
EXEC sp_OAMethod @emailBundle, 'SortBySender', NULL, @ascending

Sorts the emails in this bundle by the full content of each email's From header field.

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

top
SortBySubject
EXEC sp_OAMethod @emailBundle, 'SortBySubject', NULL, @ascending

Sorts the emails in this bundle by subject.

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

top

Deprecated

FindByHeader
EXEC sp_OAMethod @emailBundle, 'FindByHeader', @email OUT, @headerFieldName, @headerFieldValue -- returns a ChilkatEmail object token
This method is deprecated and replaced by FindEmail

Deprecated: Use FindEmail instead.

Returns the first email having a header field whose name matches headerFieldName and whose value matches headerFieldValue. Header field name matching is case-insensitive. Header field value matching is exact and case-sensitive.

Returns NULL when no matching email is found.

Returns NULL on failure

top
GetEmail
EXEC sp_OAMethod @emailBundle, 'GetEmail', @email OUT, @index -- returns a ChilkatEmail object token
This method is deprecated and replaced by EmailAt

Deprecated: Use EmailAt instead.

Returns a copy of the email at the zero-based index in index. Valid indexes range from 0 through MessageCount - 1.

The returned Email is independent of the email stored in the bundle. Changes made to the returned object do not modify the bundle.

To replace an email, retrieve the copy, modify it, remove the original with RemoveEmailByIndex, and add the modified email with AddEmail.

Note: This is an in-memory operation. It does not communicate with a mail server or download message content.

Returns NULL on failure

top
GetUidls
EXEC sp_OAMethod @emailBundle, 'GetUidls', @stringArray OUT -- returns a CkStringArray object token
This method is deprecated.

Deprecated: This method is retained for compatibility and may be removed in a future version of Chilkat.

Returns a StringArray containing the POP3 UIDL values stored with the Email objects in the bundle.

UIDLs apply to messages retrieved from a POP3 server. IMAP messages use UIDs instead; an IMAP UID associated with an email is available in the ckx-imap-uid header field.

Returns NULL on failure

top