Sample code for 30+ languages & platforms
SQL Server

Set the Charset of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentCharset method, which sets the charset parameter of the Content-Type header field for the attachment at a given zero-based index. This example adds a text attachment and sets its charset to utf-8.

Background: For a text attachment, the charset parameter tells the receiving client which character encoding the bytes use, so accented or non-Latin text renders correctly. Setting it explicitly (typically utf-8) removes ambiguity when the attachment contains non-ASCII content — without it, a client may guess wrong and display garbled characters.

Chilkat SQL Server Downloads

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

    --  Demonstrates the SetAttachmentCharset method, which sets the charset parameter of the
    --  Content-Type header field for the attachment at the given zero-based index.

    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', 'Set attachment charset'

    EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'notes.txt', 'Some notes.'

    --  Set the charset of the first attachment (index 0) to utf-8.
    EXEC sp_OAMethod @email, 'SetAttachmentCharset', @success OUT, 0, 'utf-8'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  The attachment's Content-Type now includes charset="utf-8".
    EXEC sp_OAMethod @email, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @email


END
GO