Sample code for 30+ languages & platforms
SQL Server

Count the Report Parts in a multipart/report Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumReports property, which is the number of report parts in a multipart/report email. A part is counted as a report when its Content-Type is message/* (except message/rfc822) or text/rfc822-headers. Use GetReport to retrieve the body of each report part; indexes are zero-based. This example loads a report email and prints each report part.

Background: When a message can't be delivered, the mail system usually sends back a bounce known as a Delivery Status Notification (DSN). DSNs use the multipart/report structure, which bundles several parts: a human-readable explanation, a machine-readable message/delivery-status part describing exactly what happened (recipient, status code, failing server), and often the original message's headers. Reading these report parts lets a program automatically detect and process bounces.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the read-only Email.NumReports property, which is the number of report
    --  parts in a multipart/report email (for example, a Delivery Status Notification).
    --  Use GetReport to retrieve the body of each report part.  Indexes are zero-based.

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

    --  Load a multipart/report email (such as a bounce / DSN message).
    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/dsn_bounce.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    DECLARE @n int
    EXEC sp_OAGetProperty @email, 'NumReports', @n OUT

    PRINT 'NumReports = ' + @n

    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN


        PRINT '---- Report ' + @i + ' ----'
        EXEC sp_OAMethod @email, 'GetReport', @sTmp0 OUT, @i
        PRINT @sTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @email


END
GO