Sample code for 30+ languages & platforms
SQL Server

Create a multipart/alternative MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartAlternative method, which initializes the object as an empty multipart/alternative entity. It takes no arguments; the alternative representations are then appended.

Background: multipart/alternative holds several renderings of the same content and lets the client pick the best one it can display — the standard way to send an email as both plain text and HTML. Order matters: parts go from least to most preferred, so the plain-text version comes first and the HTML last, and a client shows the last one it understands.

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 Mime.NewMultipartAlternative method, which initializes the object as an empty
    --  multipart/alternative entity, clearing any existing content.  It takes no arguments.

    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, 'NewMultipartAlternative', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  multipart/alternative holds two representations of the SAME content; the client displays the
    --  last one it can render.  Add a plain-text part, then an HTML part.
    DECLARE @textPart int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @textPart OUT

    EXEC sp_OAMethod @textPart, 'SetBodyFromPlainText', @success OUT, 'Hello (plain text version).'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @textPart, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @textPart
        RETURN
      END
    EXEC sp_OAMethod @mime, 'AppendPart', @success OUT, @textPart
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @textPart
        RETURN
      END

    DECLARE @htmlPart int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @htmlPart OUT

    EXEC sp_OAMethod @htmlPart, 'SetBodyFromHtml', @success OUT, '<html><body><b>Hello</b> (HTML version).</body></html>'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @htmlPart, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @textPart
        EXEC @hr = sp_OADestroy @htmlPart
        RETURN
      END
    EXEC sp_OAMethod @mime, 'AppendPart', @success OUT, @htmlPart
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @textPart
        EXEC @hr = sp_OADestroy @htmlPart
        RETURN
      END


    EXEC sp_OAGetProperty @mime, 'NumParts', @iTmp0 OUT
    PRINT 'Number of alternatives: ' + @iTmp0

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @textPart
    EXEC @hr = sp_OADestroy @htmlPart


END
GO