Sample code for 30+ languages & platforms
SQL Server

Convert 8-bit MIME Parts to Base64

See more MIME Examples

Demonstrates the Chilkat Mime.Convert8Bit method, which recursively converts every part whose Content-Transfer-Encoding is 8bit or binary to base64. It takes no arguments and is a void method.

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: The original SMTP standard only guaranteed 7-bit transport, and some legacy servers still reject or mangle raw 8-bit and binary content. Converting those parts to base64 — a 7-bit-safe encoding — makes the whole message safe to relay through such systems without loss. It walks the entire tree, so a multipart message with several offending parts is fixed in one call.

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
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    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, 'LoadMimeFile', @success OUT, 'qa_data/message.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  Recursively convert every part whose Content-Transfer-Encoding is 8bit or binary to base64.
    --  This makes the MIME safe to transmit over channels that only support 7-bit text (such as some
    --  legacy SMTP servers).
    EXEC sp_OAMethod @mime, 'Convert8Bit', NULL


    PRINT '8bit/binary parts converted to base64.'
    DECLARE @mimeText nvarchar(4000)
    EXEC sp_OAMethod @mime, 'GetMime', @mimeText OUT
    EXEC sp_OAGetProperty @mime, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    PRINT @mimeText

    EXEC @hr = sp_OADestroy @mime


END
GO