Tuesday, January 4, 2011

Send a mail Using System.Net.Mail

Using System.Net.Mail; // include system.net.mail name space in your application

public class SendMail
{
   public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
   {
      MailMessage mMailMessage = new MailMessage();
      mMailMessage.From = new MailAddress(from);
      mMailMessage.To.Add(new MailAddress(to));
      if ((bcc != null) && (bcc != string.Empty))
      {
         mMailMessage.Bcc.Add(new MailAddress(bcc));
      }
      if ((cc != null) && (cc != string.Empty))
      {
         mMailMessage.CC.Add(new MailAddress(cc));
      }  
      mMailMessage.Subject = subject;
      mMailMessage.Body = body;
      mMailMessage.IsBodyHtml = true;
      mMailMessage.Priority = MailPriority.Normal;
      SmtpClient mSmtpClient = new SmtpClient();
      mSmtpClient.Send(mMailMessage);
   }
}
Call this function in Your Code Where required :
SendMail.SendMailMessage("fromAddress@yourdomain.com", "test@yahoo.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
Web.config :
<?xml version="1.0"?>
<configuration>
   <system.net>
      <mailSettings>
         <smtp from="defaultEmail@yourdomain.com">
            <network host="smtp.yourdomain.com" port="25" userName="yourUserName"  password="yourPassword"/>
         </smtp>
      </mailSettings>
   </system.net>
</configuration>

divyashree.k

No comments:

Post a Comment