Back to Devexpress

How to: Send the Mail-Merge Document as an E-Mail

wpf-120509-controls-and-libraries-rich-text-editor-examples-export-how-to-send-the-mail-merge-document-as-an-e-mail.md

latest4.0 KB
Original Source

How to: Send the Mail-Merge Document as an E-Mail

  • Apr 01, 2021
  • 4 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:

  3. Implement the Export method to retrieve the document text in HTML format and convert it into the message body. Subscribe to the RichEditControl.BeforeExport event.

  4. Specify the HtmlDocumentExporterOptions.Encoding property in the BeforeExport event handler.

  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.

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 RichEditControl and the MailItem objects as parameters. Call the Export method.

In this example, the document is sent as a mail item on a “Send Mail” button click.

csharp
private void btnSend_Click(object sender, EventArgs e)
        {
            if ((edtTo.Text.Trim() == "") || (edtSubject.Text.Trim() == ""))
            {
                MessageBox.Show("Fill in required fields");
                return;
            }
            try
            {
                Outlook.Application application = new Outlook.Application();
                Outlook.MailItem mailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);

                mailItem.To = edtTo.Text;
                mailItem.Subject = edtSubject.Text;

                RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(richEdit, mailItem);
                exporter.Export();

                mailItem.Display(false);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
vb
Private Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
    If (edtTo.Text.Trim() = "") OrElse (edtSubject.Text.Trim() = "") Then
        MessageBox.Show("Fill in required fields")
        Return
    End If

    Try
        Dim application As Outlook.Application = New Outlook.Application()
        Dim mailItem As Outlook.MailItem = CType(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
        mailItem.[To] = edtTo.Text
        mailItem.Subject = edtSubject.Text
        Dim exporter As RichEditMailMessageExporter = New RichEditMailMessageExporter(richEdit, mailItem)
        exporter.Export()
        mailItem.Display(False)
    Catch exc As Exception
        MessageBox.Show(exc.Message)
    End Try
End Sub