SQL Server
SQL Server
Save All Email Attachments to a Directory
See more Email Object Examples
Demonstrates the Chilkat Email.SaveAllAttachments method, which saves all of the email's attachments to a directory. If the directory or any of its components do not exist, Chilkat creates them automatically. This example adds two attachments and saves them.
Background: Each attachment is written using its own filename. Because those filenames come from the sender and may collide, consider the
OverwriteExisting property (which can auto-generate unique names) and RemoveAttachmentPaths (which strips embedded path info) to keep saved files safe and non-clobbering — important when processing untrusted incoming mail.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the SaveAllAttachments method, which saves all attachments to a directory.
-- If the directory (or any of its components) does not exist, Chilkat creates it.
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', 'Save all attachments'
EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'a.txt', 'first attachment'
EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'b.txt', 'second attachment'
-- Save every attachment into the specified directory.
EXEC sp_OAMethod @email, 'SaveAllAttachments', @success OUT, 'qa_output/attachments'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
RETURN
END
PRINT 'Saved all attachments.'
-- Note: The path "qa_output/..." is a relative local filesystem path,
-- relative to the current working directory of the running application.
EXEC @hr = sp_OADestroy @email
END
GO