开发者

Send Partial Page as File Attachment via Email Asp.Net

开发者 https://www.devze.com 2023-04-06 21:41 出处:网络
Is it possible to send a partial page, say content of an IFrame or Content of a Panel as an attachment in an Email?

Is it possible to send a partial page, say content of an IFrame or Content of a Panel as an attachment in an Email?

I am dynamically creating manufacturing drawing pages with dynamic dimensioning. I'd like the user to be able to click开发者_开发知识库 an Email button and send it as attachment to the vendor.


You can use Server.Execute to execute a page, capture the output in your TextWriter, and then use that string to build an html attachment for your e-mail. The server.execute is still executed in the current user context, so if you put their selections in the Session or so, it´s easy enough to build the nexessary html. There are some pitfalls though:

  • If you´re linking to images and using hrefs, either make those absolute links or use a base href tag in your head
  • I´d beware of sending html attachments: spam filters and virus scanners could block them.

Good luck!

Menno


            Winnovative.WnvHtmlConvert.PdfConverter p = new Winnovative.WnvHtmlConvert.PdfConverter();

            p.LicenseKey = "NotPostingTHat";
            p.PdfDocumentOptions.GenerateSelectablePdf = true;
            /*
            ... Setting a bunch of options ...
            */
            p.PdfFooterOptions.DrawFooterLine = false;

            System.IO.TextWriter writer = new System.IO.StringWriter();

            string html = "";
            string pdfType = GetPdfType();

            switch (pdfType.ToUpper())
            {
                case "THEME":
                    HttpContext.Current.Server.Execute("~/Pdf/Theme.aspx", writer);
                    break;
                /* More cases */
            }

            html = writer.ToString();

            if (html.Length > 0)
            {
                byte[] bytes = p.GetPdfBytesFromHtmlString(html);
                context.Response.ContentType = "application/pdf";
                context.Response.OutputStream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("No data found!");

                foreach (string item in context.Request.Form.AllKeys)
                    context.Response.Write(String.Format("{0}: {1}\n", new object[] { item, context.Request.Form[item] }));
            }

Nothing to it. This was in an .ashx.

Menno

0

精彩评论

暂无评论...
验证码 换一张
取 消