Skip to main content

XtraReport.ExportToMailAsync(MailMessageExportOptions, String, String, String, CancellationToken) Method

Asynchronously exports a report to HTML and inserts it into a specified e-mail message.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v24.1.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

public Task<MailMessage> ExportToMailAsync(
    MailMessageExportOptions options,
    string from,
    string to,
    string subject,
    CancellationToken token = default(CancellationToken)
)

Parameters

Name Type Description
options MailMessageExportOptions

The mail message export options.

from String

The message sender.

to String

A comma-separated list of recipients.

subject String

The e-mail subject.

Optional Parameters

Name Type Default Description
token CancellationToken null

A cancellation token that the task observes.

Returns

Type Description
Task<MailMessage>

The exported e-mail message.

Remarks

This method is equivalent to the ExportToMail(MailMessageExportOptions, String, String, String) method but does not lock other actions performed concurrently. For instance, the user interface remains operational while the application exports a report.

Call ExportToMailAsync from an async method. Prefix the call with the await operator, as shown in the code sample below.

The optional CancellationToken parameter provides a way to send the cancellation signal to the task. The task monitors the token and stops when it receives the signal. Create a CancellationTokenSource class instance and pass its Token property to the ExportToMailAsync method call. Call the CancellationTokenSource.Cancel method to stop the task.

Example

The code sample below exports a report to mail message asynchronously. A CancellationTokenSource class instance is used to allow users to interrupt the report export if it takes too long.

using DevExpress.XtraReports.UI;
using System;
using System.Threading;
// ...
using System.Net.Mail;
// ...
// Use cancellationTokenSource to allow users to stop the document creation process.
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
// ...
// The cancelButton_Click event handler uses cancellationTokenSource to stop the document creation process.
private void cancelButton_Click(object sender, EventArgs e)
{
    cancellationTokenSource.Cancel();
}
// ...
async public void ExportReportToMailWithOptionsAndAddressesAsync()
{
    // Create a simple report.
    XtraReport report = new XtraReport()
    {
        Name = "SimpleReport",
        Bands = {
            new DetailBand() {
                Controls = {
                    new XRLabel() {
                        Text = "Simple Report"
                    }
                }
            }
        }
    };
    // Configure an SMTP client.
    SmtpClient client = new SmtpClient("mail.smtpclient.test", 25)
    {
        Credentials = new System.Net.NetworkCredential("username", "password"),
        EnableSsl = true
    };
    // Export the report to MailMessage format.
    MailMessage message = await report.ExportToMailAsync(
        null,
        "sender@test.test",
        "recipient1@test.test, recipient2@test.test",
        "Subject",
        cancellationTokenSource.Token);
    // Send the exported report.
    client.Send(message);
}
See Also