如何从动态创建的Pdf发送Pdf作为附件? [英] How Do I Send A Pdf As Attachment From A Pdf That Was Dynamically Created?

查看:85
本文介绍了如何从动态创建的Pdf发送Pdf作为附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码从.aspx页面创建一个pdf文件。我希望能够将此pdf作为附件发送。有什么想法吗?







进口NReco。 PdfGenerator

进口System.IO

进口System.Net.Mail

进口iTextSharp.text.pdf

进口Microsoft.Office.Interop.Word





Partial Class Default2

继承System.Web.UI.Page





受保护的子Button1_Click(ByVal sender As Object,ByVal e As System.EventArgs)处理Button1.Click

'从default.aspx创建pdf文件

Dim customernumber As String = txtcustomer.Text

Dim accountnumber As String = txtaccountnumber.Text

Dim sr As StringWriter = New StringWriter()

HttpContext.Current.Server.Execute(Default.aspx,sr)

Dim htmlmessage As String = sr.ToString < br $>




Dim pdfBytes As Byte()= New HtmlToPdfConverter()。GeneratePdf(htmlmessage)

'打开t他是pdf文件

昏暗的回应因为HttpResponse = Me.Response

response.Clear()

response.BufferOutput = True

response.AppendCookie(New HttpCookie(fileDownloadToken,String.Format({0} {1},customernumber,accountnumber)))

response.AddHeader(Content-Type ,binary / octet-stream)

response.AddHeader(Content-Disposition,inline; filename =+ String.Format({0} {1} .pdf,customernumber,accountnumber))

response.ContentType =application / pdf

回复.BinaryWrite(pdfBytes)

response.End()







'发送电子邮件



Dim strEmail As String =someemail

Dim mail As New System.Net.Mail.MailMessage()

Dim SmtpServer作为新的SmtpClient()

尝试

SmtpServer.Port = 25

SmtpServer.Host =mail。 bel.com.bz

mail = New System.Net.Mail.MailMessage()

mail.From =新邮件地址(someemail)

mail.To.Add(strEmail)

mail.Subject =服务申请

mail.Body =服务申请表



SmtpServer.Send(邮件)



Catch ex As Exception



结束尝试



'response.Redirect



End Sub

'Private Sub getpdf(ByVal data1()As字节)





'Dim ms As MemoryStream = New MemoryStream(data1)

'Dim doc As Document =新文档()

''需要自定义解析器吗?在这里添加。

'昏暗的作家作为PdfWriter = PdfWriter.GetInstance(doc,ms)

'doc.Save()

'作家。 CloseStream = False

''重要

'doc.Close()

''构建文档。

'ms.Position = 0

''重置流...重要

'结束子



结束Class

the code below creates a pdf file from an .aspx page. I want to be able send this pdf as an attachment.any ideas?



Imports NReco.PdfGenerator
Imports System.IO
Imports System.Net.Mail
Imports iTextSharp.text.pdf
Imports Microsoft.Office.Interop.Word


Partial Class Default2
Inherits System.Web.UI.Page


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'creates pdf file from default.aspx
Dim customernumber As String = txtcustomer.Text
Dim accountnumber As String = txtaccountnumber.Text
Dim sr As StringWriter = New StringWriter()
HttpContext.Current.Server.Execute("Default.aspx", sr)
Dim htmlmessage As String = sr.ToString


Dim pdfBytes As Byte() = New HtmlToPdfConverter().GeneratePdf(htmlmessage)
'opens the pdf file
Dim response As HttpResponse = Me.Response
response.Clear()
response.BufferOutput = True
response.AppendCookie(New HttpCookie("fileDownloadToken", String.Format("{0}{1}", customernumber, accountnumber)))
response.AddHeader("Content-Type", "binary/octet-stream")
response.AddHeader("Content-Disposition", "inline; filename=" + String.Format("{0}{1}.pdf", customernumber, accountnumber))
response.ContentType = "application/pdf"
response.BinaryWrite(pdfBytes)
response.End()



'sends email

Dim strEmail As String = "someemail"
Dim mail As New System.Net.Mail.MailMessage()
Dim SmtpServer As New SmtpClient()
Try
SmtpServer.Port = 25
SmtpServer.Host = "mail.bel.com.bz"
mail = New System.Net.Mail.MailMessage()
mail.From = New MailAddress("someemail")
mail.To.Add(strEmail)
mail.Subject = "Service Application"
mail.Body = "Service Application Form"

SmtpServer.Send(mail)

Catch ex As Exception

End Try

'response.Redirect

End Sub
'Private Sub getpdf(ByVal data1() As Byte)


' Dim ms As MemoryStream = New MemoryStream(data1)
' Dim doc As Document = New Document()
' 'require custom parsers? add here.
' Dim writer As PdfWriter = PdfWriter.GetInstance(doc, ms)
' doc.Save()
' writer.CloseStream = False
' 'important
' doc.Close()
' 'build the doc.
' ms.Position = 0
' 'reset stream... important
'End Sub

End Class

推荐答案

看起来你甚至没有尝试附加任何东西。您所需要的只是使用属性 System.Net.Mail.MailMessage.Attachments 和类 System.Net.Mail.Attachment

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.net.mail.attachment%28v=vs.110%29.aspx [ ^ ]。br />


-SA
It looks like you did not even try to attach anything. All you need is to use the property System.Net.Mail.MailMessage.Attachments and the class System.Net.Mail.Attachment:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.mail.attachment%28v=vs.110%29.aspx[^].

—SA


=
此代码有效



Dim ct As New Mime.ContentType (MediaTypeNames.Application.Pdf)

Dim pdf As New MemoryStream(pdfBytes)

Dim data As New Attachment(pdf,ct)



'将pdf作为附件发送

mail.Attachments.Add(data)
= this code works

Dim ct As New Mime.ContentType(MediaTypeNames.Application.Pdf)
Dim pdf As New MemoryStream(pdfBytes)
Dim data As New Attachment(pdf, ct)

'send the pdf as attachment
mail.Attachments.Add(data)


这篇关于如何从动态创建的Pdf发送Pdf作为附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆