Sample code for 30+ languages & platforms
SQL Server

B-Encode a String for MIME Headers

See more Email Object Examples

Demonstrates the Chilkat Email.BEncodeString method, which converts the Unicode string in the first argument to the charset named in the second argument, B-encodes the resulting multibyte data, and returns the encoded string. This is the representation used for non-ASCII text in MIME header fields.

Background: MIME headers were defined to carry only plain ASCII, so non-ASCII text (accents, non-Latin scripts) must be wrapped in an "encoded-word" per RFC 2047. The B encoding is Base64-based: the text becomes something like =?utf-8?B?...?=, which mail software recognizes and decodes back to the original characters. There is also a Q (quoted-printable-style) encoding for the same purpose; B-encoding is preferred when most characters are non-ASCII.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    --  Demonstrates the BEncodeString method, which converts a Unicode string to a specified
    --  charset, B-encodes (RFC 2047) the resulting bytes, and returns the encoded string.
    --  This is the form used for non-ASCII text in MIME header fields.

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

    --  B-encode a Unicode string using the utf-8 charset.
    DECLARE @encoded nvarchar(4000)
    EXEC sp_OAMethod @email, 'BEncodeString', @encoded OUT, 'Cafe Meeting Notes', 'utf-8'


    PRINT 'B-encoded: ' + @encoded

    EXEC @hr = sp_OADestroy @email


END
GO