发送保存在磁盘上的 eml 文件
我正在创建 eml 并使用 此处。 我想知道如何发送这些 eml 文件? 我尝试使用 SMTPClient 类的对象,但它采用 MailMessage 对象作为其参数,并且我找不到使用这些保存的 eml 文件创建 MailMessage 类型的对象的方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正确加载 EML 文件并不像看起来那么容易。您可以在几天内编写一个适用于 95% 情况的实现。剩下的 5% 至少需要几个月的时间;-)。我知道,因为我参与了开发一个。
考虑以下困难:
这种解析器的成熟需要数年时间以及对其用户的持续反馈。目前 .NET Framework 中不包含这样的解析器。在它发生变化之前,我建议从成熟的供应商那里获取第三方 MIME 解析器。
以下代码使用我们的 Rebex Secure Mail 组件,但我确信类似的任务可以也可以使用其他供应商的组件轻松复制。
该代码基于邮件消息教程。
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.
Consider following dificulities:
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.
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.
The code is based on Mail Message tutorial.
使用 EMLReader 检索数据.eml 文件。它包含创建 MailMessage 对象所需的所有数据,例如发件人、收件人、主题、正文和邮件正文。还有很多。
Use EMLReader to retrieve data from .eml file. It contains all the data you need to create a MailMessage object like From, To, Subject, Body & a whole lot more.
如果您是 Microsoft 商店并且拥有 Exchange 服务器,那么还有另一种解决方案,它比此处建议的其他解决方案要简单得多:
每个 Exchange 服务器都有一个开箱即用配置的拾取目录。
默认情况下,它是
%ExchangeInstallPath%TransportRoles\Pickup
。您只需将
.eml
文件复制到该目录,Exchange 就会自动发送邮件。阅读这篇 TechNet 文章了解更多信息:
分拣目录和重播目录
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:
Each Exchange server has a pickup directory configured out of the box.
By default, it's
%ExchangeInstallPath%TransportRoles\Pickup
.You just copy the
.eml
files to that directory, and Exchange automatically will send the mails.Read this TechNet article for more information:
Pickup directory and Replay directory
正如其他人所证明的那样,EML 并不是
序列化
邮件消息的好方法。以其他格式保存邮件可能会更好。虽然 .Net 框架中有多个序列化引擎可以序列化任何对象,但您也可以考虑将邮件的组件(例如地址、正文、要以 Base64 附加的文件)保存在您自己设计的 Xml 文件中。下面是一个帮助您入门的示例:
另一个优点是,与创建 EML 不同,您不需要 smtpClient 来构建概念邮件文件。
在 C# 中创建和解析 Xml 非常容易。
你没有说出保存 EML 的理由。如果长期归档是一个目标,XML 可能具有优势。
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.Below is an example to get you started:
Added advantage would be that, unlike with creating EML, you do not need the smtpClient to build the concept mail files.
Xml is extremely easy to create and parse in C#.
You did not tell the rationale of saving EML's. If long term archival would be a goal, xml might have an advantage.
做我所做的...放弃。
构建 MailMessage 对象似乎是焦点,我在这里也有类似的问题未解决......
我如何发送一封电子邮件,当我已经将其作为字符串时?
据我所知,最简单的方法是使用原始套接字将整个 .eml 文件内容按原样转储到邮件服务器,然后让邮件服务器通过使用其引擎解析电子邮件来找出诸如发件人、主题等硬性内容。
唯一的问题... RFC 821 ... 太痛苦了,我试图找到一种干净的方法来做到这一点,并快速阅读邮箱中已有的邮件。
编辑:
我找到了一个干净的解决方案并将其覆盖在我的线程中:)
当我已经将电子邮件作为字符串时,如何发送电子邮件?
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?
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.
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.
EDIT:
I found a clean solution and covered it in my thread :)
How do i send an email when i already have it as a string?
您可以使用 Windows Server 的内置 SMTP 服务器来执行此操作,与之前使用 Exchange 的答案中的方式相同。
将 .eml 文件拖放到
C:\inetpub\mailroot\Pickup
中,原始邮件将被发送(本地或远程)。您只需在顶部插入一行即可转发邮件:
如果需要,您可以进一步操作邮件标头。
You can do this with Windows Server’s built-in SMTP server, the same way as in the previous answer using Exchange.
Drop the .eml file to
C:\inetpub\mailroot\Pickup
and the raw message will be sent (local or remote).You can forward messages by simply inserting a line in the top:
You can manipulate the mail header further if you require.
作为记录:
在 Nuget Packager Console 中写入:
然后在您的代码中:
For the records:
In Nuget Packager Console write:
Then in your Code: