Skip to main content
A newer version of this page is available. .

Email Reports

  • 5 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:

View Example: How to Email a Report as a Document in PDF

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.

using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
// ...
    try
    {
        // Instantiate a report. 
        // Email export options are already specified at design time.                
        XtraReport1 report = new XtraReport1();

        // Create a new memory stream and export the report in PDF.
        MemoryStream stream = new MemoryStream();
        report.ExportToPdf(stream);

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

        // Create a new message and attach the PDF document.
        MailMessage mail = new MailMessage();
        mail.Attachments.Add(attachedDoc);

        // Specify the sender and get recipient settings from the report export options.
        mail.From = new MailAddress("someone@somewhere.com", "Someone");
        mail.To.Add(new MailAddress(report.ExportOptions.Email.RecipientAddress,
            report.ExportOptions.Email.RecipientName));

        // Specify other e-mail options.
        mail.Subject = report.ExportOptions.Email.Subject;
        mail.Body = "This is a test e-mail message sent by an application.";

        // Specify the SMTP server and send the message.
        string SmtpHost = null;
        int SmtpPort = -1;
        if (string.IsNullOrEmpty(SmtpHost) || SmtpPort == -1)
        {
            throw new ArgumentException("Please configure the SMTP server settings.");
        }

        string SmtpUserName = "Enter_Sender_User_Account";
        string SmtpUserPassword = "Enter_Sender_Password";
        SmtpClient smtpClient = new SmtpClient(SmtpHost, SmtpPort);
        smtpClient.Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = false;
        smtpClient.Send(mail);

        // Close the memory stream.
        stream.Close();
        stream.Flush();
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, "Error sending a report.\n" + ex.ToString());
    }

Export to HTML Email Body

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

  • Use the report’s ExportOptions.Email property to specify options for report export.
  • Invoke an ExportToMail overloaded method report to convert a report to a MailMessage object (an HTML representation of the report).
  • Initialize an SmtpClient and call its Send or SendAsync method to send the message.

View Example: How to send a report as HTML in an email body

using System.Net;
using System.Net.Mail;
using DevExpress.XtraReports.UI;
// ...
    XtraReport report = new XtraReport1();

    string SmtpHost = "Enter_SMTP_Server_Name";
    int SmtpPort = 25;
    string SmtpUserName = "Enter_Sender_User_Name";
    string SmtpUserPassword = "Enter_Sender_Password";

    using (var smtpClient = new SmtpClient(SmtpHost, SmtpPort))
    {
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = false;

        if (!string.IsNullOrEmpty(SmtpUserName))
        {
            smtpClient.Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
        }
        using(MailMessage message = report.ExportToMail("someone@example.com",
                "recipient1@example.com, recipient2@example.com, recipient3@example.com", "Enter_Message_Subject")) {
            smtpClient.Send(message);
        }
    } 

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: How to send a report via Email from the client side

Do the following:

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