Sample code for 30+ languages & platforms
SQL Server

Get an Alternative Body by Content-Type

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyByContentType method, which returns an alternative body selected by its Content-Type — such as text/plain, text/html, or text/xml — instead of by numeric index. This example retrieves both the plain-text and HTML alternatives by content type.

Background: Selecting by Content-Type is often more convenient than by index, because you usually know which representation you want (the HTML, say) but not its position in the alternative list. This mirrors how a mail client works — it looks up the best matching type to display — and avoids assumptions about ordering, which can vary between messages.

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 GetAlternativeBodyByContentType method, which returns an alternative
    --  body selected by its Content-Type (such as text/plain, text/html, or text/xml) rather
    --  than by index.

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

    --  Create an email with plain-text and HTML alternatives.
    EXEC sp_OAMethod @email, 'SetTextBody', NULL, 'This is the plain-text alternative.', 'text/plain'
    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddHtmlAlternativeBody', @success OUT, '<html><body>This is the HTML alternative.</body></html>'

    --  Get the plain-text alternative by its content type.
    DECLARE @plain nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetAlternativeBodyByContentType', @plain OUT, 'text/plain'

    PRINT 'text/plain body: ' + @plain

    --  Get the HTML alternative by its content type.
    DECLARE @htmlBody nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetAlternativeBodyByContentType', @htmlBody OUT, 'text/html'

    PRINT 'text/html body: ' + @htmlBody

    EXEC @hr = sp_OADestroy @email


END
GO