officefileapi-120519-word-processing-document-api-examples-export-how-to-send-the-mail-merge-document-as-an-e-mail.md
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.
Add a reference to the Microsoft.Office.Interop.Outlook assembly.
Create an exporter class that implements the IUriProvider interface. Declare the class constructor as follows:
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.
Specify the HtmlDocumentExporterOptions.Encoding property in the BeforeExport event handler.
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.
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);
exporter.Export();
mailItem.Display(false);
Dim application As Outlook.Application = New Outlook.Application()
Dim mailItem As Outlook.MailItem = CType(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
Dim exporter As RichEditMailMessageExporter = New RichEditMailMessageExporter(richEditDocumentServer1, mailItem)
exporter.Export()
mailItem.Display(False)
End Sub
Note
A full code example is available in the How to Export the Mail-Merge Document into an Outlook Mail Item repository on GitHub.