0

我正在尝试使用 Outlook 应用程序将水晶报告的内容作为电子邮件正文发送。这是我在 VB.net 中的代码

Imports outlook = Microsoft.Office.Interop.Outlook
Dim a As String = something.ConnectionString
            Dim cryRpt As ReportDocument
            Dim username As String = a.Split("=")(3).Split(";")(0) 'get username
            Dim password As String = a.Split("=")(4).Split(";")(0) 'get password
            cryRpt = New ReportDocument()
            Dim Path As String = Application.StartupPath
            Dim svPath As String = Application.StartupPath & "\PDF"
            If Not Directory.Exists(svPath) Then
                Directory.CreateDirectory(svPath)
            End If
            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 also
            Dim 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()

    Try
                Dim 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 As New List(Of String)({txtCC1.Text, txtCC2.Text, txtCC3.Text})
                Dim cclis As String = String.Join(",", ccArray.Where(Function(ss) Not String.IsNullOrEmpty(ss)))
                oMsg.CC = cclis
                oMsg.Display(True)
            Catch ex As Exception
                MsgBox("Something went wrong", vbExclamation)
            End Try
            SvFormPanel3.Visible = False

功能

Private Function getFileAsString(ByVal file As String) As String
        Dim reader As System.IO.FileStream
        Try
            reader = New System.IO.FileStream(file, IO.FileMode.Open)
        Catch e As Exception
            MsgBox("Something went wrong. " + e.Message, vbInformation)
        End Try

        Dim resultString As String = ""
        Dim b(1024) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While reader.Read(b, 0, b.Length) > 0
            resultString = resultString & temp.GetString(b)
            Array.Clear(b, 0, b.Length)
        Loop

        reader.Close()
        Return resultString
    End Function

报告将以 html 格式导出到指定位置。当我们手动打开该 html 文件时,它会完美地显示边界线和所有内容。但是当它被添加为outlook应用程序的html主体时,格式就会消失,看起来很分散。谁能帮忙

4

2 回答 2

0

我通过将其导出为 PDF 然后转换为图像并嵌入到电子邮件正文中解决了这个问题。

于 2019-10-01T13:15:29.660 回答
0

你试过这个吗?

打开Outlook,转到,文件>选项>邮件

转到 MessageFormat 部分并取消选中“通过删除格式来减小消息大小...”

在此处输入图像描述

于 2019-09-30T12:07:52.217 回答