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:
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.Threading.Tasks;
namespace SendReportAsEMail
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
}
private static MailMessage CreateMailMessage(MemoryStream stream)
{
// Instantiate a report.
// Email export options are already specified at design time.
XtraReport1 report = new XtraReport1();
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.
MailMessage mail = new MailMessage();
mail.Attachments.Add(attachedDoc);
// Specify the sender and retrieve the 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.";
return mail;
}
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();
NetworkCredential nameAndPassword = new NetworkCredential(SmtpUserName, SmtpUserPassword);
lblProgress.Text = "Sending mail...";
lblProgress.Text = await SendAsync(SmtpHost, SmtpPort, nameAndPassword);
}
private static async Task<string> SendAsync(string SmtpHost, int SmtpPort, NetworkCredential nameAndPassword)
{
string result = "OK";
// Create a new memory stream and export the report in PDF.
using (MemoryStream stream = new MemoryStream())
{
using (MailMessage mail = CreateMailMessage(stream))
{
using (SmtpClient smtpClient = new SmtpClient(SmtpHost, SmtpPort))
{
smtpClient.Credentials = nameAndPassword;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
try
{
await smtpClient.SendMailAsync(mail);
}
catch (Exception ex)
{
result = ex.Message;
}
}
}
}
return result;
}
}
}
Instead of the System.Net.Mail.SmtpClient object you can use the MailKit library.
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.
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.
Do the following:
- Server Side
- Create a DocumentOperationService class descendant and override its CanPerformOperation) and PerformOperation methods.
- Implement the PerformOperation method that exports a report to PDF, composes the mail message, and uses the SmtpClient instance to send a message.
- Register an instance of a custom DocumentOperationService class as a service.
- Client Side
- Call the PerformCustomDocumentOperation method to pass email address to the DocumentOperationService on the server.
See Also