Sample code for 30+ languages & platforms
SQL Server

Create a Delivery Status Notification (DSN)

See more Email Object Examples

Demonstrates the Chilkat Email.ToDsn method, which creates a new Delivery Status Notification (DSN) email in the format specified by RFC 3464, based on this email. The generated DSN is returned in the last argument, and the source email is not modified. The first argument is the human-readable explanation, the second is the delivery-status fields, and the third indicates whether to include headers only. This example builds a DSN for a received message.

Background: A DSN is the formal "bounce" a mail system returns when a message cannot be delivered. It is a multipart/report containing a human-readable explanation plus a machine-readable message/delivery-status part (with fields like Action and Status). ToDsn generates that structured report for you — useful when building a mail server or gateway that must issue standards-compliant bounces. After generating it, populate the sender/recipient fields and send.

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 ToDsn method, which creates a new Delivery Status Notification (DSN)
    --  email (RFC 3464) based on this email.  The generated DSN is returned in the last argument.
    --  The source email is not modified.

    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/received.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Create a DSN for the received message.  The 1st argument is the human-readable
    --  explanation, the 2nd is the delivery-status fields, and the 3rd (headerOnly) is false.
    DECLARE @dsn int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @dsn OUT

    EXEC sp_OAMethod @email, 'ToDsn', @success OUT, 'The message could not be delivered to the recipient.', 'Action: failed; Status: 5.1.1', 0, @dsn
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @dsn
        RETURN
      END

    --  Populate additional sender/recipient fields on the DSN before sending, then send it.
    EXEC sp_OAMethod @dsn, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    --  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
    EXEC @hr = sp_OADestroy @dsn


END
GO