Click here to Skip to main content
15,889,736 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the code below creates a pdf file from an .aspx page. I want to be able send this pdf as an attachment.any ideas?



Imports NReco.PdfGenerator
Imports System.IO
Imports System.Net.Mail
Imports iTextSharp.text.pdf
Imports Microsoft.Office.Interop.Word


Partial Class Default2
Inherits System.Web.UI.Page


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'creates pdf file from default.aspx
Dim customernumber As String = txtcustomer.Text
Dim accountnumber As String = txtaccountnumber.Text
Dim sr As StringWriter = New StringWriter()
HttpContext.Current.Server.Execute("Default.aspx", sr)
Dim htmlmessage As String = sr.ToString


Dim pdfBytes As Byte() = New HtmlToPdfConverter().GeneratePdf(htmlmessage)
'opens the pdf file
Dim response As HttpResponse = Me.Response
response.Clear()
response.BufferOutput = True
response.AppendCookie(New HttpCookie("fileDownloadToken", String.Format("{0}{1}", customernumber, accountnumber)))
response.AddHeader("Content-Type", "binary/octet-stream")
response.AddHeader("Content-Disposition", "inline; filename=" + String.Format("{0}{1}.pdf", customernumber, accountnumber))
response.ContentType = "application/pdf"
response.BinaryWrite(pdfBytes)
response.End()



'sends email

Dim strEmail As String = "someemail"
Dim mail As New System.Net.Mail.MailMessage()
Dim SmtpServer As New SmtpClient()
Try
SmtpServer.Port = 25
SmtpServer.Host = "mail.bel.com.bz"
mail = New System.Net.Mail.MailMessage()
mail.From = New MailAddress("someemail")
mail.To.Add(strEmail)
mail.Subject = "Service Application"
mail.Body = "Service Application Form"

SmtpServer.Send(mail)

Catch ex As Exception

End Try

'response.Redirect

End Sub
'Private Sub getpdf(ByVal data1() As Byte)


' Dim ms As MemoryStream = New MemoryStream(data1)
' Dim doc As Document = New Document()
' 'require custom parsers? add here.
' Dim writer As PdfWriter = PdfWriter.GetInstance(doc, ms)
' doc.Save()
' writer.CloseStream = False
' 'important
' doc.Close()
' 'build the doc.
' ms.Position = 0
' 'reset stream... important
'End Sub

End Class
Posted

It looks like you did not even try to attach anything. All you need is to use the property System.Net.Mail.MailMessage.Attachments and the class System.Net.Mail.Attachment:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.mail.attachment%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Member 10736689 9-Apr-14 18:29pm    
i added this but somehow its not adding the pdf
Dim strEmail As String = "someemail"
Dim mail As New System.Net.Mail.MailMessage()
Dim SmtpServer As New SmtpClient()
Try
SmtpServer.Port = 25
SmtpServer.Host = "mail.bel.com.bz"
mail = New System.Net.Mail.MailMessage()
mail.From = New MailAddress("someemail")
mail.To.Add(strEmail)
mail.Subject = "Service Application"
mail.Body = "Service Application Form"
Dim attachments() As Attachment = GetAttachments(pdfBytes, "filename.pdf", MS)
If attachments IsNot Nothing Then


For Each attachment As Attachment In attachments


If attachment IsNot Nothing Then

mail.Attachments.Add(attachment)

End If

Next
End If


SmtpServer.Send(mail)

Catch ex As Exception

End Try



response.BinaryWrite(pdfBytes)
response.End()




End Sub
Public Shared Function GetAttachments(ByVal attachmentPdf As Byte(), ByVal attachmentFileName As String, ByVal ms As MemoryStream) As Attachment()


'Add pdf attachment

ms.Write(attachmentPdf, 0, attachmentPdf.Length)

ms.Position = 0



Dim mailAttachments As New List(Of Attachment)()

Dim attachment As New Attachment(ms, attachmentFileName, "application/pdf")

mailAttachments.Add(attachment)



Return mailAttachments.ToArray()

End Function
Sergey Alexandrovich Kryukov 9-Apr-14 18:31pm    
All right; did it work out for you? Adding attachments is pretty simple; I recently did it...
—SA
Member 10736689 9-Apr-14 18:34pm    
no its not working. after i have dynamically created the pdf using this code:
Dim customernumber As String = txtcustomer.Text
Dim accountnumber As String = txtaccountnumber.Text
Dim sr As StringWriter = New StringWriter()
HttpContext.Current.Server.Execute("Default.aspx", sr)
Dim htmlmessage As String = sr.ToString
Dim ms As MemoryStream
Dim pdfBytes As Byte() = New HtmlToPdfConverter().GeneratePdf(htmlmessage)

Dim response As HttpResponse = Me.Response
response.Clear()
response.BufferOutput = True
response.AppendCookie(New HttpCookie("fileDownloadToken", String.Format("{0}{1}", customernumber, accountnumber)))
response.AddHeader("Content-Type", "binary/octet-stream")
response.AddHeader("Content-Disposition", "inline; filename=" + String.Format("{0}{1}.pdf", "file", "name"))
response.ContentType = "application/pdf"

how can i save this and send the pdf. or how do i refer back to this pdf/.i am really confused
Sergey Alexandrovich Kryukov 9-Apr-14 18:49pm    
In first fragment, it looked correct. Now you are doing... I cannot understand what... You should "attach" stream (in your case), second constructor parameter (file name) is just to give it a name, it's not a real file, and last parameter is content type. Don't mix content type of 1) mail message; 2) one of its parts; 3) content returned in HTTP request.
—SA
Member 10736689 9-Apr-14 18:53pm    
i am so lost any sample code? should i do something with the memory stream?i have not used it in my code
= this code works

Dim ct As New Mime.ContentType(MediaTypeNames.Application.Pdf)
Dim pdf As New MemoryStream(pdfBytes)
Dim data As New Attachment(pdf, ct)

'send the pdf as attachment
mail.Attachments.Add(data)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900