在ASP.NET中以编程方式发送带有附件的邮件 [英] Sending mail with attachments programmatically in ASP.NET

查看:59
本文介绍了在ASP.NET中以编程方式发送带有附件的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于ASP.NET中的GridView动态生成许多不同类型的文件-Excel电子表格和HTML文件.我正在使用此代码(这仅用于Excel电子表格):

I am dynamically generating a number of different types of files based upon a GridView in ASP.NET - an Excel spreadsheet and a HTML file. I am doing so using this code (this is just for the Excel spreadsheet):

  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();

我想为用户提供以下选项:通过电子邮件将生成的文件作为附件发送给他们指定的电子邮件地址或与他们在数据库上的帐户关联的附件.但是我不希望用户必须保存文件,然后以某种形式附加文件-我想自动附加生成的文件.这有可能吗?有多容易?

I would like to give users the options of emailing the generated file as an attachment to either an email address they specify or one linked with their account on the database. But I don't want the user to have to save the file, then attach it in a form - I'd like to automatically attach the generated file. Is this possible, and how easy is it?

当然,如果可能的话,我将使用System.Net.Mail类发送邮件!

Of course, I'll be using the System.Net.Mail class to send mail...if it's possible anyway!

推荐答案

您也许可以从字符串创建System.Net.Mail.Attachment,然后照常发送邮件.

You might be able to create System.Net.Mail.Attachment from string then send out the mail as normal.

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);

这篇关于在ASP.NET中以编程方式发送带有附件的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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