Skip to main content

Export to MailMessage

  • 3 minutes to read

You can export a report as an email and use the MailKit library to send it.

To do this, call the XtraReport.ExportToMail method. This method returns an instance of the MailMessage class. The body of the resulting message contains a document in HTML format, optimized for email. Use the SmtpClient to send the email.

If you want to customize the email’s content and format, create an instance of the MailMessageExportOptions class and pass it as an argument to the XtraReport.ExportToMail(MailMessageExportOptions) method.

The following code creates an email with the report included in the message body in HTML format, and sends the email.

View Example: Reporting for WinForms - How to Use MailKit to Email a Report

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using System;
using System.IO;
using System.Threading.Tasks;
// ...
private static MimeMessage CreateMimeMessageExportToMail(MemoryStream stream) {
    // Instantiate a report. 
    // Email export options are already specified at design time.                
    XtraReport1 report = new XtraReport1();

    System.Net.Mail.MailMessage mMessage = report.ExportToMail("someone@test.com",
                report.ExportOptions.Email.RecipientAddress, report.ExportOptions.Email.RecipientName);
    mMessage.Subject = report.ExportOptions.Email.Subject;

    // Create a new attachment and add the PDF document.
    report.ExportToPdf(stream);
    stream.Seek(0, System.IO.SeekOrigin.Begin);
    System.Net.Mail.Attachment attachedDoc = new System.Net.Mail.Attachment(stream, "TestReport.pdf", "application/pdf");
    mMessage.Attachments.Add(attachedDoc);

    var message = (MimeMessage)mMessage;
    return message;
}

private async void btnSend_Click(object sender, EventArgs e)
{
    string SmtpHost = edtHost.EditValue.ToString();
    int SmtpPort = Int32.Parse(edtPort.EditValue.ToString());
    string SmtpUserName = edtUsername.EditValue.ToString();
    string SmtpUserPassword = edtPassword.EditValue.ToString();
    lblProgress.Text = "Sending mail...";
    lblProgress.Text = await SendAsync(SmtpHost, SmtpPort, SmtpUserName, SmtpUserPassword);
}

private async Task<string> SendAsync(string smtpHost, int smtpPort, string userName, string password)
{
    string result = "OK";
    // Create a new memory stream and export the report in PDF.
    using (MemoryStream stream = new MemoryStream())
    {
        using (MimeMessage mail = CreateMimeMessage(stream))
        {
            using (var client = new SmtpClient())
            {
                try {
                    client.Connect(smtpHost, smtpPort, SecureSocketOptions.Auto);
                    //client.Authenticate(userName, password);
                    await client.SendAsync(mail);
                }
                catch (Exception ex) {
                    result = ex.Message;
                }
                client.Disconnect(true);
            }
        }
    }
    return result;
}

Note

Document navigation in a report is based on scripts. To use navigation based on HTML links, enable the UseHRefHyperlinks setting in the MailMessageExportOptions instance and pass this instance to the ExportToMail method as an argument.