2

i am trying to read a html file and convert it to pdf using itexsharp and send it as attachment by email :

this is the client side code

<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>

server side code

 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

Each part of this code works fine, i.e. the pdf works, email is fine but how can I use the pdf file as my attached file?

Kev
  • 118,037
  • 53
  • 300
  • 385
Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124
  • 3
    you may want to check out his question: http://stackoverflow.com/questions/1196059/itextsharp-sending-in-memory-pdf-in-an-email-attachment – kaveman May 23 '12 at 20:45

2 Answers2

3

Store PDF as a file, and then add it to the message by using the MailMessage.Attachments property.

Evgeni
  • 3,341
  • 7
  • 37
  • 64
1

You can either have the full path, sth like this: c:\asdasd\adad\" + filename.pdf

or you use the Server.MapPath("/subdir/filename.pdf");

Server.MapPath gives you the current directory, which is the directory you are already in.

user1253073
  • 374
  • 2
  • 6
  • 26