SQL Server Requires Chilkat v11.0.0+
SQL Server
Reply to an email and preserve its thread
See more Email Object Examples
Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.
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
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @original int
EXEC @hr = sp_OACreate 'Chilkat.Email', @original OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @original, 'LoadEml', @success OUT, 'with-attachments.eml'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @original, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @original
RETURN
END
DECLARE @reply int
EXEC @hr = sp_OACreate 'Chilkat.Email', @reply OUT
EXEC sp_OAMethod @original, 'ToReply', @success OUT, @reply
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @original, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @original
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OASetProperty @reply, 'From', 'Alex Rivera <alex@example.net>'
-- This is a reply to the author, not a reply-all. Set the destination explicitly.
EXEC sp_OAMethod @reply, 'ClearTo', NULL
EXEC sp_OAMethod @reply, 'ClearCC', NULL
EXEC sp_OAMethod @reply, 'ClearBcc', NULL
DECLARE @destination nvarchar(4000)
EXEC sp_OAMethod @original, 'GetHeaderField', @destination OUT, 'Reply-To'
IF @destination = ''
BEGIN
EXEC sp_OAGetProperty @original, 'From', @destination OUT
END
EXEC sp_OAMethod @reply, 'AddMultipleTo', @success OUT, @destination
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @reply, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @original
EXEC @hr = sp_OADestroy @reply
RETURN
END
-- Link this answer to its parent and preserve the earlier conversation references.
DECLARE @parentId nvarchar(4000)
EXEC sp_OAMethod @original, 'GetHeaderField', @parentId OUT, 'Message-ID'
IF @parentId <> ''
BEGIN
EXEC sp_OAMethod @reply, 'AddHeaderField', NULL, 'In-Reply-To', @parentId
DECLARE @refs int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @refs OUT
EXEC sp_OAMethod @original, 'GetHeaderField', @sTmp0 OUT, 'References'
EXEC sp_OAMethod @refs, 'Append', @success OUT, @sTmp0
EXEC sp_OAGetProperty @refs, 'Length', @iTmp0 OUT
IF @iTmp0 > 0
BEGIN
EXEC sp_OAMethod @refs, 'Append', @success OUT, ' '
END
EXEC sp_OAMethod @refs, 'Append', @success OUT, @parentId
EXEC sp_OAMethod @refs, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @reply, 'AddHeaderField', NULL, 'References', @sTmp0
END
-- A short text reply has no use for the original inline artwork or nested correspondence.
EXEC sp_OAMethod @reply, 'RemoveAttachedMessages', NULL
EXEC sp_OAMethod @reply, 'RemoveHtmlAlternative', NULL
EXEC sp_OAMethod @reply, 'DropRelatedItems', NULL
EXEC sp_OAMethod @reply, 'SetTextBody', NULL, 'Thank you for your message.', 'text/plain'
EXEC sp_OAGetProperty @reply, 'NumTo', @iTmp0 OUT
IF @iTmp0 < 1
BEGIN
PRINT 'Reply has no destination. Review the original sender headers.'
EXEC @hr = sp_OADestroy @original
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @refs
RETURN
END
EXEC sp_OAMethod @reply, 'GetToAddr', @sTmp0 OUT, 0
PRINT 'Reply goes to: ' + @sTmp0
EXEC sp_OAMethod @reply, 'GetHeaderField', @sTmp0 OUT, 'In-Reply-To'
PRINT 'In-Reply-To: ' + @sTmp0
EXEC sp_OAMethod @reply, 'SaveEml', @success OUT, 'reply.eml'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @reply, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @original
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @refs
RETURN
END
PRINT 'Saved reply.eml.'
EXEC @hr = sp_OADestroy @original
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @refs
END
GO