Skip to Content

Ever since the first version of Crystal Reports for .NET (CR 9.1), the ability to export to destination MAPI from the DHTML viewer (web) has been eliminated. I regularly come across cases where MAPI destination is required. To export to MAPI through a Web based application, you need to export the report to disk and then mail the exported report as an attachment. Both VB and C# code is shown. I would like to acknowledge an ex coworker, Dylan Lopez as the creator of the code.

Note that export to destination MAPI in a windows application is possible using the CR SDK for .NET and code for this is included at the end of this blog. Many thanks to to Don Williams and Dan Paulsen for testing the code and for sharing the code with me.

Here is the VB .NET code that will achieve MAPI destination export in a web application (C# code is further down). Note that in a web application the issues stems from IIS or the web server as this does not have access to Outlook. Thus this blog as work around to that issue.

 

    ‘Add the following namespaces 
   Imports CrystalDecisions.CrystalReports.Engine
   Imports CrystalDecisions.Shared
   Imports System.IO

   ‘ Code for exporting the report to PDF and attaching the exported file to an email. 
   Dim crExportOptions As ExportOptions
   Dim crDiskFileDestinationOptions As
   DiskFileDestinationOptions
   Dim ExportPath As String

   ‘ Create an instance of the Report
   Dim oRpt As New NameOfYourReport()

   ‘ Or open a report file
   Dim oRpt1 As New ReportDocument()
   oRpt1.Load(Request.PhysicalApplicationPath +
   “YourReport.rpt”, OpenReportMethod.OpenReportByTempCopy)

   ‘ Create the directory path if it does not already exist

   ExportPath = Request.PhysicalApplicationPath + “Exported”
   If Directory.Exists(ExportPath) = False Then
      Directory.CreateDirectory(Request.PhysicalApplicationPath + “Exported”)
   End If
   Dim fname As String
   fname = ExportPath + “Portabledoc.pdf”

   ‘ Set the path for the exported report
   crDiskFileDestinationOptions = New
   DiskFileDestinationOptions()
   crDiskFileDestinationOptions.DiskFileName = fname

   ‘ Set the options to export the report to PDF format
   crExportOptions = oRpt.ExportOptions
   With crExportOptions
      .DestinationOptions = crDiskFileDestinationOptions
      .ExportDestinationType = ExportDestinationType.DiskFile
      .ExportFormatType = ExportFormatType.PortableDocFormat
   End With

   ‘ Export the report
   oRpt.Export()
   Response.Write(“report exported to: ” & fname)

   ‘ Create a new mail attachment and add the exported report
   Dim att As New Mail.MailAttachment((fname))
   Dim oMsg As New System.Web.Mail.MailMessage()

   oMsg.Attachments.Add(att)

   ‘ Set the To, From, And subject for the email
   oMsg.To = “someonesemail@email.com
   oMsg.From = “myemail@email.com
   oMsg.Subject = “You have been sent a Crystal Report”

   ‘ Connect to the server and send the message
   ‘ For example this is the Hotmail smtp Server(“”)
   Mail.SmtpMail.SmtpServer = “lyris01.hosting.innerhost.com”
   System.Web.Mail.SmtpMail.Send(oMsg)
   Response.Write(“
“)
   Response.Write(“Message sent to: ” & oMsg.To.ToString)

   ‘ Delete the exported report file
   File.Delete(fname)

 

 

C# code looks like this:

 

    // Add the following namespaces
   using CrystalDecisions.CrystalReports.Engine;
   using CrystalDecisions.Shared;
   using System.IO;

   // Code for exporting the report to PDF and attaching the exported file to an email
   ExportOptions crExportOptions;
   DiskFileDestinationOptions;
   crDiskFileDestinationOptions;
   String ExportPath;

   // Create an instance of the Report
   NameOfYourReport oRpt = new NameOfYourReport();

   // Or open a report file
   ReportDocument oRpt1 = new ReportDocument();
   oRpt1.Load(Request.PhysicalApplicationPath + “file://YourReport.rpt/“,
   OpenReportMethod.OpenReportByTempCopy);

   // Create the directory path if it does not already exist
   ExportPath = Request.PhysicalApplicationPath + “Exported”;
   if (Directory.Exists(ExportPath) == false)
   {
      Directory.CreateDirectory(Request.PhysicalApplicat ionPath + “Exported”);
   }
   String fname;
   fname = ExportPath + “Portabledoc.pdf”;

   // Set the path for the exported report
   crDiskFileDestinationOptions = new DiskFileDestinationOptions();
   crDiskFileDestinationOptions.DiskFileName = fname;
   // Set the options to export the report to PDF format
   crExportOptions = oRpt.ExportOptions;
   crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
   crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
   crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;

   // Export the report
   oRpt.Export();
   Response.Write(“report exported to: ” + fname);

   // Create a new mail attachment and add the exported report
   System.Web.Mail.MailAttachment att = new
   System.Web.Mail.MailAttachment((fname));
   System.Web.Mail.MailMessage oMsg = new System.Web.Mail.MailMessage();
   oMsg.Attachments.Add(att);

   // Set the To, From, And subject for the email
   oMsg.To = “someonesemail@email.com“;
   oMsg.From = “myemail@email.com “; 
   oMsg.Subject = “You have been sent a Crystal Report”;

   // Connect to the server and send the message
   // For example this is the Hotmail smtp Server(“”)
   System.Web.Mail.SmtpMail.SmtpServer = “lyris01.hosting.innerhost.com”;
   System.Web.Mail.SmtpMail.Send(oMsg);
   Response.Write(“
“);
   Response.Write(“Message sent to: ” + oMsg.To.ToString());

   // Delete the exported report file
   File.Delete(fname);

 

VB code for a Windows application:

   Dim repdoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
        Dim diskOpts As New CrystalDecisions.Shared.DiskFileDestinationOptions()
        Dim ExpOpts As CrystalDecisions.Shared.ExportOptions
        Dim htmlopts As New CrystalDecisions.Shared.HTMLFormatOptions()
        Dim MailOpts As New CrystalDecisions.Shared.MicrosoftMailDestinationOptions()
        repdoc = reportDocument1
        repdoc.Load(“c:world sales report.rpt”)

        ExpOpts = repdoc.ExportOptions

        With ExpOpts
            .ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.MicrosoftMail
            .ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.RichText

        End With

        With MailOpts
           
     .MailToList = “Don Williams”
            .MailSubject = “Attached is a PDF file – .net Export test “
            .MailSubject = “This is the Subject”
            .MailCCList = “John Doe”
            .UserName = “intljdoe
            .Password = “pookie”

        End With

        ExpOpts.DestinationOptions = MailOpts

        Try
            repdoc.Export()
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try

 

C# code for a Windows application:

CrystalDecisions.CrystalReports.Engine.ReportDocument repdoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
            CrystalDecisions.Shared.ExportOptions ExpOpts = new CrystalDecisions.Shared.ExportOptions();
            CrystalDecisions.Shared.HTMLFormatOptions htmlopts = new CrystalDecisions.Shared.HTMLFormatOptions();
            CrystalDecisions.Shared.MicrosoftMailDestinationOptions MailOpts = new CrystalDecisions.Shared.MicrosoftMailDestinationOptions();

            //repdoc = rptClientDoc;
            repdoc.Load(“c:
eport2.rpt”);

            ExpOpts = repdoc.ExportOptions;

            ExpOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail;
            ExpOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;
           
         MailOpts.MailToList = “Don Williams”;
            MailOpts.MailSubject = “Attached is a PDF file – .net Export test “;
            MailOpts.MailSubject = “This is the Subject”;
            //MailOpts.MailCCList = “John Doe”;
            MailOpts.UserName = “intldwilliams”;
            MailOpts.Password = “yourpassword”;

            ExpOpts.DestinationOptions = MailOpts;

            repdoc.Export();
 

 

Other resources:

http://vb.net-informations.com/crystal-report/vb.net_crystal_report_email.htm

http://www.sdn.sap.com/irj/sdn/wiki?path=/display/bobj/available+export+formats%2c+crystal+reports+9.1+to+crystal+reports+2008

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply