在线创建一个PDF文件,将其保存在服务器上并作为电子邮件附件发送

本文关键字:服务器 存在 电子邮件 保存 创建 一个 文件 PDF 在线 | 更新日期: 2023-09-27 18:35:41

我有一个带有网格的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文件,将其保存在服务器上并作为电子邮件附件发送

您可以按如下方式使用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);
        }