将数据表转换为pdf并将该文档作为附件发送电子邮件 [英] converting data table to pdf and sending email with that document as attachment

查看:53
本文介绍了将数据表转换为pdf并将该文档作为附件发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将数据表转换为PDF并将PDF作为附件发送到电子邮件,它正在转换和发送电子邮件但是当我尝试使用Adobe Reader打开附件时它显示的消息因为无法下载文件附件下载,因为它不是支持的格式或文件已损坏....

这是我的代码

I have converted data table to PDF and sending PDF as attached file to email,it is converting and sending email but when i tried to open attachment using Adobe Reader it is displaying message as could not download file attachment download because it is not either supported format or file has been damaged ....
Here is my code

       public void ExportToPdf(DataTable dt)
       {
           var lastMonth = DateTime.Now.AddMonths(-1).ToString("Y");
           Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
           StringWriter sw = new StringWriter();
           HtmlTextWriter hw = new HtmlTextWriter(sw);
           StringReader sr = new StringReader(sw.ToString());

           HTMLWorker htmlparser = new HTMLWorker(document);
           iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream("D:/PROJECTS/EMSmain/Attendence_Report/lastMonth.pdf", FileMode.Create));
           document.Open();
           htmlparser.Parse(sr);
           iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

           PdfPTable table = new PdfPTable(dt.Columns.Count);
           PdfPRow row = null;
           float[] widths = new float[] { 10f, 10f, 10f };

           table.SetWidths(widths);

           table.WidthPercentage = 100;
           int iCol = 0;
           string colname = "";
           PdfPCell cell = new PdfPCell(new Phrase("Products"));

           cell.Colspan = dt.Columns.Count;

           foreach (DataColumn c in dt.Columns)
           {

               table.AddCell(new Phrase(c.ColumnName, font5));
           }

           foreach (DataRow r in dt.Rows)
           {
               if (dt.Rows.Count > 0)
               {
                   table.AddCell(new Phrase(r[0].ToString(), font5));
                   table.AddCell(new Phrase(r[1].ToString(), font5));
                   table.AddCell(new Phrase(r[2].ToString(), font5));

               }

           }


           document.Add(table);
           document.Close();
       }
private void sendEmail()
       {
           var doc = new Document();

           var lastMonth = DateTime.Now.AddMonths(-1).ToString("Y");
           MemoryStream memoryStream = new MemoryStream();
           iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, memoryStream);
           string fromAdd = "admin@technologyport.net";
           string Subject = "Attendence Report";
           string tomail = "vijayalakshmi@gmail.com";
           string Body = "Dear sir," + Environment.NewLine + Environment.NewLine + Environment.NewLine +
                         "Please find Attendence Report for the '"+lastMonth+"' in this Attachment " +
                        Environment.NewLine + Environment.NewLine + Environment.NewLine +
                         "Thanks & Regards" + Environment.NewLine +
                         "Admin" + "";



           MailMessage obj = new MailMessage(fromAdd, tomail, Subject, Body);
           obj.Attachments.Add(new Attachment(memoryStream, "lastMonth.pdf"));
           //Attachment attach = new Attachment(stream, "Attachment file name");
           //obj.Attachments.Add(attach);
           //new SmtpClient().Send(obj);
           NetworkCredential nc = new NetworkCredential(ConfigurationManager.AppSettings["admin"], ConfigurationManager.AppSettings["adminpwd"]);

           SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
           sc.Credentials = nc;
           sc.EnableSsl = true;
           sc.Send(obj);
       }

推荐答案

sendEmail 方法内,你正在阅读一份空白文件。

Inside the sendEmail method, you are reading a blank document.
var doc = new Document();



但我想你想阅读你在 ExportToPdf 方法。所以,阅读这个文件并附上它。



另外,看看你提到的那个位置保存的pdf文件是否正在打开。


But I think you wanted to read the pdf which you created in ExportToPdf Method. So, read this file and attach it.

Also, see if the pdf file saved in that location you have mentioned is opening or not.


这篇关于将数据表转换为pdf并将该文档作为附件发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆