SQL Server
SQL Server
Add a String Attachment to an Email
See more Email Object Examples
Demonstrates the Chilkat Email object's AddStringAttachment method, which adds an attachment directly from an in-memory string — no file needs to exist on disk. The 1st argument is the filename that is written into the MIME (it is not a path to a file that is read), and the 2nd argument is the text that becomes the attachment's body. In this example we build a simple email, attach a small CSV file namedpeople.csv straight from a string, and then print the resulting MIME so you can see the attachment embedded in the message.
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 @sTmp0 nvarchar(4000)
-- This example demonstrates the Email object's AddStringAttachment method.
-- AddStringAttachment adds an attachment directly from an in-memory string.
-- The 1st argument is the filename to be placed in the MIME (not a file to be read).
-- The 2nd argument is the text content that becomes the attachment body.
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', 'Email with a string attachment'
EXEC sp_OASetProperty @email, 'Body', 'See the attached CSV file.'
EXEC sp_OASetProperty @email, 'From', 'mary@example.com'
DECLARE @success int
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Joe', 'joe@example.com'
-- The text content of the attachment.
DECLARE @csvData nvarchar(4000)
SELECT @csvData = 'id,name' + CHAR(13) + CHAR(10) + '1,Alice' + CHAR(13) + CHAR(10) + '2,Bob' + CHAR(13) + CHAR(10)
-- Add the string as an attachment named "people.csv".
EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'people.csv', @csvData
-- Show the full MIME of the email, which now includes the attachment.
EXEC sp_OAMethod @email, 'GetMime', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
END
GO