2012-07-16 50 views
3

我想知道如何以編程方式重新啓動IIS 6.0 SMTP服務器。如何以編程方式啓動IIS 6.0 SMTP虛擬服務器?

我安裝的SMTP服務器偶爾會崩潰。我幾天沒有注意到這一點,但對於做這件事來說,已經太遲了。

我想每隔30分鐘設置一次計劃任務,以測試SMTP服務器是否正在運行,如果不是,計劃任務會自動啓動備份。

我找到了一種方法來檢查SMTP服務器是否啓動並正在運行,但是我還沒有想出如何在程序崩潰時重新啓動進程。

這樣被張貼在這裏:Testing SMTP server is running via C#

任何幫助將是輝煌!

謝謝。我開發C#中的控制檯應用程序來檢查它是否運行,所以任何代碼示例都會很好。

+0

「我已經找到一種方法來檢查,如果SMTP服務器啓動並運行」。哪一個?可能答案並不是很遠 – 2012-07-16 21:21:00

+0

已更新此評論的問題。 – 2012-07-16 21:23:20

+0

你想遠程或本地檢查嗎?你想從同一個域名進行測試嗎? – 2012-07-16 21:24:28

回答

4

A ServiceController可以幫助你,因爲它有啓動和停止方法。看看msdn頁面中的示例。

另一個樣本取自ServiceControllerStatus Enumeration幾乎是你所需要的(只需更換服務名稱)。

ServiceController sc = new ServiceController("Telnet"); 
Console.WriteLine("The Telnet service status is currently set to {0}", 
        sc.Status.ToString()); 

if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || 
    (sc.Status.Equals(ServiceControllerStatus.StopPending))) 
{ 
    // Start the service if the current status is stopped. 

    Console.WriteLine("Starting the Telnet service..."); 
    sc.Start(); 
} 
else 
{ 
    // Stop the service if its status is not set to "Stopped". 

    Console.WriteLine("Stopping the Telnet service..."); 
    sc.Stop(); 
} 

// Refresh and display the current service status. 
sc.Refresh(); 
Console.WriteLine("The Telnet service status is now set to {0}.", 
        sc.Status.ToString()); 
2

也許我失去了一些東西,或者什麼改變,但是當你在的Windows 2012R2安裝SMTP服務,有沒有爲它專門服務。因此,對於最新版本的Windows,上述建議將無法使用。

幸運的是,有一種方法可以輕鬆完成。 Powershell:

([ADSI]'IIS://LOCALHOST/SMTPSVC/1').Start() #to start 
([ADSI]'IIS://LOCALHOST/SMTPSVC/1').Stop() #to ... you guess 

最奇怪的是你通過AD控制smtp服務,但它的工作原理。 當然這應該運行提升。 如果您有多個虛擬SMTP服務器,則可能需要先通過索引或某些屬性(例如.ConnectionTimeout)來識別您的服務器。

+0

由於問題被標記爲「C#」,有沒有辦法直接在C#中做到這一點? (除了從C#調用一行PowerShell,當然,我認爲這很好,因爲任何人仍然使用IIS 6.0的任何東西可能具有很高的公差級別:)) – 2017-07-31 15:55:31

+1

Sorta。看看這裏:https://msdn.microsoft.com/en-us/library/aa746485(v=vs.85).aspx所以這個想法是,你'Type.GetTypeFromCLSID(「001677D0-FD16-11CE-ABC4 -02608C9E7553「)' - >投射到動態 - >做東西。以下是界面定義:https://msdn.microsoft.com/en-us/library/aa705985(v=vs.85).aspx?f=255&MSPPError=-2147217396或p/invoke:http://www.pinvoke。 net/default.aspx/Interfaces.IADsContainer還想走這條路嗎? :) – 2017-08-01 12:59:00

0

在C#中,你可以這樣寫:

  enum StatusVirtualServerSMTP 
      { 
       Started = 2, 
       Stopped = 4 
      } 

      DirectoryEntry dir = new DirectoryEntry("IIS://localhost/SMTPSVC/1"); 

      if (Convert.ToInt32(dir.Properties["SERVERSTATE"].Value) == (int)StatusVirtualServerSMTP.Stopped) 
      { 
       dir.Properties["SERVERSTATE"].Value = (int)StatusVirtualServerSMTP.Started; 
       dir.CommitChanges(); 
      }