Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Linkedin button
Delicious button
Digg button
Flickr button
Stumbleupon button
31 articles and counting
      

Sending email through C#.net

Module: CEmailManager
Calling function:

public static bool SendDummyEmail()
{
   return SendEmail(CGlobalParams.AdminEmail, "dummy", "Hi");
}

Worker function:

private static bool SendEmail(string p_strTo, string p_strSubject, string p_strBody)
{
   try
   {
      MailMessage objMessage = new MailMessage();
 
      string[] lstRecipient = p_strTo.Split(',');
      foreach (string strTo in lstRecipient)
      {
          objMessage.To.Add(strTo);
      }
 
      objMessage.From = new MailAddress(CGlobalParams.SMTPUser, CGlobalParams.EmailSenderDisplay);
      objMessage.Subject = p_strSubject;
      objMessage.Body = p_strBody;
 
      SmtpClient objClient = new SmtpClient(CGlobalParams.SMTPServer, CGlobalParams.SMTPPort);
      objClient.UseDefaultCredentials = false;
      objClient.Credentials = new System.Net.NetworkCredential(CGlobalParams.SMTPUser, CGlobalParams.SMTPPassword);
      objClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 
      if (CGlobalParams.SMTPRequireSSL)
      {
          objClient.EnableSsl = true;
      }
 
      objClient.Send(objMessage);
 
      return true;
   }
   catch
   {
     return false;
   }
}

Where

CGlobalParams: A class responsible for reading values for global parameters

Global parameters used here:

SMTPPassword    : somevalue
SMTPPort   : 587
SMTPRequireSSL    : true
SMTPServer    : smtp.gmail.com
SMTPUser    : jyotsnas@philogy.com

With currently set global parameters, the code sends email with gmail/ google apps over SSL. Setting the SMTPRequireSSL to false, will make the code send email with SMTP server which requires authentication but not SSL.


Comment / Leave a Review

  Name (required)
  Mail (will not be published) (required)



Parse error: syntax error, unexpected '<' in /home/philogy/public_html/wordpress/wp-content/themes/prettypress/statcode.php on line 7