Chr*_*ris 8 c# asp.net email attachment
我基于ASP.NET中的GridView动态生成许多不同类型的文件 - Excel电子表格和HTML文件.我正在使用此代码(这仅适用于Excel电子表格):
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=InvoiceSummary" + Request.QueryString["id"] + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
contents.RenderControl(htmlWrite);
//GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Run Code Online (Sandbox Code Playgroud)
我想向用户提供选项,通过电子邮件将生成的文件作为附件发送给他们指定的电子邮件地址,或者将其与数据库中的帐户相关联.但我不希望用户必须保存文件,然后将其附加到表单中 - 我想自动附加生成的文件.这可能吗,它有多容易?
当然,我将使用System.Net.Mail类发送邮件......如果可能的话!
您可以从字符串创建System.Net.Mail.Attachment然后正常发送邮件.
var m = new System.Net.Mail.MailMessage(from, to, subject, body);
var a = System.Net.Mail.Attachment.CreateAttachmentFromString(stringWrite.ToString(), "application/vnd.xls");
m.Attachments.Add(a);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24106 次 |
| 最近记录: |