1

i am trying to send PDF file using itextsharp , the email sends fine but with out the pdf attachment can some tell me what is wrong with my code ?
here is my code :

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)
Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124

1 Answers1

2

Try flushing the writer and rewinding the stream:

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;

Adapted from: iTextSharp - Sending in-memory pdf in an email attachment

Community
  • 1
  • 1
Douglas
  • 53,759
  • 13
  • 140
  • 188