简体   繁体   English

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

[英]Send eml files saved on disk

I am creating eml's and saving them to a directory using procedure mentioned over here . 我正在创建eml并使用此处提到的过程将它们保存到目录中。 I want to know how to send these eml files? 我想知道如何发送这些eml文件? 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. 我尝试使用SMTPClient类的对象,但它将MailMessage对象作为其参数,我无法找到并使用这些保存的eml文件创建MailMessage类型的对象。

Loading an EML file correctly is not as easy as it looks. 正确加载EML文件并不像看起来那么容易。 You can write an implementation working in 95% cases within few days. 您可以在几天内编写一个在95%的情况下工作的实现。 Remaining 5% would take at least several months ;-). 剩下的5%至少需要几个月;-)。 I know, becase I involved in developing one. 我知道,因为我参与了一个开发。

Consider following dificulities: 考虑以下困难:

  • unicode emails unicode电子邮件
  • right-to-left languages 从右到左的语言
  • correcting malformed EML files caused by well known errors in popular mail clients and servers 纠正由流行的邮件客户端和服务器中众所周知的错误导致的格式错误的EML文件
  • dealing with S/MIME (encrypted and signed email messages) 处理S / MIME(加密和签名的电子邮件)
  • dealing correctly with several methods of encoding attachments 正确处理几种编码附件的方法
  • dealing with inline images and stylesheets embedded into HTML emails 处理嵌入到HTML电子邮件中的内嵌图像和样式表
  • making sure that it parses correctly a MIME torture message from Mike Crispin (coauthor of Mime and IMAP RFCs) 确保它正确解析来自Mike Crispin(Mime和IMAP RFC的合着者)的MIME酷刑消息
  • 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. 现在,.NET Framework中没有包含此类解析器。 Until it changes I would sugest getting a thrid party MIME parser from an established vendor. 在它改变之前,我会从已建立的供应商处获取第三方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安全邮件组件 ,但我确信类似的任务也可以使用其他供应商的组件轻松复制。

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

Use EMLReader to retrieve data from .eml file. 使用EMLReader从.eml文件中检索数据。 It contains all the data you need to create a MailMessage object like From, To, Subject, Body & a whole lot more. 它包含创建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);

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: 如果你是一个微软商店并且还有一台Exchange服务器,那么还有另一种解决方案比这里建议的其他方法容易得多:

Each Exchange server has a pickup directory configured out of the box. 每个Exchange服务器都有一个开箱即用的拾取目录。
By default, it's %ExchangeInstallPath%TransportRoles\\Pickup . 默认情况下,它是%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: 阅读此TechNet文章以获取更多信息:
Pickup directory and Replay directory 分拣目录和重播目录

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文件内容转储到邮件服务器,然后让邮件服务器找出诸如from,to subject等内容。通过使用它的引擎解析电子邮件。

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 :) 我找到了一个干净的解决方案并在我的线程中覆盖它:)

How do i send an email when i already have it as a string? 当我已经将它作为字符串时,我如何发送电子邮件?

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

As others demonstrated, EML is just not a good way to serialize a mail message. 正如其他人所说,EML不是serialize邮件消息的好方法。 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. 虽然.Net框架中有几个序列化引擎可以序列化任何对象,但您也可以考虑将您的邮件组件(如地址,正文,要附加到base64中的文件)保存在您自己设计的Xml文件中。

Below is an example to get you started: 以下是一个让您入门的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <mail>
      <to display="Thomas Edison" address="tedison@domain.com" />
      <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. 你没有说出保存EML的理由。 If long term archival would be a goal, xml might have an advantage. 如果长期归档是一个目标,那么xml可能具有优势。

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@address.com

You can manipulate the mail header further if you require. 如果需要,您可以进一步操作邮件头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM