Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Create a message disposition notification

See more Email Object Examples

An MDN describes a recipient-side event such as displaying a message. Create one only when the application has an appropriate request and the recipient has consented. Set Final-Recipient from the actual receiving mailbox. MDN format.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.

    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, 'message.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    DECLARE @fields int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @fields OUT

    EXEC sp_OASetProperty @fields, 'Tag', 'DispositionFields'
    EXEC sp_OAMethod @fields, 'NewChild2', NULL, 'Final-Recipient', 'rfc822; alex@example.net'
    DECLARE @originalId nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetHeaderField', @originalId OUT, 'Message-ID'
    IF @originalId <> ''
      BEGIN
        EXEC sp_OAMethod @fields, 'NewChild2', NULL, 'Original-Message-ID', @originalId
      END
    EXEC sp_OAMethod @fields, 'NewChild2', NULL, 'Disposition', 'manual-action/MDN-sent-manually; displayed'
    DECLARE @report int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @report OUT

    EXEC sp_OAMethod @fields, 'GetXml', @sTmp0 OUT
    EXEC sp_OAMethod @email, 'ToMdn', @success OUT, 'The recipient displayed your message.', @sTmp0, 1, @report
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @fields
        EXEC @hr = sp_OADestroy @report
        RETURN
      END
    EXEC sp_OASetProperty @report, 'From', 'Alex Rivera <alex@example.net>'

    EXEC sp_OAMethod @report, 'SaveEml', @success OUT, 'disposition.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @report, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @fields
        EXEC @hr = sp_OADestroy @report
        RETURN
      END

    PRINT 'Saved disposition.eml.'

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @fields
    EXEC @hr = sp_OADestroy @report


END
GO