Articles in this section
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 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();
 
// Export 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 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 Body of the mail message.
mailMessage.Body = htmlText;
 
// Set True if the Body content is HTML. False, if the Body content is plain text.
mailMessage.IsBodyHtml = true;

VB

' Initializes a stream
Dim stream As Stream = New MemoryStream()
 
' Export 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 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 Body of the mail message.
mailMessage.Body = htmlText
 
' Set True if the Body content is HTML. False, if the Body content is plain text.
mailMessage.IsBodyHtml = True

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied