Sample code for 30+ languages & platforms
SQL Server

Attach a File with an Explicit Content Type

See more Email Object Examples

Demonstrates the Chilkat Email.AddFileAttachment2 method, which attaches a file from the filesystem and lets you explicitly specify its content type rather than having Chilkat infer it from the file extension. This example attaches a binary file as application/octet-stream.

Background: Extension-based type detection is convenient but not always right — a file may have an unusual or missing extension, or you may need a very specific MIME type for the recipient to process it correctly. Specifying the content type explicitly removes the guesswork. application/octet-stream is the generic "arbitrary binary data" type, a safe default that tells the client to treat the attachment as an opaque download rather than trying to render it.

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 AddFileAttachment2 method, which attaches a file and lets you
    --  explicitly specify its content type instead of letting Chilkat infer 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', 'Email with a file attachment'
    EXEC sp_OASetProperty @email, 'Body', 'Please see the attached file.'

    --  Attach a file, explicitly specifying the content type.
    EXEC sp_OAMethod @email, 'AddFileAttachment2', @success OUT, 'qa_data/attachments/data.bin', 'application/octet-stream'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    PRINT 'NumAttachments = ' + @iTmp0

    --  Note: The path "qa_data/attachments/data.bin" is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email


END
GO