SQL Server
SQL Server
Create DSN (Delivery Status Notification) Email
See more Email Object Examples
Demonstrates how to create a DSN (Delivery Status Notification) Email having the format as defined in RFC 3464.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
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_OASetProperty @email, 'Subject', 'Test'
EXEC sp_OASetProperty @email, 'From', 'joe@example.com'
EXEC sp_OASetProperty @email, 'Body', 'This is a test'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, '', 'recipient@example.com'
EXEC sp_OAMethod @email, 'GetMime', @sTmp0 OUT
PRINT @sTmp0
PRINT ''
PRINT '-------------------------------------------------------------'
PRINT ''
-- This is the email we just created:
-- MIME-Version: 1.0
-- Date: Mon, 12 May 2025 11:13:15 -0500
-- Message-ID: <917FA49F75544EF51948B0A52F403B925B51073F@SLICE>
-- Content-Type: text/plain; charset=us-ascii; format=flowed
-- Content-Transfer-Encoding: 7bit
-- X-Priority: 3 (Normal)
-- Subject: Test
-- From: joe@example.com
-- To: recipient@example.com
--
-- This is a test
-- -----------------------------------------------------------------
-- Convert the above email into a DSN (Delivery Status Notification)
DECLARE @xml int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
EXEC sp_OASetProperty @xml, 'Tag', 'DeliveryStatusFields'
EXEC sp_OAMethod @xml, 'NewChild2', NULL, 'Final-Recipient', 'rfc822; recipient@example.com'
EXEC sp_OAMethod @xml, 'NewChild2', NULL, 'Action', 'failed'
EXEC sp_OAMethod @xml, 'NewChild2', NULL, 'Status', '5.1.2'
EXEC sp_OAMethod @xml, 'NewChild2', NULL, 'Diagnostic-Code', 'smtp; 550 5.1.2 Host unknown (Domain name not found)'
DECLARE @dtNow int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dtNow OUT
EXEC sp_OAMethod @dtNow, 'SetFromCurrentSystemTime', @success OUT
EXEC sp_OAMethod @dtNow, 'GetAsRfc822', @sTmp0 OUT, 1
EXEC sp_OAMethod @xml, 'NewChild2', NULL, 'Last-Attempt-Date', @sTmp0
DECLARE @headerOnly int
SELECT @headerOnly = 1
DECLARE @sbText int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbText OUT
EXEC sp_OAMethod @sbText, 'Append', @success OUT, 'This is an automatically generated Delivery Status Notification.' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
EXEC sp_OAMethod @sbText, 'Append', @success OUT, 'Delivery to the following recipient failed permanently:' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
EXEC sp_OAMethod @sbText, 'Append', @success OUT, ' recipient@example.com' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
EXEC sp_OAMethod @sbText, 'Append', @success OUT, 'Technical details of permanent failure:' + CHAR(13) + CHAR(10)
EXEC sp_OAMethod @sbText, 'Append', @success OUT, 'DNS Error: Domain name not found' + CHAR(13) + CHAR(10)
DECLARE @explain nvarchar(4000)
EXEC sp_OAMethod @sbText, 'GetAsString', @explain OUT
DECLARE @dsnEmail int
EXEC @hr = sp_OACreate 'Chilkat.Email', @dsnEmail OUT
EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
EXEC sp_OAMethod @email, 'ToDsn', @success OUT, @explain, @sTmp0, @headerOnly, @dsnEmail
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @dtNow
EXEC @hr = sp_OADestroy @sbText
EXEC @hr = sp_OADestroy @dsnEmail
RETURN
END
EXEC sp_OAMethod @dsnEmail, 'GetMime', @sTmp0 OUT
PRINT @sTmp0
-- Here's the MIME of the DNS email we just created:
-- -------------------------------------------------
-- Content-Type: multipart/report; report-type="delivery-status"; boundary="------------060100020300020303000802"
--
-- --------------060100020300020303000802
-- Content-Type: text/plain; charset=windows-1252; format=flowed
-- Content-Transfer-Encoding: 7bit
--
-- This is an automatically generated Delivery Status Notification.
--
-- Delivery to the following recipient failed permanently:
--
-- recipient@example.com
--
-- Technical details of permanent failure:
-- DNS Error: Domain name not found
--
-- --------------060100020300020303000802
-- Content-Type: message/delivery-status
--
-- Final-Recipient: rfc822; recipient@example.com
-- Action: failed
-- Status: 5.1.2
-- Diagnostic-Code: smtp; 550 5.1.2 Host unknown (Domain name not found)
-- Last-Attempt-Date: Mon, 12 May 2025 11:30:39 -0500
--
-- --------------060100020300020303000802
-- Content-Type: text/rfc822-headers; charset=windows-1252
--
-- MIME-Version: 1.0
-- Date: Mon, 12 May 2025 11:30:39 -0500
-- Message-ID: <B8E6875D582A78AE779FC0B46ACC8C858CEAF608@SLICE>
-- Content-Type: text/plain; charset=us-ascii; format=flowed
-- Content-Transfer-Encoding: 7bit
-- X-Priority: 3 (Normal)
-- Subject: Test
-- From: joe@example.com
-- To: recipient@example.com
-- --------------060100020300020303000802--
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @dtNow
EXEC @hr = sp_OADestroy @sbText
EXEC @hr = sp_OADestroy @dsnEmail
END
GO