Sample code for 30+ languages & platforms
SQL Server

Get the HTML Body of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.GetHtmlBody method, which returns the body having the text/html content type. It reads the HTML representation already present in the email object — it does not generate one from the plain-text body. This example sets an HTML body and reads it back.

Background: Because an email can carry both plain-text and HTML representations, GetHtmlBody targets the HTML one specifically, whereas the general Body property is ambiguous when both exist. If the message has no HTML representation, this returns an empty string — use HasHtmlBody to check first, or GetPlainTextBody for the text version.

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 GetHtmlBody method, which returns the body having the text/html content
    --  type.  It reads the HTML representation already present in the email object.

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

    EXEC sp_OASetProperty @email, 'Subject', 'GetHtmlBody example'

    EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><body><b>Hello in HTML.</b></body></html>'

    --  Retrieve the text/html body.
    DECLARE @htmlBody nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetHtmlBody', @htmlBody OUT

    PRINT @htmlBody

    EXEC @hr = sp_OADestroy @email


END
GO