2012-05-24 98 views
1

我想發送PDF文件使用itextsharp,電子郵件發送罰款,但沒有PDF附件 有人可以告訴我什麼是我的代碼錯?
這裏是我的代碼:VB.net itextsharp附件

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

也許沖洗和倒帶流? – Douglas

+0

你試過其他文件嗎?我的意思是來自磁盤或文本流的真實文件。 – IvanH

+0

同意@Douglas。 –

回答

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