Skip to main content

Email Reports

  • 6 minutes to read

This document describes how to send an exported report by e-mail.

Export to PDF and Send Email in Code

You can export a report to PDF and attach the PDF document to the email message.

For each report class you can specify different email export options in the Visual Studio Designer:

Report Email Options

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

The following code exports a report to PDF, attaches the PDF document to a message, and sends the message using email options specified in the Visual Studio Report Designer. The code uses the MailKit library.

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

    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Someone", "someone@test.com"));
    message.To.Add(new MailboxAddress(report.ExportOptions.Email.RecipientName,
        report.ExportOptions.Email.RecipientAddress));
    message.Subject = report.ExportOptions.Email.Subject;
    var builder = new BodyBuilder();
    builder.TextBody = "This is a test e-mail message sent by an application.";
    // Create a new attachment and add the PDF document.
    report.ExportToPdf(stream);
    stream.Seek(0, System.IO.SeekOrigin.Begin);
    builder.Attachments.Add("TestReport.pdf", stream.ToArray(), new ContentType("application","pdf"));
    message.Body = builder.ToMessageBody();
    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;
}

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

Export to HTML Email Body

Do the following to export a report to an email message:

[!example[Reporting for WinForms - How to Use MailKit to Email a Report](https://github.com/DevExpress-Examples/reporting-winforms-mailkit-email-report-pdf]

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;
}

Email a Report in a Web Application

You can send a request to the server to export a report to PDF and email the resulting PDF document.

View Example: Reporting for Web - How to Email a Report from the Document Viewer

Do the following:

Server Side
Client Side
  • Call the PerformCustomDocumentOperation method to pass email address to the DocumentOperationService on the server.
See Also