How to: Send the Document as an E-mail

  • 3 minutes to read

The following example describes how to send the mail merge result as an e-mail.

Refer to the Mail Merge topic for an example on how to create a mail-merge document. Follow the steps below to export the result as a Microsoft® Outlook® mail item.

Create an Exporter Class

  1. Add a reference to the Microsoft.Office.Interop.Outlook assembly.
  2. Create an exporter class that implements the IUriProvider interface. Declare the class constructor as follows:

    using DevExpress.Office.Services;
    using Outlook = Microsoft.Office.Interop.Outlook;
    public class RichEditMailMessageExporter : IUriProvider
      readonly RichEditDocumentServer server;
      readonly Outlook.MailItem mailItem; 
      public RichEditMailMessageExporter(RichEditDocumentServer server, Outlook.MailItem mailItem)
        Guard.ArgumentNotNull(server, "server");
        Guard.ArgumentNotNull(mailItem, "mailItem");
        this.server = richServer;
        this.mailItem = mailItem;
  3. Implement the Export method to retrieve the document text in HTML format and convert it into the message body. Subscribe to the RichEditDocumentServer.BeforeExport event.

    string tempFiles = Path.Combine(Directory.GetCurrentDirectory(), "TempFiles");
    public virtual void Export()
         if (!Directory.Exists(tempFiles))
         server.BeforeExport += OnBeforeExport;
         string htmlBody = server.Document.GetHtmlText(server.Document.Range, this);
         server.BeforeExport -= OnBeforeExport;
         mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
         mailItem.HTMLBody = htmlBody;
  4. Specify the HtmlDocumentExporterOptions.Encoding property in the BeforeExport event handler.

    private void OnBeforeExport(object sender, BeforeExportEventArgs e)
         HtmlDocumentExporterOptions options = e.Options as HtmlDocumentExporterOptions;
         if (options != null)
            options.Encoding = Encoding.UTF8;
  5. Implement the interface's IUriProvider.CreateCssUri and IUriProvider.CreateImageUri methods. In this example, the IUriProvider.CreateImageUri method retains the document images and attaches them to the mail message.

    int imageId;
    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}.png", imageId);
       string imagePath = Path.Combine(tempFiles, imageName);
       image.NativeImage.Save(imagePath, ImageFormat.Png);
       mailItem.Attachments.Add(imagePath, Outlook.OlAttachmentType.olByValue, 0, Type.Missing);
       return "cid:" + imageName;

Send the result to the Microsoft® Outlook® application

Declare a new Outlook application object and create a new mail item in the main class. Create a new RichEditMailMessageExporter instance and pass the RichEditDocumentServer and the MailItem objects as parameters. Call the Export method.

Outlook.Application application = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);

RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(richServer, mailItem);


A full code example is available in the How to Export the Mail-Merge Document into an Outlook Mail Item repository on GitHub.