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:
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;
}
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).
- Convert the MailMessage object to a MimeMessage object.
- Use the SmtpClient.SendAsync method to send the 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.
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.