3

I would like to know how to programatically restart IIS 6.0 SMTP server.

The SMTP server I have setup crashes every now and then. I don't notice it for a couple days, but that is by far way too late to do anything about it.

I want to set up a scheduled task every 30 minutes or so to test if the SMTP server is running, and if its not, the Scheduled task with automatically start it back up.

I have found a way to check if the SMTP server is up and running, but I have not figured out how to restart the process if it crashes.

That way is posted here: Testing SMTP server is running via C#

Any help would be brilliant!

Thank you.

Im developing the Console application in C# to check if its running or not, so any code examples would be great too.

Community
  • 1
  • 1
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100

3 Answers3

4

A ServiceController can help you, as it has start and stop methods. Look at the sample in the msdn page.

Another sample is taken from the ServiceControllerStatus Enumeration is nearly what you need (just replace the service name).

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());
Steve B
  • 36,818
  • 21
  • 101
  • 174
2

Maybe I'm missing something, or something changed, but when you install SMTP service on Windows 2012R2, there is no dedicated service for it. So, for recent version of Windows the advice above won't work.

Luckily there's a way to do it much easier. Powershell:

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

The weirdest thing is that you control smtp service through AD, but it works. And of course this should be run elevated. If you have several virtual SMTP servers, you may need to identify your server by index or by some property (e.g. .ConnectionTimeout) first.

Mike Makarov
  • 1,287
  • 8
  • 17
  • Since the question is tagged "C#", is there a way to do this directly in C#? (Other than invoking a line of PowerShell from C#, of course, which I guess is fine, as anyone still using IIS 6.0 for anything probably has a high tolerance level for hacks :) ) – Florian Winter Jul 31 '17 at 15:55
  • 1
    Sorta. Take a look here: https://msdn.microsoft.com/en-us/library/aa746485(v=vs.85).aspx So the idea is, you `Type.GetTypeFromCLSID("001677D0-FD16-11CE-ABC4-02608C9E7553")` --> cast to dynamic --> do stuff. Here's interface definition: https://msdn.microsoft.com/en-us/library/aa705985(v=vs.85).aspx?f=255&MSPPError=-2147217396 or p/invoke: http://www.pinvoke.net/default.aspx/Interfaces.IADsContainer Still want to go this path? :) – Mike Makarov Aug 01 '17 at 12:59
0

in c# you can write:

            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();
            }
nicogis
  • 129
  • 3