SQL Server Requires Chilkat v11.0.0+
SQL Server
Preserve the original email and save a modified copy
See more Email Object Examples
Use with-attachments.eml as a stand-in for bytes from an IMAP fetch or a message queue. Preserve those bytes first. Parse them into an Email object, add a local processing header, and serialize the result into a separate buffer. The archived input remains suitable for later byte-level inspection.
Chilkat SQL Server Downloads
--
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 @mimeBytes int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @mimeBytes OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @mimeBytes, 'LoadFile', @success OUT, 'with-attachments.eml'
IF @success <> 1
BEGIN
PRINT 'Failed: mimeBytes.LoadFile.'
EXEC @hr = sp_OADestroy @mimeBytes
RETURN
END
EXEC sp_OAMethod @mimeBytes, 'WriteFile', @success OUT, 'received-original.eml'
IF @success <> 1
BEGIN
PRINT 'Failed: mimeBytes.WriteFile.'
EXEC @hr = sp_OADestroy @mimeBytes
RETURN
END
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
EXEC sp_OAMethod @email, 'SetFromMimeBd', @success OUT, @mimeBytes
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mimeBytes
EXEC @hr = sp_OADestroy @email
RETURN
END
EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'X-Workflow-State', 'reviewed'
-- GetMimeBd appends to its destination. Use a fresh buffer for this message.
DECLARE @rendered int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @rendered OUT
EXEC sp_OAMethod @email, 'GetMimeBd', @success OUT, @rendered
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mimeBytes
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @rendered
RETURN
END
EXEC sp_OAMethod @rendered, 'WriteFile', @success OUT, 'reviewed.eml'
IF @success <> 1
BEGIN
PRINT 'Failed: rendered.WriteFile.'
EXEC @hr = sp_OADestroy @mimeBytes
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @rendered
RETURN
END
PRINT 'Archived received-original.eml and saved reviewed.eml.'
EXEC @hr = sp_OADestroy @mimeBytes
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @rendered
END
GO