Saving Emails to Disk in C#

Sending emails in C# is a simple task. All you need is a MailMessage object with your email and a SmtpClient to send it:

The configuration for the SmtpClient is inside the web.config or app.config can look like this example from Sanjay kumar:

Whenever your code is executed, your email will be sent using the specified mail server. While this is great for production, it’s a different story for testing your code. Do you really want to send all those emails?

 

A Different Delivery Method

The great benefit of using a configuration based approach with the mail configuration settings is that you can change the configuration in the app.config and don’t need to modify your code. If you switch the configuration from above with this one, then the SmtpClient will store the emails instead of sending them:

Attention: make sure that the pickup directory exists. Otherwise you will get an exception.

5 thoughts on “Saving Emails to Disk in C#”

    • Hi,
      Thanks for pointing that out. Without a remark in the code one will not find such an important info. It’s a bit disappointing that the issue in GitHub is still open and no clear answer was posted.

      Thanks again,
      Johnny

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.