如何使用System.Net.Mail将附件添加到电子邮件?

问题描述:

我有一个表示为byte []的excel文档,我想将它作为附件发送到电子邮件中。如何使用System.Net.Mail将附件添加到电子邮件?

我在构建附件时遇到了一些麻烦。

我可以创建,其具有以下的构造的附件:此刻

(Stream contentStream, ContentType contentType) 
(Stream contentStream, string name) 
(Stream contentStream, string name, string mediaType) 

我的想法是创建从字节[]一个MemoryStream并将它传递给它创建附件的方法。

不幸的是我看不到从MemoryStream获取预期文件名和内容类型的方法,我也看不到如何提供正确的内容类型。有纯文本,Pdf,Rtf等选项,但没有我可以看到,立即跳出我作为一个我应该用于Excel文档。

我能找到最接近的是MediaTypeNames.Application.Octet其中指出:

该八位位组构件指定该 附件包含一般的二进制数据 。

然而,即使这是一个使用,除非它可以作为流的属性传递,那么我发送电子邮件的方法将只能发送一个字节[]作为Excel文档。

有没有我可以使用的其他类型的流?或者我将不得不创建自己的Stream类型,它具有我需要的细节。

肯定有人在那里已经做过这事,肯定微软会通过这个水平认为这....

任何帮助将非常感激。

更新: 请不要投票给任何使用以文件名作为字符串的构造函数的答案。我真的需要使用Stream的帮助......我想避免将文件写入磁盘,发送电子邮件,然后立即删除它。既然有一种方法可以让我这样做,我想尽可能地使用那个。

解决方案更新

康拉德设法找到了我一直在寻找!感谢堆人!

我只是记录了建议的解决方案,以防万一发生在提供的链接上的内容。

信用这个解决方案去www.systemnetmail.com

static void AttachmentFromStream() 
{ 

//create the mail message 
MailMessage mail = new MailMessage(); 

//set the addresses 
mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 

//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为字符串。我会尝试使用八位字节......但是如果我只是传入正式的MIME类型,那就失败了。

所有的事情都考虑到了,这是一个非常明显的解决方案......但我非常感谢解决它的帮助......好的是这个解决方案将被记录下来供将来的程序员遇到同样的问题。

再次感谢大家的帮助!

附件构造函数确实有一个构造函数来完成你所需要的。我假设你正在使用的System.Net.MailMessage类从.NET框架2.如果是这样read this link为你需要

---------------我猜我是错的,这是如果你有一个文件要附加---------------

它看起来像有一个与此附件发送邮件的例子:

http://www.aspnettutorials.com/tutorials/email/email-attach-aspnet2-csharp.aspx

我希望这是你在找什么。

+0

unfortunat这并不是因为该示例中使用的方法是传递文件名的字符串......这意味着文件在发送之前必须写入磁盘。我想在内存中执行所有操作,因为我没有理由将文件写入磁盘,发送它,然后删除文件。 – mezoid 2009-02-09 06:12:32

+1

对不起,您的答案投票约翰,但有人投了它,它不是正确答案。请,没有其他人投票支持这个答案......我需要使用构造函数来创建附件,这些构造函数使用Stream而不是字符串。 – mezoid 2009-02-09 06:43:23

Attachment构造函数中的name参数是将在收件人的电子邮件中显示的附件名称。

因此,您可以自由选择名称参数(首选扩展名为.xls),并将mediaType参数设置为“application/vnd.ms-excel”,这是为excel文件定义的MIME type

由于linkaccepted answer什么一些示例代码消失了,这里是从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("[email protected]"); 
    mail.To.Add("[email protected]"); 

    //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; 
}