Sample code for 30+ languages & platforms
SQL Server

Set a MIME Body from Transfer-Encoded Text

See more MIME Examples

Demonstrates the Chilkat Mime.SetBodyFromEncoded method, which sets the body from already transfer-encoded text. The first argument is the encoding (base64 or quoted-printable) and the second is the encoded text.

Background: Sometimes you already hold content in its encoded form — a Base64 string from a database or another API — and re-decoding just to have Chilkat re-encode it is wasteful. This method stores the encoded text as-is and records the matching transfer encoding, so the body is correct without a needless round trip. Only the standard reversible encodings, Base64 and quoted-printable, are accepted.

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

    --  Demonstrates the Mime.SetBodyFromEncoded method, which sets the body from already transfer-
    --  encoded text.  The 1st argument is the encoding ("base64" or "quoted-printable") and the 2nd is
    --  the encoded text.

    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_OASetProperty @mime, 'ContentType', 'text/plain'

    --  The body is provided already Base64-encoded; Chilkat stores it without re-encoding.
    DECLARE @base64Body nvarchar(4000)
    SELECT @base64Body = 'SGVsbG8sIHRoaXMgaXMgdGhlIGJvZHku'
    EXEC sp_OAMethod @mime, 'SetBodyFromEncoded', @success OUT, 'base64', @base64Body
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  The decoded body is the original text.
    DECLARE @decoded nvarchar(4000)
    EXEC sp_OAMethod @mime, 'GetBodyDecoded', @decoded 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 @decoded

    EXEC @hr = sp_OADestroy @mime


END
GO