Sample code for 30+ languages & platforms
SQL Server

Get the Content-Type of Each Alternative Body

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeContentType method, which returns the content type of the Nth alternative body. The NumAlternatives property gives the number of alternatives, and indexes are zero-based. This example builds a message with plain-text and HTML alternatives and prints the content type of each.

Background: Each alternative in a multipart/alternative message declares a Content-Type such as text/plain or text/html. Enumerating those types tells you which representations a message actually contains before you fetch a body — letting you decide, for instance, whether an HTML version exists or whether you must fall back to plain text. It is the natural companion to GetAlternativeBody, which fetches the body at the same index.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  Demonstrates the GetAlternativeContentType method, which returns the content type of
    --  the Nth alternative body.  The NumAlternatives property gives the number of
    --  alternatives; indexes are zero-based.

    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>'

    DECLARE @n int
    EXEC sp_OAGetProperty @email, 'NumAlternatives', @n OUT
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN


        EXEC sp_OAMethod @email, 'GetAlternativeContentType', @sTmp0 OUT, @i
        PRINT 'Alternative ' + @i + ' content type: ' + @sTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @email


END
GO