Send eml files saved on disk

2019-01-24 10:33发布

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.

标签: c# email smtp eml
7条回答
戒情不戒烟
2楼-- · 2019-01-24 10:40

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

查看更多
再贱就再见
3楼-- · 2019-01-24 10:42

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 );
}
查看更多
神经病院院长
4楼-- · 2019-01-24 10:44

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

You can manipulate the mail header further if you require.

查看更多
甜甜的少女心
5楼-- · 2019-01-24 10:50

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);
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-24 10:52

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?

查看更多
乱世女痞
7楼-- · 2019-01-24 10:55

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="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.

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.

查看更多
登录 后发表回答