Author: Admin 08/11/2021
Language:
C#
Tags:
How to dynamically send a DevExpress report via e-mail as a PDF
This example demonstrates how to send a report by e-mail.
Perform these steps:
See also:
Form1.cs
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
namespace SendReportAsEMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
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());
}
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace SendReportAsEMail
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
XtraReport1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
namespace SendReportAsEMail
{
public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
{
public XtraReport1()
{
InitializeComponent();
}
}
}
How to dynamically send a report via e-mail as a PDF | DevExpress Support