Sample code for 30+ languages & platforms
SQL Server

Attach a File to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the one-call way to add an attachment: Chilkat reads the file, infers its Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Mime.AppendPartFromFile method, which reads a local file, creates an attachment
    --  part, and appends it.  The only argument is the file path; the content headers are chosen from
    --  the filename extension.

    DECLARE @mime int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @mime, 'NewMultipartMixed', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  A message body part.
    DECLARE @bodyPart int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @bodyPart OUT

    EXEC sp_OAMethod @bodyPart, 'SetBodyFromPlainText', @success OUT, 'Please see the attached files.'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bodyPart, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @bodyPart
        RETURN
      END
    EXEC sp_OAMethod @mime, 'AppendPart', @success OUT, @bodyPart
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @bodyPart
        RETURN
      END

    --  Attach files directly.
    EXEC sp_OAMethod @mime, 'AppendPartFromFile', @success OUT, 'qa_data/report.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @bodyPart
        RETURN
      END
    EXEC sp_OAMethod @mime, 'AppendPartFromFile', @success OUT, 'qa_data/photo.jpg'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @bodyPart
        RETURN
      END


    EXEC sp_OAGetProperty @mime, 'NumParts', @iTmp0 OUT
    PRINT 'Number of parts: ' + @iTmp0

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @bodyPart


END
GO