In this article I will explain with an example, how to export Crystal Report to PDF and send as Email Attachment in ASP.Net using C# and VB.Net.
Crystal Report provides function named ExportToStream which exports Crystal Report to Word, Excel, PDF or CSV file in Stream format.
Later the Stream can be used to attach the exported PDF file as Attachment and send as Email.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
HTML Markup
The HTML Markup consists of a CrystalReportViewer control and a Button to export Crystal Report to PDF and send as Email Attachment.
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
    Height="400" Width="600" BestFitPage="False" ToolPanelView="None" />
<br />
<asp:Button ID="btnSend" Text="Send Email" runat="server" OnClick="SendEmail" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Data;
using System.Net.Mail;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
 
VB.Net
Imports System.Net
Imports System.Data
Imports System.Net.Mail
Imports System.Configuration
Imports System.Data.SqlClient
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
 
 
Designing and populating the Crystal Report from Database
Inside the Page Load event, the Crystal Report is populated from database.
Note: For more details about designing and populating Crystal Report, please refer the following article.
         Crystal Report ASP.Net Example using DataSet or DataTable in C# VB.Net and Visual Studio 2010
 
C#
ReportDocument crystalReport;
protected void Page_Load(object sender, EventArgs e)
{
    crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
    Customers dsCustomers = this.GetData("SELECT TOP 5 * FROM customers");
    crystalReport.SetDataSource(dsCustomers);
    CrystalReportViewer1.ReportSource = crystalReport;
}
 
private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
 
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}
 
VB.Net
Private crystalReport As ReportDocument
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    crystalReport = New ReportDocument()
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
    Dim dsCustomers As Customers = Me.GetData("SELECT TOP 5 * FROM customers")
    crystalReport.SetDataSource(dsCustomers)
    CrystalReportViewer1.ReportSource = crystalReport
End Sub
 
Private Function GetData(query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsCustomers As New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function
 
 
Exporting Crystal Report to PDF and sending as Email Attachment in ASP.Net
When the Send Email Button is clicked, first the Crystal Report is populated from database in the Page Load event and is exported PDF file in Stream format using the ExportToStream method.
Note: The Crystal Report can also be exported to other formats and send as Email Attachment, for more details, please refer Export Crystal Report on Button Click to Word Excel PDF and CSV in ASP.Net.
 
The exported PDF file is attached to the Email as Attachment and finally the Email is sent.
Note: This Article uses GMAIL server for sending email, for more details refer Send email using Gmail SMTP Mail Server in ASP.Net.
 
C#
protected void SendEmail(object sender, EventArgs e)
{
    using (MailMessage mm = new MailMessage("sender@gmail.com", "recipient@gmail.com"))
    {
        mm.Subject = "Crystal Report PDF";
        mm.Body = "Attachment: Customer's Crystal Report PDF";
        mm.Attachments.Add(new Attachment(crystalReport.ExportToStream(ExportFormatType.PortableDocFormat), "Crystal.pdf"));
        mm.IsBodyHtml = true;
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.Host = "smtp.gmail.com";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new NetworkCredential
            {
                UserName = "sender@gmail.com",
                Password = "xxxx"
            };
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Send(mm);
       }
    }
}
 
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    Using mm As MailMessage = New MailMessage("sender@gmail.com", "recipient@gmail.com")
        mm.Subject = "Crystal Report PDF"
        mm.Body = "Attachment: Customer's Crystal Report PDF"
        mm.Attachments.Add(New Attachment(crystalReport.ExportToStream(ExportFormatType.PortableDocFormat), "Crystal.pdf"))
        mm.IsBodyHtml = True
 
        Using smtp As SmtpClient = New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.UseDefaultCredentials = True
            smtp.Credentials = New NetworkCredential With {
                .UserName = "sender@gmail.com",
                .Password = "xxxx"
            }
            smtp.Port = 587
           smtp.EnableSsl = True
            smtp.Send(mm)
        End Using
    End Using
End Sub
 
 
Screenshots
The Crystal Report
Export Crystal Report to PDF and send as Email Attachment in ASP.Net
 
Email with Crystal Report PDF file as Attachment
Export Crystal Report to PDF and send as Email Attachment in ASP.Net
 
Crystal Report exported to PDF file
Export Crystal Report to PDF and send as Email Attachment in ASP.Net
 
 
Downloads