Back to Devexpress

XtraReport.ExportToMailAsync(MailMessageExportOptions, CancellationToken) Method

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xtrareport-dot-exporttomailasync-x28-devexpress-dot-xtraprinting-dot-mailmessageexportoptions-system-dot-threading-dot-cancellationtoken-x29.md

latest5.7 KB
Original Source

XtraReport.ExportToMailAsync(MailMessageExportOptions, CancellationToken) Method

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

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public Task<AlternateView> ExportToMailAsync(
    MailMessageExportOptions options = null,
    CancellationToken token = default(CancellationToken)
)
vb
Public Function ExportToMailAsync(
    options As MailMessageExportOptions = Nothing,
    token As CancellationToken = Nothing
) As Task(Of AlternateView)

Optional Parameters

NameTypeDefaultDescription
optionsMailMessageExportOptionsnull

The mail message export options.

| | token | CancellationToken | null |

A cancellation token that the task observes.

|

Returns

TypeDescription
Task<AlternateView>

A task that exports the report.

|

Remarks

This method is equivalent to the ExportToMail(MailMessageExportOptions) 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.

csharp
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 ExportReportToMailWithOptionsAsync()
{
    // Create a simple report.
    XtraReport report = new XtraReport()
    {
        Name = "SimpleReport",
        Bands = {
            new DetailBand() {
                Controls = {
                    new XRLabel() {
                        Text = "Simple Report"
                    }
                }
            }
        }
    };
    // Export the report as AlternateView to include in a mail message.
    AlternateView altView = await report.ExportToMailAsync(null, cancellationTokenSource.Token);

}
vb
Imports DevExpress.XtraReports.UI
Imports System.Threading
' ...
Imports System.Net.Mail
' ...
' Use cancellationTokenSource to allow users to stop the document creation process.
Private cancellationTokenSource As New CancellationTokenSource()
' ...
' The cancelButton_Click event handler uses cancellationTokenSource to stop the document creation process.
Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    cancellationTokenSource.Cancel()
End Sub
' ...
Public Async Sub ExportReportToMailWithOptionsAsync()
    ' Create a simple report.
    Dim report = New XtraReport() With {.Name = "SimpleReport"}
    Dim band = New DetailBand()
    Dim control = New XRLabel() With {.Text = "Simple Report"}
    band.Controls.Add(control)
    report.Bands.Add(band)
    ' Export the report as AlternateView to include in a mail message.
    Dim altView As AlternateView = Await report.ExportToMailAsync(Nothing, cancellationTokenSource.Token)
End Sub

See Also

Export Reports

Email Reports

XtraReport Class

XtraReport Members

DevExpress.XtraReports.UI Namespace