How to save MailMessage object to disk as *.eml or *.msg file

ashwnacharya picture ashwnacharya · Aug 12, 2009 · Viewed 83.1k times · Source

How do I save MailMessage object to the disk? The MailMessage object does not expose any Save() methods.

I dont have a problem if it saves in any format, *.eml or *.msg. Any idea how to do this?

Answer

Ryan Versaw picture Ryan Versaw · Aug 12, 2009

For simplicity, I'll just quote an explanation from a Connect item:

You can actually configure the SmtpClient to send emails to the file system instead of the network. You can do this programmatically using the following code:

SmtpClient client = new SmtpClient("mysmtphost");
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\somedirectory";
client.Send(message);

You can also set this up in your application configuration file like this:

 <configuration>
     <system.net>
         <mailSettings>
             <smtp deliveryMethod="SpecifiedPickupDirectory">
                 <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
             </smtp>
         </mailSettings>
     </system.net>
 </configuration>

After sending the email, you should see email files get added to the directory you specified. You can then have a separate process send out the email messages in batch mode.

You should be able to use the empty constructor instead of the one listed, as it won't be sending it anyway.