MailBee.NET Objects 4.0

MailMessage.MakePlainBodyFromHtmlBody Method 

Creates a plain text body from the existing HTML body of the message.

public bool MakePlainBodyFromHtmlBody();

Return Value

true if the plain-text body was successfully generated from the HTML body; false if the message did not have an HTML body.

Remarks

This method overwrites the original value of BodyPlainText property if any. BodyHtmlText is left intact (i.e. this method does not replace HTML body with plain-text body, it rather adds alternative plain-text version of the HTML body to the message).

To make plain-text body be generated automatically when the message contains only HTML body, set msg.Builder.HtmlToPlainMode = HtmlToPlainAutoConvert.IfNoPlain prior to sending the message (assuming msg is MailMessage instance).

If you are parsing an existing message (received from the mail server, loaded from file, etc) rather than composing a new one, set msg.Parser.HtmlToPlainMode = HtmlToPlainAutoConvert.IfNoPlain instead.

Example

This sample creates the HTML body of the message from .HTM file and converts it into the plain text body. Reading from stream and using MakePlainBodyFromHtmlBody is for demonstration purposes only. In real world apps, it's easier to use LoadBodyText method to load the file and MailMessage.Builder.HtmlToPlainMode property to enable automatic creating plain-text version of the HTML body.

[C#]
// To use the code below, import these namespaces at the top of your code.
using System.IO;
using MailBee;
using MailBee.Mime;

// The actual code (put it into a method of your class).
using (StreamReader sr = new StreamReader(@"C:\Temp\doc.htm"))
{
    MailMessage msg = new MailMessage();
    msg.BodyHtmlText = sr.ReadToEnd();
    msg.MakePlainBodyFromHtmlBody();

    Console.WriteLine(msg.BodyPlainText);
}
[Visual Basic]
' To use the code below, import these namespaces at the top of your code.
Imports System.IO
Imports MailBee
Imports MailBee.Mime

' The actual code (put it into a method of your class).
Dim msg As New MailMessage
Dim sr As StreamReader
Try
    sr = New StreamReader("C:\Temp\doc.htm")
    msg.BodyHtmlText = sr.ReadToEnd()
    msg.MakePlainBodyFromHtmlBody()

    Console.WriteLine(msg.BodyPlainText)
Finally
    If Not sr Is Nothing Then
        sr.Close()
    End If
End Try

See Also

MailMessage Class | MailBee.Mime Namespace | Builder