2016-01-06 28 views
0

有一個在ASP.NET MVC應用程序的郵件分揀目錄:如何自動創建電子郵件提貨目錄?

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]" deliveryMethod="SpecifiedPickupDirectory"> 
     <specifiedPickupDirectory pickupDirectoryLocation="C:/emails"/> 
     <network host="localhost"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

的Web應用程序無法在啓動時自動創建的目錄c:\emails。它可以很容易地設置它?或者我需要解析web.config並在啓動時創建「手動」目錄?

回答

2

我不認爲有什麼「開箱即可」做到這一點 - 但自己做這件事也很容易。

你可以使用這段代碼,你可以把它放在例如您的global.asax.cs文件採用其中一種適當方法(例如Application_Start):

// get the config section <system.net>/<mailSettings>/<smtp> 
SmtpSection smtpConfig = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection; 

if (smtpConfig != null) 
{ 
    // if you have DeliveryMethod=SpecifiedPickupDirectory 
    if(smtpConfig.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory) 
    { 
     // read out the configured directory 
     string pickupDir = smtpConfig.SpecifiedPickupDirectory.PickupDirectoryLocation; 

     // check if directory exists.... 
     if (!Directory.Exists(pickupDir)) 
     { 
      // and if not - just create it! 
      Directory.CreateDirectory(pickupDir); 
     } 
    } 
}