Here in this blog I will tell you that how to send mail to users using SmtpClient and MailMessage class. Here I have created a small function which is used to send mail. One thing important that before using this method your application should be hosted on some webserver.

/// <summary>
        /// This method will be used to send mail to client
        /// by using Smtp and MailMessage class.
        /// </summary>
        /// <param name="sFrom"></param>
        /// <param name="sTo"></param>
        /// <param name="sBcc"></param>
        /// <param name="sCC"></param>
        /// <param name="sSubject"></param>
        /// <param name="sBody"></param>
        public static void sendMail(string sFrom, string sTo, string sBcc, string sCC, string sSubject, string sBody)
        {
            MailMessage message = new MailMessage();          //Here we will create object of MailMessage class.
            message.From = new MailAddress(sFrom);       //Initilize From in mail address.
            message.To.Add(new MailAddress(sTo));        //Initilize To in mail address.
           
            if (!string.IsNullOrEmpty(sBcc))           //Check whether sBcc is not empty.
            {
                message.Bcc.Add(new MailAddress(sBcc));    //Add sBcc in mail address.
            }
            if (!string.IsNullOrEmpty(sCC))          //Check whether sCC is not empty.
            {
                message.CC.Add(new MailAddress(sCC));      //Add CC in mail address.
            }
            message.Subject = sSubject;        //Add subject in mail message.
            message.Body = sBody;            //Add body in mail message.
            message.IsBodyHtml = true;             
            message.Priority = MailPriority.High;   //Set priority of mail message.
            SmtpClient client = new SmtpClient();                  //Create an object of Smtp client.
            client.Send(message);            //Send message by using send() method.
        }

  Modified On Sep-18-2014 01:23:57 PM

Leave Comment