Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

Email Crystal Reports from C# Application



After an export the Crystal Reports as a PDF file format in your disk, the next step is to email that .pdf file. For that here we are using System.Web.Mail of C#. We have to provide the necessary information to configuring SmtpMail client and send the exported file as an attachment .

Select the default form (Form1.cs) you created in C# and drag two buttons (Button1, Button2 ) and a CrystalReportViewer control to your form.

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Web.Mail;

Copy and paste the following source code and run your C# project.

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Web.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        ReportDocument cryRpt;
        string pdfFile = "c:\\getsuyodev.pdf";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cryRpt = new ReportDocument();
            cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh(); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                ExportOptions CrExportOptions ;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = pdfFile;
                CrExportOptions = cryRpt.ExportOptions;
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;
                cryRpt.Export();

                sendmail();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void sendmail()
        {
            try {
                SmtpMail.SmtpServer.Insert(0, "your hostname");
                MailMessage Msg = new MailMessage();
                Msg.To = "to address here";
                Msg.From = "from address here";
                Msg.Subject = "Crystal Report Attachment ";
                Msg.Body = "Crystal Report Attachment ";
                Msg.Attachments.Add(new MailAttachment(pdfFile));
                System.Web.Mail.SmtpMail.Send(Msg);
            } 
            catch (Exception ex) { 
                MessageBox.Show (ex.ToString()); 
            }         
        }
    }
}

cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt”);
The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt. So give the full pathname of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt

Before you run this program, you have to provide the necessary SMTP information, that is your HOSTNAME, FROM ADDRESS and TO ADDRESS to the SMTP client.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More