Category / Section
How to create mail messages using WPF RichTextBox (SfRichTextBoxAdv) control?
2 mins read
You can use the WPF RichTextBox (SfRichTextBoxAdv) control to edit rich-text contents and easily create mail messages using HTML export functionality.
The following XAML code denotes how to initialize the SfRichTextBoxAdv control.
XAML
<RichTextBox:SfRichTextBoxAdv x:Name="richTextBoxAdv" LayoutType="Continuous" xmlns:RichTextBox="clr-namespace:Syncfusion.Windows.Controls.RichTextBoxAdv;assembly=Syncfusion.SfRichTextBoxAdv.Wpf" />
The following code example demonstrates how to create mail messages using SfRichTextBoxAdv.
C#
// Initializes a stream
Stream stream = new MemoryStream();
// Exports SfRichTextBoxAdv content into the stream as HTML.
// You can also export the content as Text Format by specifying FormatType as Txt.
richTextBoxAdv.Save(stream, Syncfusion.Windows.Controls.RichTextBoxAdv.FormatType.Html);
// Seeks the stream to the starting position.
stream.Position = 0;
// Reads the HTML bytes from the stream.
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// Gets the HTML text from bytes.
string htmlText = Encoding.ASCII.GetString(bytes);
// Creates the mail message using the HTML content from SfRichTextBoxAdv.
MailMessage mailMessage = new MailMessage("from@mail.com", "to@mail.com");
mailMessage.Subject = "Mail using SfRichTextBoxAdv";
// Sets the HTML content as the body of the mail message.
mailMessage.Body = htmlText;
// Set true if the body content is HTML. Set false if the body content is plain text.
mailMessage.IsBodyHtml = true;
VB
' Initializes a stream
Dim stream As Stream = New MemoryStream()
' Exports SfRichTextBoxAdv content into the stream as HTML.
' You can also export the content as Text Format by specifying FormatType as Txt.
richTextBoxAdv.Save(stream, Syncfusion.Windows.Controls.RichTextBoxAdv.FormatType.Html)
' Seeks the stream to the starting position.
stream.Position = 0
' Reads the HTML bytes from the stream.
Dim bytes As Byte() = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
' Gets the HTML text from bytes.
Dim htmlText As String = System.Text.Encoding.GetEncoding("ASCII").GetString(bytes, 0, bytes.Length)
' Creates the mail message using the HTML content from SfRichTextBoxAdv.
Dim mailMessage As New MailMessage("me@mail.com", "you@mail.com")
mailMessage.Subject = "Mail using SfRichTextBoxAdv"
' Sets the HTML content as the body of the mail message.
mailMessage.Body = htmlText
' Set true if the body content is HTML. Set false if the body content is plain text.
mailMessage.IsBodyHtml = True