Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 435
ashwnacharya Avatar asked Aug 12 '09 07:08

ashwnacharya


1 Answers

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.

like image 174
Ryan Versaw Avatar answered Sep 23 '22 06:09

Ryan Versaw