Sample code for 30+ languages & platforms
SQL Server

Remove the Plain-Text Body from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemovePlainTextAlternative method, which removes the plain-text body from the email if one exists. Other body representations, attachments, and related items remain unchanged. This example builds a message with both plain-text and HTML alternatives, then removes the plain text.

Background: This is the counterpart to RemoveHtmlAlternative. Removing the plain-text alternative leaves an HTML-only message. Note that dropping the plain-text fallback is generally discouraged for real mail — it hurts accessibility and can raise spam scores — but it is occasionally needed when a downstream system expects a single HTML representation.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    --  Demonstrates the RemovePlainTextAlternative method, which removes the plain-text body
    --  from the email (if one exists).  Other body representations, attachments, and related
    --  items remain.

    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', 'Remove plain-text alternative'

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

    EXEC sp_OAGetProperty @email, 'NumAlternatives', @iTmp0 OUT
    PRINT 'NumAlternatives before = ' + @iTmp0

    EXEC sp_OAMethod @email, 'HasPlainTextBody', @iTmp0 OUT
    PRINT 'HasPlainTextBody before: ' + @iTmp0

    --  Remove the plain-text body, leaving the HTML alternative.
    EXEC sp_OAMethod @email, 'RemovePlainTextAlternative', NULL

    EXEC sp_OAGetProperty @email, 'NumAlternatives', @iTmp0 OUT
    PRINT 'NumAlternatives after = ' + @iTmp0

    EXEC sp_OAMethod @email, 'HasPlainTextBody', @iTmp0 OUT
    PRINT 'HasPlainTextBody after: ' + @iTmp0

    EXEC @hr = sp_OADestroy @email


END
GO