Cpo Design

Testing emails by configuring email to go into directory

Pavel Svarc Tuesday, April 10, 2018 4:17 AM

While testing email behavior I do not want to send emails all the time. 

Here is a solution how we can configure application to store emails locally. Because this is standard behavior in framework the switch to using net configuration to point into smtp server is safe and can be done using XML transform when we are ready to use it.

The Solution

Update your web.config or app.config to the following

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

Or if you prefer you can use code for pick up

 

var smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
var emailPickupDirectory = HostingEnvironment.MapPath("~/EmailPickup");
if (!Directory.Exists(emailPickupDirectory)) { 
    Directory.CreateDirectory(emailPickupDirectory)
}
smtpClient.PickupDirectoryLocation = emailPickupDirectory;