Export Crystal Report to PDF and send in Email as Attachment in ASP.Net using C#

Last Reply on Apr 10, 2013 09:56 AM By Mudassar

Posted on Apr 10, 2013 08:52 AM

I have a process to send report to exchange server"outlook" i want to send it as pdf format. how can i do this process??

Posted on Apr 10, 2013 09:56 AM

I have made use of the following article to create a sample for you.

Crystal Report ASP.Net Example using DataSet or DataTable in C# VB.Net and Visual Studio 2010

What I simply did is added a button to the above code to send email and on Button click I am generating PDF and sending email

Namespaces

using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using System.Net.Mail;
using System.Net;
using CrystalDecisions.Shared;

Code

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReportDocument crystalReport = new ReportDocument();
            crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
            Customers dsCustomers = GetData("select * 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;
                }
            }
        }
    }
    protected void ExportAndEmail(object sender, EventArgs e)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
        Customers dsCustomers = GetData("select * from customers");
        crystalReport.SetDataSource(dsCustomers);
        using (MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com"))
        {
            mm.Subject = "Crystal Report PDF example";
            mm.Body = "Crystal Report PDF example";
            mm.Attachments.Add(new Attachment(crystalReport.ExportToStream(ExportFormatType.PortableDocFormat), "Report.pdf"));
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            NetworkCredential credential = new NetworkCredential();
            credential.UserName = "sender@gmail.com";
            credential.Password = "xxxxx";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = credential;
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Send(mm);
        }
    }