【问题标题】:create a pdf file online ,save it on server and send it as an email attachment在线创建pdf文件,将其保存在服务器上并作为电子邮件附件发送
【发布时间】:2013-10-17 09:34:16
【问题描述】:

我有一个带有网格的 web 表单。我想生成整个 web 表单和网格数据的 pdf,将其保存在服务器端本身,而不像我们在将数据导出为 pdf 时那样将其保存在本地计算机上,并将该 pdf 作为电子邮件附件发送。我想在网站在服务器上运行时执行所有这些功能。我怎样才能完成这项任务。

我有将网格数据转换为 pdf 的代码,但不知道如何在服务器上保存该 pdf 文件

我使用以下类型的代码转换为 pdf

private void Export_to_Pdf()

{

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=DotnetGeekBlog.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string str = “<h1 title=’Header’ align=’Center’> Writing To PDF Using ASP.NET</h1> <br><table align=’Center’><tr><td style=’width:100px;color:green’> <b>iTextSharp</b></td><td style=’width:100px;color:red’>dotnetgeekblog</td></tr></table>”;
StringReader sr = new StringReader(str);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}

文件刚刚下载到本地机器上。我想将它保存在服务器上并通过电子邮件发送。

提前致谢。

【问题讨论】:

  • 如果您不保存它,如何将其作为附件通过电子邮件发送
  • 您必须将此 pdf 保存在服务器端并在邮件发送后删除或删除...关于您的下载,这可能取决于您的代码,您可以删除下载代码...它适用于我
  • 我们使用第三方库dynamicpdf.com(动态pdf生成器)在我们的网站上创建pdf。它很容易使用,但它不是免费的。
  • @REx - 您可以使用流附加。内存流也可以服务。 msdn.microsoft.com/en-us/library/6sdktyws.aspx

标签: c# asp.net


【解决方案1】:

你可以使用System.net命名空间,如下:

public static void CreateMessageWithAttachment(string server)
    {
        MemoryStream file = // Your Memeory stream to send as attachment
        MailMessage message = new MailMessage(
           "abc@xyz.com",
           "pqr@xyz.com",
           "report.",
           "See the attached pdf.");

        Attachment data = new Attachment( file , "example.pdf", "application/pdf" );

        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

          client.Send(message);

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多