我有一个项目要求,我们需要将 HTML 格式的日志表附加到发送给用户的电子邮件中。我不希望日志表成为正文的一部分。我不想使用 HTMLTextWriter 或 StringBuilder,因为日志表非常复杂。
是否有另一种我没有提到的方法或可以使这变得更容易的工具?
注意:我已经使用 MailDefinition 类并创建了一个模板,但我还没有找到一种方法将其作为附件(如果可能的话)。
由于您使用的是 WebForms,我建议将控件中的日志表呈现为字符串, 进而将其附加到 MailMessage.
渲染部分看起来有点像这样:
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();
}
如果您有可编辑控件(TextBox
, DropDownList
等),您需要在调用之前将它们替换为标签或文字GetRenderedHtml()
. See 这篇博文一个完整的例子。
这是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(
"[email protected]",
"[email protected]",
"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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)