如何添加附件使用System.Net.Mail的电子邮件? [英] How do I add an attachment to an email using System.Net.Mail?

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

问题描述

我有psented为一个byte []的Excel文档重新$ P $,我想送它作为电子邮件的附件。

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)

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

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

不幸的是我看不到的方式来获得从MemoryStream的预期的文件名和内容类型,我也不能看到如何提供正确的内容类型。有纯文本,PDF,RTF等,但没有选择,我可以看到,马上在我跳出对视了一眼,我应该为一个Excel文档使用。

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.

我能找到的最接近的是<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.mime.mediatypenames.application.octet.aspx\">MediaTypeNames.Application.Octet其中规定:

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

的八位成员指定的
  附件包含通用二进制
  数据。

The Octet member designates that the attachment contains generic binary data.

然而,即使是使用的人,除非它可以作为流的财产传给我的,然后用于发送电子邮件的方法将只能发送一个字节[]作为一个Excel文档...

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...

有可能还有一些其他类型的流,我可以使用?或将我要创造我自己的Stream类型有我需要的信息。

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....

任何帮助将是非常美联社preciated。

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.

解决方案更新

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

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.

信贷这个解决方案去 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为字符串。我会尝试使用八位... ...不过做不到这一点我就通过在官方的MIME类型。

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.

所有的事情考虑,这是一个pretty显而易见的解决办法......但我做AP preciate帮助解决它...好事是该解决方案将被记录谁拥有未来的程序员同样的问题。

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.

再次感谢大家的帮助!

推荐答案

附件构造确实有一个构造函数,做你所需要的。我假设你正在使用的.NET框架2. System.Net.MailMessage类如果是这样阅读此链接了解你需要什么。

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的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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