Sample code for 30+ languages & platforms
SQL Server

Count the Attached Messages in an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumAttachedMessages property, which is the number of embedded emails represented by message/rfc822 MIME parts. These are counted separately from ordinary attachments and related items, and their indexes are zero-based. This example builds an inner email, attaches it to an outer email as a nested message, and prints the count.

Background: When you "forward as attachment," many mail clients embed the original message as a complete nested email rather than quoting its text. In MIME this appears as a message/rfc822 part — an entire email (its own headers and body) tucked inside the carrier message. Chilkat distinguishes three kinds of enclosed content: ordinary attachments (NumAttachments), inline related items (NumRelatedItems), and these nested messages (NumAttachedMessages). Use GetAttachedEmail to pull an embedded message into its own Email object.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the read-only Email.NumAttachedMessages property, which is the
    --  number of embedded emails (message/rfc822 MIME parts) contained in the email.

    --  Create an inner email that we'll attach as a complete nested message.
    DECLARE @innerEmail int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @innerEmail OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @innerEmail, 'Subject', 'I am an attached message'
    EXEC sp_OASetProperty @innerEmail, 'Body', 'This entire email is nested inside another email.'
    EXEC sp_OASetProperty @innerEmail, 'From', 'alice@example.com'
    EXEC sp_OAMethod @innerEmail, 'AddTo', @success OUT, 'Bob', 'bob@example.com'

    --  Create the outer email and attach the inner email directly as a
    --  message/rfc822 part.  AttachEmail attaches a copy of the inner email.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'Outer email with an attached message'
    EXEC sp_OASetProperty @email, 'Body', 'See the attached email.'
    EXEC sp_OAMethod @email, 'AttachEmail', @success OUT, @innerEmail


    EXEC sp_OAGetProperty @email, 'NumAttachedMessages', @iTmp0 OUT
    PRINT 'NumAttachedMessages = ' + @iTmp0


    EXEC @hr = sp_OADestroy @innerEmail
    EXEC @hr = sp_OADestroy @email


END
GO