The requested page is not available for the requested platform. You are viewing the content for Default platform.

Export to MailMessage

Overview

This document details the export of a document to an email message represented by an instance of the MailMessage class.

Note that exporting a document to an email message and sending the resulting message to recipients can only be performed at run time. The resulting MailMessage instance contains an HTML representation of the report contents optimized for transmission by email. You can then use the mail functionality of the .NET Framework to programmatically send the generated message to an arbitrary number of recipients using the required email settings.

The options that can be specified for a document exported to an email message are stored in an MailMessageExportOptions object, and can be accessed using a report's ExportOptions.MailMessage property. The MailMessageExportOptions class extends the HtmlExportOptionsBase class and provides access to a set of options common to all HTML-based export formats.

Among these options, the HtmlExportOptionsBase.ExportMode property determines the way in which a document is exported to HTML. For instance, it may be exported to a single file (with a single page header at the beginning and a single page footer at the end), or it may be exported page-by-page to either a single file or different files.

The HtmlExportOptionsBase.TableLayout property determines whether or not to use the table layout in the resulting HTML markup. If this property is set to false, the non-table layout is used instead. The table layout is recommended, as it uses a more compact and efficient markup.

The HtmlExportOptionsBase.ExportWatermarks property specifies whether or not to maintain the existing text and image watermarks of a report in the HTML markup. The following image illustrates the corresponding option in the HTML Export Options dialog.

html-export-options-watermarks

Only report controls that do not intersect each other can be correctly exported to HTML using the table layout.
Intersecting report controls may completely break the resulting mail message layout.

To make sure that your report layout will be preserved in RTF, enable the report's DesignerOptions.ShowExportWarnings property at design time, and ensure there are no controls colored in red, which marks intersecting controls.

ShowExportWarnings

If you need to export a report that contains intersecting controls to an email message, use the non-table layout.

Code Example

This example demonstrates how to export a report to an email message and use the mailing functionality of the .NET Framework to programmatically send the generated message to an arbitrary number of recipients using the required email settings. To accomplish this task, use the report's XtraReport.ExportToMail method. This method returns an instance of the MailMessage class. The body of the resulting mail message contains an HTML representation of the report contents optimized for sending using email.

After a mail message is generated, you can initialize a SmtpClient and call its SmtpClient.Send method to send the message.

using DevExpress.XtraReports.UI;
using System.Collections.Generic;
using System.Net.Mail;
using System.Windows.Forms;
// ...

private void sendButton_Click(object sender, EventArgs e) {
    XtraReport report = new XtraReport1();

    using (SmtpClient client = new SmtpClient("mail.smtpclient.test", 25)) {
        using (MailMessage message = report.ExportToMail("sender@test.test",
        "reciever1@test.test, reciever2@test.test, reciever3@test.test", "Subject")) {
            client.Credentials = new System.Net.NetworkCredential("username", "password");
            client.EnableSsl = true;
            client.Send(message);
        }
    }
}

When emailing a report, consider using the following additional settings.

  • Maintain inline CSS styles

    To specify whether the style properties should be written to the <head> section of an HTML document, or they should be defined at the same place where a specific style is assigned in a document, use the HtmlExportOptionsBase.InlineCss property.

    When the styles are written to the <head> section of an HTML document, they are ignored by some mail clients, and to maintain a consistent look for your reports, the HtmlExportOptionsBase.InlineCss property must be to true.

    In this case, the style properties will be defined at the same place where a specific style is assigned in a document. This will ensure that the resulting document is compatible with any mail client (although at the expense of a larger document size).

    This property also affects how the XRPictureBox and XRBarCode controls are rendered on a web page. By default, images corresponding to rendered controls are assigned to the background-image property of styles that are specified in the <head> section of an HTML document. Setting the HtmlExportOptionsBase.InlineCss property to true will make these controls rendered as <img> elements in the document body, with the src attribute specifying a base64 image representation.

  • Maintain document navigation

    To specify whether or not the document navigation is implemented by using scripts, use the HtmlExportOptionsBase.UseHRefHyperlinks property.

    Setting the HtmlExportOptionsBase.UseHRefHyperlinks property to true will enable the use of standard HTML link references in document navigation. On viewing such a document in a client web browser, unnecessary script security messages will not be displayed.

    When this property is enabled, links with empty text may be generated if a corresponding control's content is not specified. It will be impossible to click such links in a published document.