Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C#
Expertise: Advanced
Aug 14, 2007

Send Emails Through SMTP in .NET

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.ServiceProcess
ServiceController[] 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);
      }
   }
}
Srinath MS
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date