本文介绍了如何使用System.Net.Mail添加附件到电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel文件表示为一个字节[],我想把它作为附件发送到一封电子邮件。



我有一点构建附件的麻烦。



我可以创建一个附件,其中包含以下构造函数:

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
/ pre>

我的想法是从byte []创建一个MemoryStream,并将其传递给创建附件的方法。



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



最接近的是,其中指出:

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



是还有其他一些Stream可以使用?或者我必须创建自己的Stream类型,具有我需要的细节。



当然有人在此之前做过这个事情,当然,微软本来会想到这一点到这个级别....



任何帮助将不胜感激。



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



解决方案更新



康拉德设法找到我要找的东西!感谢堆人!



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



此解决方案的信用转到

  static void AttachmentFromStream()
{

//创建邮件
MailMessage mail = new MailMessage();

//设置地址
mail.From = new MailAddress(me@mycompany.com);
mail.To.Add(you@yourcompany.com);

//设置内容
mail.Subject =这是一封电子邮件;
mail.Body =这个内容在体内;

//获取一些二进制数据
byte [] data = GetData();

//将数据保存到内存流
MemoryStream ms = new MemoryStream(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类。如果这样,了解您需要的某些示例代码


I have an excel document represented as a byte[] and I'm wanting to send it as an attachment in an email.

I'm having a bit of trouble constructing the attachment.

I can create an Attachment which has the following constructors:

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

My idea at the moment is to create a MemoryStream from the byte[] and pass it to the method which creates the attachment.

Unfortunately I can't see a way to obtain the intended filename and content type from the MemoryStream and I also can't see how to supply the correct content type. There are options for plain text, Pdf, Rtf etc but none that I can see that immediately jump out at me as the one I should use for an Excel document.

The closest I can find is MediaTypeNames.Application.Octet which states:

However, even if this is the one to use, unless it can be passed as a property of the Stream then my method for sending emails will only be able to send a byte[] as an Excel document...

Is there perhaps some other sort of Stream I could use? Or will I have to create my own type of Stream that has the details I need.

Surely someone out there has done this thing before and surely Microsoft would have thought this through to this level....

Any help would be much appreciated.

Update:Please don't vote for any answers that use the constructors that take the filename as a string. I'm really needing help using the ones that take a Stream...I want to avoid having to write the file to disk, email it, and then immediately delete it. Since there is a method that allows me to do that I'd like to use that one if at all possible.

Solution Update

Conrad managed to find what I was looking for! Thanks heaps man!

I'll just document the suggested solution just in case something happens to the content at the supplied link.

Credit for this solution goes to 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);
}

In my case, it just means I'll have to change my method to take the filename and fileformat as strings. I'll try using the Octet one...but failing that I'll just pass in the official MIME type.

All things considered, this is a pretty obvious solution...but I do appreciate the help in solving it...and the good thing is this solution will be documented for future programmers who have the same problem.

Thanks again everyone for you help!

解决方案

The attachment constructor does indeed have a constructor that does what you need. I'm assuming you're using the System.Net.MailMessage class from .NET Framework 2. If so read this link for some sample code of what you need

这篇关于如何使用System.Net.Mail添加附件到电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:43
查看更多