How can I make email go to a local folder during testing?

时光怂恿深爱的人放手 提交于 2019-12-20 10:27:08

问题


How can I test sending email from my application without flooding my inbox?

Is there a way to tell IIS/ASP.NET how to deliver email to a local folder for inspection?


回答1:


Yes there is a way.

You can alter web.config like this so that when you send email it will instead be created as an .EML file in c:\LocalDir.

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

You can also create an instance of the SmtpClient class with these same settings, if you don't want to/can't change the web.config. In C# that looks something like this:

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



回答2:


Configure rules in your email client to move the messages based on the subject/sender's email address?



来源:https://stackoverflow.com/questions/177974/how-can-i-make-email-go-to-a-local-folder-during-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!