在运行时生成HTML文件,并作为电子邮件发送附件 [英] Generate HTML file at runtime and send as email attachment

查看:369
本文介绍了在运行时生成HTML文件,并作为电子邮件发送附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我们需要一个HTML格式的记录表附加到被发送到用户的电子邮件项目的要求。 我不希望记录表成为身体的一部分。我宁可不使用的HtmlTextWriter或StringBuilder的,因为记录表是相当复杂的。

I have a project requirement that we need to attach an HTML formatted log sheet to an email that gets sent to a user. I don't want the log sheet to be part of the body. I'd rather not use HTMLTextWriter or StringBuilder because the log sheet is quite complex.

难道还有其他的,我不是提的方法​​或工具,这将使这更容易?

Is there another method that I'm not mentioning or a tool that would make this easier?

请注意:我已经与MailDefinition类工作,并创建了一个模板,但我还没有找到一种方法,使这个附件如果这甚至有可能

Note: I've worked with the MailDefinition class and created a template but I haven't found a way to make this an attachment if that's even possible.

推荐答案

由于您使用的WebForms,我会建议<一href=\"http://www.jrummell.com/blog/index.php/2009/06/send-a-completed-form-email-without-a-stringbuilder/\"相对=nofollow>渲染在控制作为一个字符串你的记录表,然后的附加了一个 MAILMESSAGE

Since you're using WebForms, I would recommend rendering your log sheet in a Control as a string, and then attaching that to a MailMessage.

渲染部分看起来有点像这样:

The rendering part would look a bit like this:

public static string GetRenderedHtml(this Control control)
{
    StringBuilder sbHtml = new StringBuilder();
    using (StringWriter stringWriter = new StringWriter(sbHtml))
    using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter))
    {
        control.RenderControl(textWriter);
    }
    return sbHtml.ToString();
}

如果您有可编辑控件(文本框的DropDownList ,等等),你需要更换他们之前调用标签或字面 GetRenderedHtml()。见<一href=\"http://www.jrummell.com/blog/index.php/2009/06/send-a-completed-form-email-without-a-stringbuilder/\"相对=nofollow>这个博客帖子以一个完整的例子。

If you have editable controls (TextBox, DropDownList, etc), you'll need to replace them with Labels or Literals before calling GetRenderedHtml(). See this blog post for a complete example.

这里的 MSDN例如,对于附件

// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = "data.xls";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
   "jane@contoso.com",
   "ben@contoso.com",
   "Quarterly data report.",
   "See the attached spreadsheet.");

// Create  the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);

这篇关于在运行时生成HTML文件,并作为电子邮件发送附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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