我正在尝试读取一个 html 文件并使用 itexsharp 将其转换为 pdf 并通过电子邮件将其作为附件发送:
这是客户端代码
<form id="form1" runat="server">
<div>
<img alt="" src="C:\Users\Intern\Documents\Visual Studio 2008\Projects\highChart\highChart\images\279.gif" id="myImg" />
</div>
</form>
服务器端代码
Imports iTextSharp
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System
Imports System.Text
Imports System.IO
Imports System.Net.Mail
Imports System.Net
Imports iTextSharp.text.html.simpleparser
Partial Public Class imagePdf
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.[End]()
Dim file As (( i don't know what to add here ))
Dim message As New MailMessage()
message.From = New MailAddress("testApp@somewhereelse.com")
message.To.Add(New MailAddress("anotheraddres84@hotmail.com"))
message.Subject = "pdf "
message.Body = "pdf attached "
Dim data As New Attachment(File)
message.Attachments.Add(data)
Dim client As New SmtpClient()
client.Host = "smtp.gmail.com"
client.Credentials = New NetworkCredential("email", "password")
client.EnableSsl = True
client.Port = 587
client.Send(message)
End Sub
End Class
此代码的每个部分都可以正常工作,即 pdf 可以正常工作,电子邮件也可以,但我如何使用 pdf 文件作为附件?