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

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

问题描述

我有一个用 byte[] 表示的 excel 文档,我想将它作为电子邮件的附件发送.

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

我可以创建一个具有以下构造函数的附件:

(Stream contentStream, ContentType contentType)(流内容流,字符串名称)(流内容流,字符串名称,字符串媒体类型)

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

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

我能找到的最接近的是 MediaTypeNames.Application.Octet 声明:

<块引用>

Octet 成员指定附件包含通用二进制文件数据.

但是,即使这是要使用的,除非它可以作为 Stream 的属性传递,否则我发送电子邮件的方法只能将字节 [] 作为 Excel 文档发送...

也许我可以使用其他类型的流?或者我是否必须创建自己的流类型,其中包含我需要的详细信息.

肯定有人以前做过这件事,而且微软肯定会考虑到这个程度......

任何帮助将不胜感激.

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

解决方案更新

康拉德设法找到了我要找的东西!谢谢大佬!

我只会记录建议的解决方案,以防万一提供的链接中的内容发生问题.

此解决方案归功于 www.systemnetmail.com

static void AttachmentFromStream(){//创建邮件消息MailMessage 邮件 = new MailMessage();//设置地址mail.From = new MailAddress("me@mycompany.com");mail.To.Add("you@yourcompany.com");//设置内容mail.Subject = "这是一封电子邮件";mail.Body = "此内容在正文中";//获取一些二进制数据byte[] 数据 = 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);}

就我而言,这只是意味着我必须更改我的方法以将文件名和文件格式作为字符串.我将尝试使用八位字节...但如果失败,我将只传递官方 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:

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

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

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