The ASP.NET Capsule #14: How to automatically send a DevExpress XtraReport via Email

The following example demonstrates how to automatically send a report via e-mail. To do this, a report should first be exported into one of the available formats. In this example, a report is exported to PDF, since this format provides the best output quality (the PDF result is as close to a report's print result as possible).

You can find the full sample (with C# and VB.NET code) on: http://www.devexpress.com/Support/Center/e/E16.aspx

The following source code assumes you have a button with a click event handler named “Button1_Click”. The XtraReport class is named XtraReport1.

   1:  using System;
   2:  using System.IO;
   3:  using System.Net.Mail;
   4:  // ...
   5:   
   6:  using System.Windows.Forms;
   7:   
   8:  namespace SendReportAsEMailCS
   9:  {
  10:      public partial class Form1 : Form
  11:      {
  12:          public Form1()
  13:          {
  14:              InitializeComponent();
  15:          }
  16:   
  17:          private void button1_Click(object sender, EventArgs e)
  18:          {
  19:              try
  20:              {
  21:                  // Create a new report.                
  22:                  XtraReport1 report = new XtraReport1();
  23:   
  24:                  // Create a new memory stream and export the report into 
  25:                  // it as PDF.
  26:                  MemoryStream mem = new MemoryStream();
  27:                  report.ExportToPdf(mem);
  28:   
  29:                  // Create a new attachment and put the PDF report into 
  30:                  // it.
  31:                  mem.Seek(0, System.IO.SeekOrigin.Begin);
  32:                  Attachment att = new Attachment(mem, "TestReport.pdf", 
  33:                                                  "application/pdf");
  34:   
  35:                  // Create a new message and attach the PDF report to 
  36:                  // it.
  37:                  MailMessage mail = new MailMessage();
  38:                  mail.Attachments.Add(att);
  39:   
  40:                  // Specify sender and recipient options for the e-mail 
  41:                  // message.
  42:                  mail.From = new MailAddress("someone@somewhere.com", 
  43:                                              "Someone");
  44:                  mail.To.Add(new 
  45:                      MailAddress(report.ExportOptions.Email.RecipientAddress,
  46:                      report.ExportOptions.Email.RecipientName));
  47:   
  48:                  // Specify other e-mail options.
  49:                  mail.Subject = report.ExportOptions.Email.Subject;
  50:                  mail.Body = "This is a test e-mail message sent by an 
  51:                               application.";
  52:   
  53:                  // Send the e-mail message via the specified SMTP server.
  54:                  SmtpClient smtp = new SmtpClient("smtp.somewhere.com");
  55:                  smtp.Send(mail);
  56:   
  57:                  // Close the memory stream.
  58:                  mem.Close();
  59:                  mem.Flush();
  60:              }
  61:              catch (Exception ex)
  62:              {
  63:                  MessageBox.Show(this, "Error sending a report.\n" +
  64:                                         ex.ToString());
  65:              }
  66:          }
  67:      }
  68:  }

1 Comment

  • Thanks Jose for making it a no-brainer for others to use this functionality!

    I should have found it on the dx support center, i suppose, but did a google search instead.

    Kind regards,
    Willem

Comments have been disabled for this content.