下载PDF并将其附加到电子邮件中

时间:2016-05-10 22:17:31

标签: c# asp.net vb.net

这是我的问题,我生成了一个网页(不属于我现有项目的外部网页),它生成一个PDF文件,该文件保存在临时文件夹中的服务器上。将使用给定参数生成PDF。 我想保存网页生成的任何内容(PDF)并将其附加到电子邮件中,结果将是---->将PDF附加到电子邮件中。

Dim FullReportUrl As String = Nothing
            FullReportUrl = (String.Format("https://www.something.com/Reports/ReportManager.aspx?SessionID={0}&report=CaseFullReport&CompanyCode={1}&ExportType=pdf&ConnectionType=Main&GlobalVar=IncidentID^{2}|CompanyCode^{3}", "2c3fe87f-5c4e-4a7d-acda-970ed47f27eb", m_User.CompanyCode, m_IncidentCaseID.ToString(), m_User.CompanyCode))

            '# Create mail message
            Dim msg As New MailMessage
            msg.From = New MailAddress(Globals.gFromEmailAddr)
            msg.To.Add(New MailAddress("test@something.com"))
            msg.Subject = String.Concat("Full Report IR-", m_IncidentID.ToString())
            '# Attach the additional text to the E-Mail.
            If Not String.IsNullOrEmpty(rtbAdditionalText.Text.ToString()) Then
                msg.Body = "<html><body>Dear " + "Test" + ",  (Please Do Not Replay to this email)<br><br>" + rtbAdditionalText.Text.ToString().Trim() + "<br><br> Sincerely,<br> Something." + "<br><img id=""Logo"" alt=""logo"" src=""https://www.something.com/gs/Images/Layout/logo.png""/></body></html>"
            End If

            msg.IsBodyHtml = True
            msg.Priority = MailPriority.High


            '# Attach the full report to the email.

            Dim ms As MemoryStream = New MemoryStream
            Dim writer As StreamWriter = New StreamWriter(ms)
            With writer
                .Write(GetFileStream(FullReportUrl))
                .Flush()

                'ms.Position = 0 '== Reading the stream from the beginging and finializing it.
                Dim attach As Attachment = New Attachment(ms, "test.pdf", System.Net.Mime.MediaTypeNames.Application.Pdf)
                msg.Attachments.Add(attach)
            End With

          

            'Create client and send
            Dim client As New SmtpClient(Globals.gMailServer)
            client.Credentials = New System.Net.NetworkCredential("test@something.com", "555555$")

            Dim userState As Object = msg
            client.Send(msg)
            msg = Nothing
            client.Dispose()

PDF出现损坏。有人能指出原因吗?

2 个答案:

答案 0 :(得分:0)

尝试在

中获取PDF的数据
byte[]

StreamReader

通过

HttpWebRequest

使用生成PDF文件的URL ,然后将它更容易附加到电子邮件

答案 1 :(得分:0)

如果您对服务器有控制权,我不清楚。如果是这样,我会创建一个api,为您提供文件流结果。下面的伪代码(对不起c#)     public FileStreamResult Index(int id)

    {
        MemoryStream mystream = new MemoryStream(); 
        mystream = {Generate pdf code}
        return new FileStreamResult(mystream,"application/pdf");
    }

您的客户看起来像这样

public Sub GetPDFfromAPI(int id)
       dim myfile as byte() = Nothing
        dim handler as HttpClientHandler  = new HttpClientHandler()
        dim myclient as new HttpClient(handler)
        dim client as new 
            client.BaseAddress = new Uri("http://myhosturl")
            client.DefaultRequestHeaders.Accept.Clear()
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"))

            Dim response as HttpResponseMessage  = client.Get("api/ControllerName/"+id.ToString())
            if (response.IsSuccessStatusCode) then


                 myfile = response.Content.ReadAsByteArray()


            else

                return null
            End if
    Dim oFileStream As System.IO.FileStream
    oFileStream = New System.IO.FileStream("myfile.pdf",System.IO.FileMode.Create)
    oFileStream.Write(myfile, 0, myfle.Length)
    oFileStream.Close()

将方法设为异步

可能是一种很好的做法