When sending emails via SMTP in .NET, developers seldom check to ensure the SMTP service is installed and running. Here’s some code to perform the check and start the service if it isn’t running:
//Check for SMTP Service Using System.ServiceProcessServiceController[] services = ServiceController.GetServices();bool bServiceInstalled= false;ServiceController smtpServ = null; // Loop through all the services and find the SMTP Service.foreach(ServiceController service in services){ if (service.ServiceName.ToLower() == "smtpsvc") { bServiceInstalled = true; smtpServ = service; break; }} if (!bServiceInstalled) { MessageBox.Show ("SMTP Service is not installed on this " + "machine.", "SMTPService",MessageBoxButtons.OK);}else { //Start if it is not running if (smtpServ != null) { if (smtpServ.Status != ServiceControllerStatus.Running) { MessageBox.Show("SMTP Service not running..." + "Starting Service..."); try smtpServ.Start(); catch(Exception ex) MessageBox.Show(ex.Message,"SMTP Service", MessageBoxButtons.OK); } }}
Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.























