3

我想知道如何以编程方式重新启动 IIS 6.0 SMTP 服务器。

我设置的 SMTP 服务器不时崩溃。我有几天都没有注意到它,但现在做任何事情都为时已晚。

我想每 30 分钟左右设置一个计划任务来测试 SMTP 服务器是否正在运行,如果没有,计划任务会自动启动它。

我找到了一种方法来检查 SMTP 服务器是否已启动并正在运行,但我还没有弄清楚如果它崩溃了如何重新启动该进程。

这种方式发布在这里:Testing SMTP server is running via C#

任何帮助都会很棒!

谢谢你。

我在 C# 中开发控制台应用程序以检查它是否正在运行,所以任何代码示例也会很棒。

4

3 回答 3

4

ServiceController可以帮助您,因为它具有启动和停止方法。查看 msdn 页面中的示例。

另一个示例取自ServiceControllerStatus 枚举几乎是您需要的(只需替换服务名称)。

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());
于 2012-07-16T21:49:04.113 回答
2

也许我遗漏了一些东西,或者改变了一些东西,但是当你在Windows 2012R2上安装 SMTP 服务时,没有专门的服务。因此,对于最新版本的 Windows,上述建议将不起作用。

幸运的是,有一种方法可以轻松得多。电源外壳:

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

最奇怪的是,你通过AD控制smtp服务,但它确实有效。当然,这应该被提升。如果您有多个虚拟 SMTP 服务器,您可能需要首先通过索引或某些属性(例如.ConnectionTimeout)来识别您的服务器。

于 2016-05-25T09:03:59.837 回答
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();
            }
于 2017-12-05T15:20:00.083 回答