如何使用System.Net.Mail为电子邮件添加附件?
我有一个表示为byte []的excel文档,我想将其作为附件发送到电子邮件中。
我在构建附件时遇到了一些麻烦。
我可以创建一个具有以下构造函数的Attachment:
(Stream contentStream, ContentType contentType) (Stream contentStream, string name) (Stream contentStream, string name, string mediaType)
我的想法是从byte []创建一个MemoryStream并将其传递给创建附件的方法。
不幸的是,我看不到从MemoryStream获取预期文件名和内容类型的方法,我也看不到如何提供正确的内容类型。 有纯文本,Pdf,Rtf等选项,但我没有看到立即跳出来作为我应该用于Excel文档的那个。
我能找到的最接近的是MediaTypeNames.Application.Octet ,它指出:
Octet成员指定附件包含通用二进制数据。
但是,即使这是一个使用的,除非它可以作为Stream的属性传递,那么我的发送电子邮件的方法只能发送一个byte []作为Excel文档…
是否有其他类型的流可以使用? 或者我是否必须创建我自己的Stream类型,其中包含我需要的详细信息。
当然有人在那里做过这件事,当然微软会把这个想到这个级别….
任何帮助将非常感激。
更新:请不要为使用将文件名作为字符串的构造函数的任何答案投票。 我真的需要帮助使用带有Stream的那些……我想避免将文件写入磁盘,通过电子邮件发送,然后立即将其删除。 既然有一种方法允许我这样做,我想尽可能使用那个。
解决方案更新
康拉德设法找到了我想要的东西! 谢谢堆人!
我只是记录建议的解决方案,以防万一在提供的链接上的内容发生了什么。
这个解决方案归功于www.systemnetmail.com
static void AttachmentFromStream() { //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("me@mycompany.com"); mail.To.Add("you@yourcompany.com"); //set the content mail.Subject = "This is an email"; mail.Body = "this content is in the body"; //Get some binary data byte[] data = GetData(); //save the data to a memory stream MemoryStream ms = new MemoryStream(data); //create the attachment from a stream. Be sure to name the data //with a file and //media type that is respective of the data mail.Attachments.Add( new Attachment( ms, "example.txt", "text/plain" )); SmtpClient smtp = new SmtpClient("127.0.0.1"); smtp.Send(mail); }
在我的情况下,它只是意味着我将不得不改变我的方法采取文件名和fileformat作为字符串。 我会尝试使用Octet …但如果没有,我将只传入官方的MIME类型。
考虑到所有这些,这是一个非常明显的解决方案……但我非常感谢帮助解决它…好的是,这个解决方案将记录给具有相同问题的未来程序员。
再次感谢大家的帮助!
附件构造函数确实有一个构造函数可以满足您的需要。 我假设您正在使用.NET Framework 2中的System.Net.MailMessage类。如果是这样,请阅读此链接以获取您需要的示例代码
由于接受答案的链接消失了,这里来自Wayback Machine
TL; DR: mail.Attachments.Add(new Attachment(contentStream, "yourfilename.txt", "text/plain"));
充分:
static void AttachmentFromStream() { //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("me@mycompany.com"); mail.To.Add("you@yourcompany.com"); //set the content mail.Subject = "This is an email"; mail.Body = "this content is in the body"; //Get some binary data byte[] data = GetData(); //save the data to a memory stream MemoryStream ms = new MemoryStream(data); //create the attachment from a stream. Be sure to name the data with a file and //media type that is respective of the data mail.Attachments.Add(new Attachment(ms, "example.txt", "text/plain")); //send the message SmtpClient smtp = new SmtpClient("127.0.0.1"); smtp.Send(mail); } static byte[] GetData() { //this method just returns some binary data. //it could come from anywhere, such as Sql Server string s = "this is some text"; byte[] data = Encoding.ASCII.GetBytes(s); return data; }
“附件”构造函数中的name参数是将在收件人的电子邮件中显示附件的名称。
因此,您可以自由选择名称参数(扩展名.xls首选),并将mediaType参数设置为“application / vnd.ms-excel”,这是excel文件的已定义MIME类型 。
—————我想我错了,这是因为你有一个你要附加的文件—————
看起来这里有一个带附件的邮件示例:
http://www.aspnettutorials.com/tutorials/email/email-attach-aspnet2-csharp.aspx
我希望这就是你要找的东西。