Email a Crystal Reports from VB.NET

To send a Crystal Reports document via email, the initial step involves exporting the Crystal Reports to one of the available file formats within Crystal Reports. Once the export is complete, the exported file can then be attached and sent via email.

It is worth noting that all the programming samples provided in these tutorials for Crystal Reports are based on the crystaldb database. Prior to commencing this tutorial, I recommend familiarizing yourself with the structure of the database. To access the database structure, kindly click on the following link: Click here to view the Database Structure.

In this tutorial, we will be utilizing the "Export Crystal Report to PDF file" tutorial as a foundation. Therefore, it is advisable to review the tutorial on exporting Crystal Reports to PDF before proceeding with this section.

After successfully Export Crystal Report to PDF file, the subsequent step involves sending the exported file via email. To accomplish this, we will be utilizing the System.Web.Mail framework. It is imperative to provide the necessary configuration details for the SmtpMail client and attach the exported file as an attachment to the email.

Select the default form (Form1.vb) you created in VB.NET and drag two buttons (Button1, Button2 ) and CrystalReportViewer control to your form.

Select Form's source code view and import the following :

  1. Imports CrystalDecisions.CrystalReports.Engine
  2. Imports CrystalDecisions.Shared
  3. Imports System.Web.Mail

Put the following source code in the button click events

Full Source VB.NET
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Imports System.Web.Mail Public Class Form1 Dim cryRpt As New ReportDocument Dim pdfFile As String = "c:\ProductReport1.pdf" Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") CrystalReportViewer1.ReportSource = cryRpt CrystalReportViewer1.Refresh() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Try Dim CrExportOptions As ExportOptions Dim CrDiskFileDestinationOptions As New _ DiskFileDestinationOptions() Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions CrDiskFileDestinationOptions.DiskFileName = pdfFile CrExportOptions = cryRpt.ExportOptions With CrExportOptions .ExportDestinationType = ExportDestinationType.DiskFile .ExportFormatType = ExportFormatType.PortableDocFormat .DestinationOptions = CrDiskFileDestinationOptions .FormatOptions = CrFormatTypeOptions End With cryRpt.Export() Catch ex As Exception MsgBox(ex.ToString) End Try sendMail() End Sub Private Sub sendMail() Try Dim Smtp As SmtpMail SmtpMail.SmtpServer.Insert(0, "your hostname") Dim Msg As MailMessage = 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)) Smtp.Send(Msg) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class
NOTES:

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here.

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