This example demonstrates how to email a report with the MailKit email client library. Run the application, enter the SMTP host name, port, smtp credentials if needed, and click Send to email a document to the address you specified earlier in the report export options.
To specify the report export options for email, use the report's ExportOptions.Email property.
Before you click Send, you can choose from two mail options:
- Send a report in HTML format in the email message body and in the attached PDF file. The ExportToMail method creates HTML part of MailMessage object, the ExportToPdf method creates a PDF stream attached to the MailMessage instance. After converting the message into a MimeMessage object, the MailKit's SmtpClient object calls the SendAsync method to send the message.
- Send a report as PDF attachment. The ExportToPdf method creates a PDF stream that the BoduyBuilder class uses to create an attachment for the MimeMessage object. The MailKit's SmtpClient object calls the SendAsync method to send the message.
The following image shows an email in which the report is included both in the HTML body and as a PDF attachment.
Files to Review
Documentation
More Examples
- Reporting for ASP.NET MVC - How to Email a Report from the Document Viewer
- Reporting for Blazor - Email a Report from the Native Blazor Report Viewer
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using System;
using System.IO;
using System.Threading.Tasks;
namespace SendReportWithMailKit {
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
this.edtHost.EditValue = "localhost";
this.edtPort.EditValue = 25;
}
private MimeMessage CreateMimeMessage(MemoryStream stream) {
if ((bool)this.radioGroup1.EditValue)
return CreateMimeMessageExportToMail(stream);
else
return CreateMimeMessageExportToPdf(stream);
}
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 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;
}
}
}