Sample code for 30+ languages & platforms
SQL Server

Get a Report Part from a multipart/report Email

See more Email Object Examples

Demonstrates the Chilkat Email.GetReport method, which returns the body content of the Nth report within a multipart/report email. The NumReports property gives the count, and indexes are zero-based. This example loads a report email and prints each report part.

Background: A multipart/report message — used for bounces (DSNs) and read receipts (MDNs) — bundles machine-readable report parts alongside the human-readable explanation. GetReport returns the raw content of one such part so a program can parse it, for example to extract the failing recipient and status code from a bounce. Use GetDeliveryStatusInfo for direct field-level access to the delivery-status part.

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 GetReport method, which returns the body content of the Nth report
    --  within a multipart/report email.  The NumReports property gives the number of reports;
    --  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

    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

    --  A report returned by GetReport is the body of a report part -- for a bounce (DSN),
    --  that is the machine-readable message/delivery-status part.  It looks similar to:
    --  
    --    Reporting-MTA: dns; mail.example.com
    --    Received-From-MTA: dns; sender.example.com
    --    Arrival-Date: Fri, 10 Jul 2026 20:15:30 +0000
    --  
    --    Final-Recipient: rfc822; nonexistent@example.com
    --    Action: failed
    --    Status: 5.1.1
    --    Diagnostic-Code: smtp; 550 5.1.1 <nonexistent@example.com> User unknown

    --  Note: The path "qa_data/..." is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email


END
GO