Example E2216
Visible to All Users

Rich Text Editor for WinForms - Build a Mail Application with the RichEditControl

This example illustrates how to use the RichEditControl to create a simple mail application that sends a loaded document in HTML format. The System.Net.Mail.MailMessage class methods are used to create a message. The message is sent with the help of the System.Net.Mail.SmtpClient class instance.

image

Implementation Details

  • Convert the loaded document into the HTML formatted stream. Use the GetHtmlText method to retrieve the loaded document as HTML formatted content.
  • You can use the IUriProviderService interface implementation to embed images as linked resources.
  • Handle the BeforeExport event to specify the required encoding and create a System.Net.Mail.AlternateView object required for HTML email format.

Files to Look At

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

RichEditSendMail/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Mail; using DevExpress.XtraRichEdit; using DevExpress.Utils; using DevExpress.Office.Services; using System.Net.Mime; using System.IO; using DevExpress.XtraRichEdit.Export; using DevExpress.XtraRichEdit.Export.Html; using System.Net; using DevExpress.Office.Utils; namespace RichEditSendMail { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); richEdit.LoadDocument("Hello.docx"); } private void btnSend_Click(object sender, EventArgs e) { if ((edtTo.Text.Trim() == "") || (edtSubject.Text.Trim() == "") || (edtSmtp.Text.Trim() == "")) { MessageBox.Show("Fill in required fields"); return; } try { MailMessage mailMessage = new MailMessage("XtraRichEdit@devexpress.com", edtTo.Text); mailMessage.Subject = edtSubject.Text; RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(richEdit, mailMessage); exporter.Export(); SmtpClient mailSender = new SmtpClient(edtSmtp.Text); //specify your login/password to log on to the SMTP server, if required //mailSender.Credentials = new NetworkCredential("login", "password"); mailSender.Send(mailMessage); MessageBox.Show("Message sent", "RichEditSendMail", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exc) { MessageBox.Show(exc.Message); } } public class RichEditMailMessageExporter : IUriProvider { readonly RichEditControl control; readonly MailMessage message; List<AttachementInfo> attachments; int imageId; public RichEditMailMessageExporter(RichEditControl control, MailMessage message) { Guard.ArgumentNotNull(control, "control"); Guard.ArgumentNotNull(message, "message"); this.control = control; this.message = message; } public virtual void Export() { this.attachments = new List<AttachementInfo>(); AlternateView htmlView = CreateHtmlView(); message.AlternateViews.Add(htmlView); message.IsBodyHtml = true; } protected internal virtual AlternateView CreateHtmlView() { control.BeforeExport += OnBeforeExport; string htmlBody = control.Document.GetHtmlText(control.Document.Range, this); AlternateView view = AlternateView.CreateAlternateViewFromString(htmlBody, Encoding.UTF8, MediaTypeNames.Text.Html); control.BeforeExport -= OnBeforeExport; int count = attachments.Count; for (int i = 0; i < count; i++) { AttachementInfo info = attachments[i]; LinkedResource resource = new LinkedResource(info.Stream, info.MimeType); resource.ContentId = info.ContentId; view.LinkedResources.Add(resource); } return view; } void OnBeforeExport(object sender, BeforeExportEventArgs e) { HtmlDocumentExporterOptions options = e.Options as HtmlDocumentExporterOptions; if (options != null) { options.Encoding = Encoding.UTF8; } } #region IUriProvider Members public string CreateCssUri(string rootUri, string styleText, string relativeUri) { return String.Empty; } public string CreateImageUri(string rootUri, OfficeImage image, string relativeUri) { string imageName = String.Format("image{0}", imageId); imageId++; OfficeImageFormat imageFormat = GetActualImageFormat(image.RawFormat); Stream stream = new MemoryStream(image.GetImageBytes(imageFormat)); string mediaContentType = OfficeImage.GetContentType(imageFormat); AttachementInfo info = new AttachementInfo(stream, mediaContentType, imageName); attachments.Add(info); return "cid:" + imageName; } OfficeImageFormat GetActualImageFormat(OfficeImageFormat _officeImageFormat) { if (_officeImageFormat == OfficeImageFormat.Exif || _officeImageFormat == OfficeImageFormat.MemoryBmp) return OfficeImageFormat.Png; else return _officeImageFormat; } #endregion } public class AttachementInfo { Stream stream; string mimeType; string contentId; public AttachementInfo(Stream stream, string mimeType, string contentId) { this.stream = stream; this.mimeType = mimeType; this.contentId = contentId; } public Stream Stream { get { return stream; } } public string MimeType { get { return mimeType; } } public string ContentId { get { return contentId; } } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.