我正在尝试使用itextsharp发送PDF文件,电子邮件发送正常但没有pdf附件 有人可以告诉我我的代码有什么问题吗? 这是我的代码:
Dim pdfFile As MemoryStream = New MemoryStream()
Dim d As Document = New Document(PageSize.A4, 60, 60, 40, 40)
Dim w As PdfWriter = PdfWriter.GetInstance(d, pdfFile)
w.Open()
w.Add(New Paragraph("do foo something "))
Dim message As New MailMessage()
message.From = New MailAddress("******")
message.To.Add(New MailAddress("***************"))
message.Subject = "new image "
message.Body = "this is the apak chart img"
Dim data As New Attachment(pdfFile, New System.Net.Mime.ContentType("application/pdf"))
message.Attachments.Add(data)
Dim client As New SmtpClient()
client.Host = "smtp.gmail.com"
client.Credentials = New NetworkCredential("*****", "*******")
client.EnableSsl = True
client.Port = 587
client.Send(message)
答案 0 :(得分:2)
尝试刷新编写器并重绕流:
Dim pdfFile As MemoryStream = New MemoryStream()
Dim d As Document = New Document(PageSize.A4, 60, 60, 40, 40)
Dim w As PdfWriter = PdfWriter.GetInstance(d, pdfFile)
w.Open()
w.Add(New Paragraph("do foo something "))
w.CloseStream = false;
d.Close();
pdfFile.Position = 0;
改编自:iTextSharp - Sending in-memory pdf in an email attachment