简体   繁体   中英

How to email a PDF without opening it

I have this code that creates a PDF file and then emails it. However, when I run it, it opens and will change my entire page to PDF. How can I stop the page to open the PDF?

 PdfWriter.GetInstance(document, Response.OutputStream);
 Response.ContentType = "application/pdf";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 HTMLWorker htmlparser = new HTMLWorker(document);
 MemoryStream memoryStream = new MemoryStream();
 PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);

 document.Open();
 document.Add(mainTable);
 Response.Write(document);
 writer.CloseStream = false;
 document.Close();
 memoryStream.Position = 0;

 EmailPresenter.SkyMail asd = new EmailPresenter.SkyMail();
 asd.SendMail("test@test.com", "This is a test email...", memoryStream, "Test.pdf");
 Response.End();

I do not know this PdfWriter library. But my suggestion would be to not provide the Response.OutputStream to the PdfWriter.

Simply delete the first three lines and the Response stream will remain untouched. You might also want to remove the last line with the Response.End() method call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM