2 Replies - 1640 Views - Last Post: 18 September 2012 - 10:14 PM Rate Topic: -----

#1 Robin228  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-September 12

Convert aspx to pdf and send it via mail as attachment

Posted 14 September 2012 - 01:09 AM

Hi,
I am able to convert aspx page to pdf. Problem is i hv text boxes and its values are not getting saved in pdf file. Also i need some assistance of how to send it via email.Here is the code am using

Thanks in advance.

  protected void Page_Load(object sender, EventArgs e)
        {
         
        }

         protected void SendMail()
        {

            var userName = "4@gmail.com";
           
            var toAddress = YourEmail.Text.ToString();
            
            const string Password = "Mypassword1#";
          
            string subject = YourSubject.Text.ToString();
            string body = "From: " + YourName.Text + "\n";
            body += "Email: " + YourEmail.Text + "\n";
            body += "Subject: " + YourSubject.Text + "\n";
            body += "Question: \n" + Comments.Text + "\n";
        
           
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "10.238.52.210";
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(userName, Password);
                smtp.Timeout = 20000;
            }

            smtp.Send(userName, toAddress, subject, body);
        }

         protected void Button1_Click(object sender, EventArgs e)
        {

            Response.ContentType = "application/pdf"; 
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache); 
            StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); 
            this.Page.RenderControl(hw); 
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); 
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
            pdfDoc.Open(); 
            htmlparser.Parse(sr); 
           
            Response.Write(pdfDoc);
           

             try
            {
                 SendMail();
               
                DisplayMessage.Text = "Your Comments after sending the mail";
                DisplayMessage.Visible = true;
                YourSubject.Text = "";
                YourEmail.Text = "";
                YourName.Text = "";
                Comments.Text = "";
                pdfDoc.Close();
                Response.End();
            }
            catch (Exception) { }


Is This A Good Question/Topic? 0
  • +

Replies To: Convert aspx to pdf and send it via mail as attachment

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

Re: Convert aspx to pdf and send it via mail as attachment

Posted 14 September 2012 - 07:23 AM

I'm guessing that you're using iTextSharp for the PDF creation? I suggest you ask for support with them. This is a general purpose C# forum, there's no guarantee that we have anyone that's worked with that product before.

As to sending it as an email...you probably need to do that before you write it out as your HTTP response. Also, I've had trouble using Gmail as a relay before.
Was This Post Helpful? 0
  • +
  • -

#3 Windina  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 20-August 12

Re: Convert aspx to pdf and send it via mail as attachment

Posted 18 September 2012 - 10:14 PM

You can use a PDF component to realize this by below code:

        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            String url = "http://www.apple.com";
            doc.LoadFromHTML(url, false, true, true);
            doc.SaveToFile("sample.pdf");
            doc.Close();
        }


Details can be seen: Aspx to PDF
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1