Thomas
Thomas

Reputation: 34188

Drop e-mails into a specified folder instead of sending them via SMTP server c#

i was reading an article from this link http://sampathloku.blogspot.in/2010/12/how-to-check-email-functionality.html about How to check Email functionality without using SMTP Server and there i have seen that email is not sent rather saved in local folder. so i like to know why anyone would like to saved mail in local pc folder instead of sending it.

can anyone explain the right situation when people save mail in folder and also tell me how to sent that saved mail later. thanks

Upvotes: 1

Views: 1141

Answers (2)

Stefan
Stefan

Reputation: 17658

Its for testing purposes. update: see @Christian Specht answer for other usages.

Imagine you want to 'auto-generate' an email. It's easier to save it to disk than actual send it, receive it etc. This way you can just open it from your hard drive to check the contents. Or even better; run unit tests on it.

Upvotes: 2

Christian Specht
Christian Specht

Reputation: 36431

It's not just for testing.
We're using it in production for two reasons: performance and availability.

If you have an Exchange server, it's very easy to send those saved mails later by just copying the .eml files to a certain directory on the Exchange server.

Why would you want to do this?
Because the default way (using the SmtpClient class to send the mail directly to the SMTP server) has two disadvantages:

  • Your app has to establish a connection to the SMTP server (which takes time) for each and every single mail
  • if the SMTP server is offline, it doesn't work

We do have an Exchange server, so we're sending mails by having our application server save them as .eml files in a local directory.
(fortunately, we had a central "send mail" method instead of directly using SmtpClient in a bazillion places, so we just had to change a few lines in that single method to make sure all mails go to the local directory)

That's all as far as our application server is concerned.
For our application server, "sending" a mail is faster now because it's just a local write. Plus, it works the same in case the Exchange server is offline.


So, what happens after the .eml files are saved in the app server's local directory?

We have a (separate) simple C# app which runs each minute via Windows Task Scheduler and does the following:

  • check if the Exchange server's pickup directory exists and is writable
  • if yes, move all .eml files from the local directory to the Exchange pickup directory

Upvotes: 1

Related Questions