Sending generated pdf file from a gridview as an email attachmenthttp://forums.asp.net/t/1798542.aspx/1?Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentTue, 01 May 2012 03:29:59 -040017985424958556http://forums.asp.net/p/1798542/4958556.aspx/1?Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentSending generated pdf file from a gridview as an email attachment <p>Hello,&nbsp;</p> <p>I managed to create a PDF file from a gridview using the following code:</p> <pre class="prettyprint">string attachment = &quot;attachment; filename=test.pdf&quot;; Response.ClearContent(); Response.AddHeader(&quot;content-disposition&quot;, attachment); Response.ContentType = &quot;application/pdf&quot;; StringWriter stw = new StringWriter(); HtmlTextWriter htextw = new HtmlTextWriter(stw); gridview.RenderControl(htextw); Document document = new Document(); PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); StringReader str = new StringReader(stw.ToString()); HTMLWorker htmlworker = new HTMLWorker(document); htmlworker.Parse(str); document.Close(); Response.Write(document);</pre> <p>I also want to send the PDF file as an email attachment. But I don't know which element of the above code should be used as the attachment file.</p> <pre class="prettyprint">Attachment file = new Attachment(What should I insert here?????); message.Attachments.Add(file);</pre> <p>TIA</p> <p></p> 2012-04-30T15:54:54-04:004958625http://forums.asp.net/p/1798542/4958625.aspx/1?Re+Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentRe: Sending generated pdf file from a gridview as an email attachment <pre class="prettyprint">Using fs As New FileStream(reportPath, FileMode.Create) fs.Write(fbytes, 0, fbytes.Length) End Using</pre> <p>where you save this file is the path you give for the email attachment.</p> 2012-04-30T16:51:29-04:004958655http://forums.asp.net/p/1798542/4958655.aspx/1?Re+Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentRe: Sending generated pdf file from a gridview as an email attachment <p>Thanks UstesG.</p> <p>Tried that, but didnt recognize fbytes saying that it doesnt exist in the current context.</p> <p>Also, I'm using C# instead of VB. I guess then, that the code should be something like the following:</p> <pre class="prettyprint">string path= &quot;C:\\&quot;; FileStream fs = new FileStream(path, FileMode.Create); fs.Write(fbytes, 0, fbytes.Length); Attachment file= new Attachment(path); message.Attachments.Add(file);</pre> <p>Is that correct? Or am I missing something? (Quite frankly, I'm still a beginner in .NET Development)</p> <p><br> <br> </p> 2012-04-30T17:17:40-04:004958798http://forums.asp.net/p/1798542/4958798.aspx/1?Re+Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentRe: Sending generated pdf file from a gridview as an email attachment <p>fbytes is your response stream saved to a byte array.&nbsp; Basically you have to save the html to disk first before you can attached it.&nbsp;</p> 2012-04-30T18:47:29-04:004959043http://forums.asp.net/p/1798542/4959043.aspx/1?Re+Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentRe: Sending generated pdf file from a gridview as an email attachment <p>Could you please post an example?</p> <p>I'm really strugglin with this&nbsp;<img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-frown.gif" alt="Frown" title="Frown" border="0" class="emoticon"></p> 2012-04-30T23:10:47-04:004959150http://forums.asp.net/p/1798542/4959150.aspx/1?Re+Sending+generated+pdf+file+from+a+gridview+as+an+email+attachmentRe: Sending generated pdf file from a gridview as an email attachment <pre class="prettyprint">lets see if this gets you out of the rut....</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">Dim ms As New MemoryStream Dim doc As New iTextSharp.text.Document() Dim writer As PdfWriter = Nothing writer = PdfWriter.GetInstance(doc, ms) 'build your pdf 'get doc into byte array Dim fbytes As Byte() = ms.GetBuffer 'save bytestream to file Using fs As New FileStream(reportPath, FileMode.Create) fs.Write(fbytes, 0, fbytes.Length) End Using 'attach reportpath to email after here</pre> <p></p> 2012-05-01T03:29:59-04:00