Web服务器请求在电子邮件附件C#中生成pdf [英] Webserver request to generate pdf in an email attachment C#

查看:60
本文介绍了Web服务器请求在电子邮件附件C#中生成pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如果您能告诉我我的代码需要做什么,我会很感激。我的代码动态地使用iTextSharp从HTML创建PDF并将生成的PDF作为电子邮件附件发送。但我需要做的就是如何向
返回Base64编码PDF的Web服务器发送请求?因为我需要执行相同的过程,发送请求以将Base64编码的PDF附加到电子邮件中!任何帮助将不胜感激。

I'd appreciate if you could tell me what do I need to do with my code. My code below works dynamically in creating PDF from HTML using iTextSharp and send the generated PDF as Email Attachment. But all I need to do is how to send a request to a web server that returns a Base64 Coded PDF? As I need to do the same procedure sending a request to get Base64 coded PDF to be attached to emails! Any help would be appreciated.

  protected void Page_Load(object sender, EventArgs e)
    {
    //string in memory by making use of MemoryStream class 
     if (!this.IsPostBack)
     {
       DataTable dt = new DataTable();
       dt.Columns.AddRange(new DataColumn[3] {
                    new DataColumn("NR"),
                    new DataColumn("SERVICETYP"),
                    new DataColumn("STATUS")});
      dt.Rows.Add(101, "", 5);
      dt.Rows.Add(102, "", 2);
      dt.Rows.Add(103, "", 12);
      dt.Rows.Add(104, "", 9);
      SendPDFEmail(dt);
   }

  }
  private void SendPDFEmail(DataTable dt)
 {
  using (StringWriter sw = new StringWriter())
  {
     using (HtmlTextWriter hw = new HtmlTextWriter(sw))
    {
        string companyName = "ASPSnippets";
        int orderNo = 2303;
        StringBuilder sb = new StringBuilder();
        sb.Append("<table width='100%' cellspacing='0' 
        cellpadding='2'>");
        sb.Append("<tr><td align='center' style='background-color: 
         #18B5F0' colspan = '2'><b>Order Sheet</b></td></tr>");
        sb.Append("<tr><td colspan = '2'></td></tr>");
        sb.Append("<tr><td><b>Order No:</b>");
        sb.Append(orderNo);
        sb.Append("</td><td><b>Date: </b>");
        sb.Append(DateTime.Now);
        sb.Append(" </td></tr>");
        sb.Append("<tr><td colspan = '2'><b>Company Name :</b> ");
        sb.Append(companyName);
        sb.Append("</td></tr>");
        sb.Append("</table>");
        sb.Append("<br />");
        sb.Append("<table border = '1'>");
        sb.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            sb.Append("<th style = 'background-color: 
         #D20B0C;color:#ffffff'>");
            sb.Append(column.ColumnName);
            sb.Append("</th>");
        }
        sb.Append("</tr>");
        foreach (DataRow row in dt.Rows)
        {
            sb.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                sb.Append("<td>");
                sb.Append(row[column]);
                sb.Append("</td>");
            }
            sb.Append("</tr>");
        }
        sb.Append("</table>");
        StringReader sr = new StringReader(sb.ToString());

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, 
            memoryStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

            MailMessage mail = new MailMessage();
            mail.To.Add("password");
            mail.From = new MailAddress("email");

            mail.Subject = "iTextSharp PDF";
            mail.Body = "iTextSharp PDF Attachment";
            mail.Attachments.Add(new Attachment(new MemoryStream(bytes), 
           "iTextSharpPDF.pdf"));
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Credentials = new 
            System.Net.NetworkCredential("Email", 
             "Password");
            smtp.Port = 587;
            smtp.Send(mail);
        }
    }
}



}

}

推荐答案

请在ASP.NET论坛(http://forums.asp.net)上发布与ASP.NET和Web应用程序相关的问题。返回编码文件仅在某些类型的应用程序的当前情况下才有意义。对于其他Web应用程序,您只需返回常规文件和
然后对其进行编码。 ASP.NET论坛的人员可以相应地指导您。
Please post questions related to ASP.NET and web applications in the ASP.NET forums (http://forums.asp.net ). Returning an encoded file only makes sense in current cases for certain types of apps. For other web apps you'd just return the regular file and then encode it. The ASP.NET forums folks will be able to direct you accordingly.


这篇关于Web服务器请求在电子邮件附件C#中生成pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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