xtrareports-17634-feature-guide-to-devexpress-reports-store-and-distribute-reports-export-reports-email-reports.md
This document contains examples of how to email a report in code, from the Web Document Viewer and Blazor Report Viewer.
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: Reporting for WinForms - 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. This 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", "[email protected]"));
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;
}
Imports MailKit.Net.Smtp
Imports MailKit.Security
Imports MimeKit
Imports System
Imports System.IO
Imports System.Threading.Tasks
' ...
Private Shared Function CreateMimeMessageExportToPdf(ByVal stream As MemoryStream) As MimeMessage
' Instantiate a report.
' Email export options are already specified at design time.
Dim report As New XtraReport1()
Dim message = New MimeMessage()
message.From.Add(New MailboxAddress("Someone", "[email protected]"))
message.To.Add(New MailboxAddress(report.ExportOptions.Email.RecipientName, report.ExportOptions.Email.RecipientAddress))
message.Subject = report.ExportOptions.Email.Subject
Dim 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
End Function
' ...
Private Async Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
Dim SmtpHost As String = Me.edtHost.EditValue.ToString()
Dim SmtpPort As Integer = Integer.Parse(Me.edtPort.EditValue.ToString())
Dim SmtpUserName As String = Me.edtUsername.EditValue.ToString()
Dim SmtpUserPassword As String = Me.edtPassword.EditValue.ToString()
Me.lblProgress.Text = "Sending mail..."
Me.lblProgress.Text = Await SendAsync(SmtpHost, SmtpPort, SmtpUserName, SmtpUserPassword)
End Sub
Private Async Function SendAsync(ByVal smtpHost As String, ByVal smtpPort As Integer, ByVal userName As String, ByVal password As String) As Task(Of String)
Dim result = "OK"
' Create a new memory stream and export the report in PDF.
Using stream As MemoryStream = New MemoryStream()
Using mail = CreateMimeMessage(stream)
Using client = New SmtpClient()
Try
client.Connect(smtpHost, smtpPort, SecureSocketOptions.Auto)
client.Authenticate(userName, password)
Await client.SendAsync(mail)
Catch ex As Exception
result = ex.Message
End Try
client.Disconnect(True)
End Using
End Using
End Using
Return result
End Function
Export a report as an email and use the MailKit library to send it.
To export a report to an email message, do the following:
MimeMessage object.SmtpClient.SendAsync method to send the message.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("[email protected]",
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;
}
Imports MailKit.Net.Smtp
Imports MailKit.Security
Imports MimeKit
Imports System
Imports System.IO
Imports System.Threading.Tasks
' ...
Private Shared Function CreateMimeMessageExportToMail(ByVal stream As MemoryStream) As MimeMessage
' Instantiate a report.
' Email export options are already specified at design time.
Dim report As New XtraReport1()
Dim mMessage As System.Net.Mail.MailMessage = report.ExportToMail("[email protected]", 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)
Dim attachedDoc As New System.Net.Mail.Attachment(stream, "TestReport.pdf", "application/pdf")
mMessage.Attachments.Add(attachedDoc)
Dim message = CType(mMessage, MimeMessage)
Return message
End Function
Private Async Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
Dim SmtpHost As String = Me.edtHost.EditValue.ToString()
Dim SmtpPort As Integer = Integer.Parse(Me.edtPort.EditValue.ToString())
Dim SmtpUserName As String = Me.edtUsername.EditValue.ToString()
Dim SmtpUserPassword As String = Me.edtPassword.EditValue.ToString()
Me.lblProgress.Text = "Sending mail..."
Me.lblProgress.Text = Await SendAsync(SmtpHost, SmtpPort, SmtpUserName, SmtpUserPassword)
End Sub
Private Async Function SendAsync(ByVal smtpHost As String, ByVal smtpPort As Integer, ByVal userName As String, ByVal password As String) As Task(Of String)
Dim result = "OK"
' Create a new memory stream and export the report in PDF.
Using stream As MemoryStream = New MemoryStream()
Using mail = CreateMimeMessage(stream)
Using client = New SmtpClient()
Try
client.Connect(smtpHost, smtpPort, SecureSocketOptions.Auto)
client.Authenticate(userName, password)
Await client.SendAsync(mail)
Catch ex As Exception
result = ex.Message
End Try
client.Disconnect(True)
End Using
End Using
End Using
Return result
End Function
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 - Email a Report from the Document Viewer
Do the following on the client and server sides:
Create a DocumentOperationService class descendant and override 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:
PerformCustomDocumentOperation method to pass an email address to the DocumentOperationService on the server.Use the Mailkit library to send an email from the native Blazor Report Viewer. You can find a complete code sample here: Email a Report from the Native Blazor Report Viewer.
The example adds a Send Email button to the Viewer’s toolbar. The button opens the Send Email dialog (DxPopup). You can specify the recipient list, subject, attachment, and body within the dialog. Click the Send button to send the report with the specified settings.
View Example: Reporting for Blazor - Email a Report from the Native Blazor Report Viewer