发送保存在磁盘上的 eml 文件

发布于 2024-08-02 08:27:42 字数 268 浏览 2 评论 0原文

我正在创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小…楫夜泊 2024-08-09 08:27:42

正确加载 EML 文件并不像看起来那么容易。您可以在几天内编写一个适用于 95% 情况的实现。剩下的 5% 至少需要几个月的时间;-)。我知道,因为我参与了开发一个。

考虑以下困难:

  • unicode 电子邮件
  • 从右到左的语言
  • 流行邮件客户端和服务器中众所周知的错误引起的格式错误的 EML 文件
  • 纠正由处理 S/MIME(加密和签名的电子邮件消息)的
  • 正确处理附件编码的几种方法
  • 处理内联嵌入到 HTML 电子邮件中的图像和样式表
  • ,确保它正确解析 MIME来自 Mike Crispin(Mime 和 IMAP RFC 的合著者)的折磨消息
  • 确保格式错误的消息不会导致缓冲区溢出或其他应用程序崩溃
  • 处理分层消息(带有附加消息的消息)
  • 确保它正确处理非常大的电子邮件

这种解析器的成熟需要数年时间以及对其用户的持续反馈。目前 .NET Framework 中不包含这样的解析器。在它发生变化之前,我建议从成熟的供应商那里获取第三方 MIME 解析器。

以下代码使用我们的 Rebex Secure Mail 组件,但我确信类似的任务可以也可以使用其他供应商的组件轻松复制。

该代码基于邮件消息教程

// 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");

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:

  • 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 message from 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

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.

// 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");
段念尘 2024-08-09 08:27:42

使用 EMLReader 检索数据.eml 文件。它包含创建 MailMessage 对象所需的所有数据,例如发件人、收件人、主题、正文和邮件正文。还有很多。

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);

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.

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);
兔姬 2024-08-09 08:27:42

如果您是 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

温柔一刀 2024-08-09 08:27:42

正如其他人所证明的那样,EML 并不是序列化邮件消息的好方法。以其他格式保存邮件可能会更好。虽然 .Net 框架中有多个序列化引擎可以序列化任何对象,但您也可以考虑将邮件的组件(例如地址、正文、要以 Base64 附加的文件)保存在您自己设计的 Xml 文件中。

下面是一个帮助您入门的示例:

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

另一个优点是,与创建 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:

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

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.

时光磨忆 2024-08-09 08:27:42

做我所做的...放弃。

构建 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?

嗫嚅 2024-08-09 08:27:42

您可以使用 Windows Server 的内置 SMTP 服务器来执行此操作,与之前使用 Exchange 的答案中的方式相同。

将 .eml 文件拖放到 C:\inetpub\mailroot\Pickup 中,原始邮件将被发送(本地或远程)。

您只需在顶部插入一行即可转发邮件:

To: [email protected]

如果需要,您可以进一步操作邮件标头。

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:

To: [email protected]

You can manipulate the mail header further if you require.

无声情话 2024-08-09 08:27:42

作为记录:

在 Nuget Packager Console 中写入:

Install-Package LumiSoft.Net.dll

然后在您的代码中:

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 );
}

For the records:

In Nuget Packager Console write:

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文