0

im exporting web page div into pdf and sending that mail to pdf. for first time its creating pdf and sending mail properly.but for next time its giving error saying . file is bieng used by a another process at this line

 PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));

my aspx.cs code is-

System.IO.StringWriter stringWrite = new System.IO.StringWriter();


                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                design.RenderControl(htmlWrite);
                string myText = stringWrite.ToString().Replace("&", "&");
                StringReader sr = new StringReader(myText.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

                string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + ".pdf";
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);

                pdfDoc.Close();
                pdfDoc.Dispose();

                LblNoteMsg.Text = strPath;
                DateTime input = DateTime.Now;
                int delta = DayOfWeek.Monday - input.DayOfWeek;
                DateTime dats = DateTime.Now.AddDays(delta);
                //this week
                DateTime monday = input.AddDays(delta);
                string MonDate = monday.ToShortDateString();
                DateTime sat = monday.AddDays(5);
                string SatDate = sat.ToShortDateString();
                StreamReader r = new StreamReader(Server.MapPath("~/WeeklyMail.txt"));
                string body = r.ReadToEnd();
                MailMessage Msg = new MailMessage();
                string MailId = txtMailId.Text;
                foreach (string ss in MailId.Split(",".ToCharArray()))
                {
                    if (string.IsNullOrEmpty(ss) == false)
                    {
                        Msg.To.Add(new MailAddress(ss));
                    }
                }


                Msg.Subject = "Weekly status Report";
                Msg.Body = body;
                Msg.IsBodyHtml = true;
                Msg.Attachments.Add(new Attachment(strPath));

                SmtpClient MailServer = new SmtpClient();
                try
                {

                    MailServer.Send(Msg);
  • 1
    Have you tried to assign new FileStream(strPath, FileMode.Create) to a separate varaible and then call either Close() or Dispose() on it when you are done writing? – Denis Yarkovoy Jun 08 '15 at 05:25

3 Answers3

3

In below code you can add some timestamp to file name so that different files get created.

string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + ".pdf";

To

string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + DateTime.Now.ToString("dd-MM-yyyy_HH:mm:ss") +".pdf";
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
  • its giving error if i change the file path name ,The given path's format is not supported.@Nitin Varpe –  Jun 08 '15 at 05:30
  • Use Path.Combine, http://stackoverflow.com/questions/7348768/the-given-paths-format-is-not-supported and http://stackoverflow.com/questions/21468159/filestream-the-given-paths-format-is-not-supported – Nitin Varpe Jun 08 '15 at 05:32
  • D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015 10:58:39.pdf ....this is the path its creating @Nitin Varpe –  Jun 08 '15 at 05:32
  • still same error. D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015_11:03:24.pdf @Nitin Varpe –  Jun 08 '15 at 05:38
  • Whats error now, file being used or path format incorrect? – Nitin Varpe Jun 08 '15 at 05:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79916/discussion-between-coder-and-nitin-varpe). –  Jun 08 '15 at 05:41
0

I have changed file name by adding date and time.

string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + DateTime.Now.ToString(" dd-MM-yyyy_HH-mm-ss") + ".pdf";
Saud Khan
  • 786
  • 9
  • 24
0

After sending the email

Msg.Attachments.Dispose();
Saud Khan
  • 786
  • 9
  • 24