C# 发送保存在磁盘上的 eml 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1306115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send eml files saved on disk
提问by UJ.
I am creating eml's and saving them to a directory using procedure mentioned over here. I want to know how to send these eml files? I tried using SMTPClient class's object but it takes MailMessage object as its parameter and I couldn't find and way to create an object of type MailMessage using these saved eml files.
我正在创建 eml 并使用这里提到的过程将它们保存到一个目录中。我想知道如何发送这些eml文件?我尝试使用 SMTPClient 类的对象,但它将 MailMessage 对象作为其参数,我找不到使用这些保存的 eml 文件创建 MailMessage 类型对象的方法。
回答by HuBeZa
Use EMLReaderto retrieve data from .eml file. It contains all the data you need to create a MailMessageobject like From, To, Subject, Body & a whole lot more.
使用EMLReader从 .eml 文件中检索数据。它包含创建MailMessage对象(如 From、To、Subject、Body 等)所需的所有数据。
FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
EMLReader reader = new EMLReader(fs);
fs.Close();
MailMessage message = new System.Net.Mail.MailMessage(reader.From, reader.To, reader.Subject, reader.Body);
回答by Martin Vobr
Loading an EML file correctly is not as easy as it looks. You can write an implementation working in 95% cases within few days. Remaining 5% would take at least several months ;-). I know, becase I involved in developing one.
正确加载 EML 文件并不像看起来那么容易。您可以在几天内编写一个适用于 95% 情况的实现。剩下的 5% 至少需要几个月的时间;-)。我知道,因为我参与了开发。
Consider following dificulities:
考虑以下困难:
- unicode emails
- right-to-left languages
- correcting malformed EML files caused by well known errors in popular mail clients and servers
- dealing with S/MIME (encrypted and signed email messages)
- dealing correctly with several methods of encoding attachments
- dealing with inline images and stylesheets embedded into HTML emails
- making sure that it parses correctly a MIME torture messagefrom Mike Crispin (coauthor of Mime and IMAP RFCs)
- making sure that malformed message will not result in buffer overun or other application crash
- handling hierarchical messages (message with attached messages)
- making sure that it handles correctly very big emails
- Unicode 电子邮件
- 从右到左的语言
- 纠正由流行邮件客户端和服务器中众所周知的错误引起的畸形 EML 文件
- 处理 S/MIME(加密和签名的电子邮件)
- 正确处理几种编码附件的方法
- 处理嵌入到 HTML 电子邮件中的内嵌图像和样式表
- 确保它正确解析来自 Mike Crispin(Mime 和 IMAP RFC 的合著者)的MIME 酷刑消息
- 确保格式错误的消息不会导致缓冲区溢出或其他应用程序崩溃
- 处理分层消息(带有附加消息的消息)
- 确保它正确处理非常大的电子邮件
Maturing of such parser takes years and continuous feedback for it's users. Right now is no such parser included in the .NET Framework. Until it changes I would sugest getting a thrid party MIME parser from an established vendor.
这种解析器的成熟需要数年时间并为其用户提供持续的反馈。目前 .NET Framework 中不包含此类解析器。在它改变之前,我会建议从已建立的供应商那里获得第三方 MIME 解析器。
Following code uses our Rebex Secure Mail component, but I'm sure that similar task could be replicated easily with components from other vendors as well.
以下代码使用我们的Rebex Secure Mail 组件,但我确信使用其他供应商的组件也可以轻松复制类似的任务。
The code is based on Mail Message tutorial.
该代码基于邮件消息教程。
// create an instance of MailMessage
MailMessage message = new MailMessage();
// load the message from a local disk file
message.Load("c:\message.eml");
// send message
Smtp.Send(message, "smtp.example.org");
回答by War
Do What i did ... give up.
做我所做的......放弃。
Building the MailMessage object seems to be the focus i have a similar questions outstanding on here too ... How do i send an email when i already have it as a string?
构建 MailMessage 对象似乎是重点,我在这里也有一个类似的问题...... 当我已经将它作为字符串发送时,我如何发送电子邮件?
From what i've seen the simplest way to do this is to use a raw socket to dump the entire .eml file contents up to the mail server as is and let the mail server figure out the hard stuff like from, to subject, ect by parsing the email using it's engine.
从我所看到的最简单的方法是使用原始套接字将整个 .eml 文件内容按原样转储到邮件服务器,并让邮件服务器找出诸如从、到主题等困难的内容通过使用它的引擎解析电子邮件。
The only problem ... RFC 821 ... such a pain, i'm trying to figure out a clean way to do this and read mail already in the mailbox quickly too.
唯一的问题... RFC 821 ... 太痛苦了,我正试图找出一种干净的方法来做到这一点,并快速阅读邮箱中已有的邮件。
EDIT:
编辑:
I found a clean solution and covered it in my thread :)
我找到了一个干净的解决方案并在我的线程中进行了介绍:)
回答by mmaty
For the records:
对于记录:
In Nuget Packager Console write:
在 Nuget Packager Console 中写入:
Install-Package LumiSoft.Net.dll
Then in your Code:
然后在您的代码中:
using (FileStream fs = new FileStream( cacheFileName, FileMode.Open, FileAccess.Read ))
using (LumiSoft.Net.SMTP.Client.SMTP_Client client =
new LumiSoft.Net.SMTP.Client.SMTP_Client())
{
client.SendMessage( fs );
}
回答by Christian Specht
If you're a Microsoft shop and have an Exchange server anyway, then there's another solution which is much, much easier than everything else suggested here:
如果您是 Microsoft 商店并且无论如何都拥有 Exchange 服务器,那么还有另一种解决方案,它比此处建议的所有其他方法要容易得多:
Each Exchange server has a pickup directory configured out of the box.
By default, it's %ExchangeInstallPath%TransportRoles\Pickup
.
每个 Exchange 服务器都有一个现成配置的拾取目录。
默认情况下,它是%ExchangeInstallPath%TransportRoles\Pickup
.
You just copy the .eml
files to that directory, and Exchange automatically will send the mails.
您只需将.eml
文件复制到该目录,Exchange 就会自动发送邮件。
Read this TechNet article for more information:
Pickup directory and Replay directory
阅读这篇 TechNet 文章以了解更多信息:
Pickup directory 和 Replay directory
回答by Roland
As others demonstrated, EML is just not a good way to serialize
a mail message. You might be better off by saving your mails in another format. While there are several serialization engines in the .Net framework to serialize any object, you might also consider just saving the components of your mails, like addresses, body, files to be attached in base64, in an Xml file of your own design.
正如其他人所证明的那样,EML 并不是serialize
邮件消息的好方法。以另一种格式保存邮件可能会更好。虽然 .Net 框架中有几个序列化引擎可以序列化任何对象,但您也可以考虑将邮件的组件(如地址、正文、要附加在 base64 中的文件)保存在您自己设计的 Xml 文件中。
Below is an example to get you started:
以下是帮助您入门的示例:
<?xml version="1.0" encoding="utf-8"?>
<mail>
<to display="Thomas Edison" address="[email protected]" />
<body>
Hi Thomas,
How are you doing?
Bye
</body>
<attachment name="MaryLamb.wav">
cmF0aWUgYWFuIGluIFBERi1mb3JtYWF0LiBEZSBmYWN0dXVyIGlzIGVlbiBvZmZpY2ll
ZWwgZ2VzaWduZWVyZA0KZG9jdW1lbnQgdmFuIEV1cm9maW5zIE9tZWdhbSBCVi4gRGUg
c2lnbmF0dXJlIGt1bnQgdSB2ZXJpZmnDq3Jlbi4NCg0KVm9vciBoZXQgdmVyaWZpw6ty
...
</attachment>
</mail>
Added advantage would be that, unlike with creating EML, you do not need the smtpClient to build the concept mail files.
附加的优点是,与创建 EML 不同,您不需要 smtpClient 来构建概念邮件文件。
Xml is extremely easy to create and parse in C#.
Xml 在 C# 中非常容易创建和解析。
You did not tell the rationale of saving EML's. If long term archival would be a goal, xml might have an advantage.
您没有说明保存 EML 的理由。如果将长期存档作为目标,则 xml 可能具有优势。
回答by spludlow
You can do this with Windows Server's built-in SMTP server, the same way as in the previous answer using Exchange.
您可以使用 Windows Server 的内置 SMTP 服务器执行此操作,与使用 Exchange 的上一个答案相同。
Drop the .eml file to C:\inetpub\mailroot\Pickup
and the raw message will be sent (local or remote).
将 .eml 文件拖放到C:\inetpub\mailroot\Pickup
,原始消息将被发送(本地或远程)。
You can forward messages by simply inserting a line in the top:
您只需在顶部插入一行即可转发消息:
To: [email protected]
You can manipulate the mail header further if you require.
如果需要,您可以进一步操作邮件标题。