Skip to Content
Sep 30, 2019 at 10:20 AM Crystal report export to html and send as html mail body using outlook


I am trying to send contents of a crystal report as email body using outlook application. Here is my code in

Imports outlook = Microsoft.Office.Interop.Outlook
Dim a AsString= something.ConnectionString
            Dim cryRpt As ReportDocument
            Dim username AsString= a.Split("=")(3).Split(";")(0)'get usernameDim password AsString= a.Split("=")(4).Split(";")(0)'get password
            cryRpt =New ReportDocument()Dim Path AsString= Application.StartupPath
            Dim svPath AsString= Application.StartupPath &"\PDF"IfNot Directory.Exists(svPath)Then
            cryRpt.Load(Path &"\Reports\dr.rpt")
            CrystalReportViewer1.ReportSource = cryRpt
            cryRpt.SetDatabaseLogon(username, password)
            CrystalReportViewer1.Refresh()Dim myExportOptions As ExportOptions
            myExportOptions = cryRpt.ExportOptions
            myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
            myExportOptions.ExportFormatType = ExportFormatType.HTML40 'i tried HTML32 alsoDim html40FormatOptions As HTMLFormatOptions =New HTMLFormatOptions()
            html40FormatOptions.HTMLBaseFolderName = svPath
            html40FormatOptions.HTMLFileName ="dr.htm"
            html40FormatOptions.HTMLEnableSeparatedPages =False
            html40FormatOptions.HTMLHasPageNavigator =False
            html40FormatOptions.UsePageRange =False
            myExportOptions.FormatOptions = html40FormatOptions
            cryRpt.Export()TryDim oApp As outlook.Application
                oApp =New outlook.Application

                Dim oMsg As outlook.MailItem
                oMsg = oApp.CreateItem(outlook.OlItemType.olMailItem)

                oMsg.Subject = txtSubject.Text
                oMsg.BodyFormat = outlook.OlBodyFormat.olFormatHTML
                 oMsg.HTMLBody =""

                oMsg.HTMLBody = getFileAsString(svPath &"\PoPrt\QuotPrt.html")

                oMsg.To= txtEmailId.Text
                Dim ccArray AsNew List(Of String)({txtCC1.Text, txtCC2.Text, txtCC3.Text})Dim cclis AsString=String.Join(",", ccArray.Where(Function(ss)NotString.IsNullOrEmpty(ss)))
                oMsg.CC = cclis
                oMsg.Display(True)Catch ex As Exception
                MsgBox("Something went wrong", vbExclamation)EndTry
            SvFormPanel3.Visible =False

the function

PrivateFunction getFileAsString(ByVal file AsString)AsStringDim reader As System.IO.FileStream
            reader =New System.IO.FileStream(file, IO.FileMode.Open)Catch e As Exception
            MsgBox("Something went wrong. "+ e.Message, vbInformation)EndTryDim resultString AsString=""Dim b(1024)AsByteDim temp As UTF8Encoding =New UTF8Encoding(True)DoWhile reader.Read(b,0, b.Length)>0
            resultString = resultString & temp.GetString(b)
            Array.Clear(b,0, b.Length)Loop

        reader.Close()Return resultString

The report will get exported to the specified location as html. And when we manually open that html file it displays perfectly with border lines and all. But when its getting added as html body of outlook application, the formatting will be gone, and looks scattered. can anyone help