Below class and its methods will help you in sending email alerts to the end users.

public class clsEmail
    {
        StringBuilder Strbuilder = new StringBuilder();
        StringBuilder obj1 = new StringBuilder();
        static string strMailFrom = System.Configuration.ConfigurationManager.AppSettings["mailFrom"];
        static string strMailTo = System.Configuration.ConfigurationManager.AppSettings["mailTo"];
        static string strMailCC = System.Configuration.ConfigurationManager.AppSettings["MailCC"];
        static string strMailBcc = System.Configuration.ConfigurationManager.AppSettings["MailBcc"];
        static string strSmtpServer = System.Configuration.ConfigurationManager.AppSettings["MailServer"];
        static string strUserName = System.Configuration.ConfigurationManager.AppSettings["mailUserName"];

        /// <summary>
        /// procedure used to send the mail notification to the end user.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="subject"></param>
        public static void SendNewCustomerEmail(string message, string subject)
        {
            string Style1 = "style=’font-family: Verdana;font-weight: normal;font-size: 11px;color: black’";
            StringBuilder Strbuilder = new StringBuilder();
            try
            {
                if (message != string.Empty)
                {
                    // Create the Objects
                    System.Net.Mail.MailMessage objMsg = new System.Net.Mail.MailMessage();
                    System.Net.Mail.SmtpClient SmtpMail = new SmtpClient(strSmtpServer,25);
                    SmtpMail.Credentials = new NetworkCredential("EMailAddress", "Password");         
                    MailAddress fromAddress = new MailAddress(strMailFrom, "Testing");
                    objMsg.To.Add(strMailTo);
                    objMsg.CC.Add(strMailCC);
                    objMsg.Bcc.Add(strMailBcc);
                    objMsg.From = fromAddress;
                    objMsg.Subject = subject;
                    Strbuilder.Append("<form><div " + Style1 + "> Hi,<Br><br> " + message + "</Div></form>");
                    Strbuilder.Append("<form><div " + Style1 + "> Regards,<Br> Dev Team </Div></form>");
                    objMsg.Body = Strbuilder.ToString();
                    objMsg.IsBodyHtml = true;
                    objMsg.Priority = MailPriority.High;
                    objMsg.BodyEncoding = System.Text.Encoding.UTF8;    
                    SmtpMail.Send(objMsg);
                }
                else
                {
                }
            }
            catch (SmtpException E)
            {
                AeSharedLibrary.clsMessage.Show("Error :" + E.Message.ToString() + " : " + E.InnerException.Message.ToString());
            }
            finally
            {

            }

        }

        /// <summary>
        /// procedure used to send the mail notification to the end user.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="subject"></param>
        public static void SendNewCustomerEmail(string EmailId, string message, string subject)
        {
            string Style1 = "style=’font-family: Verdana;font-weight: normal;font-size: 11px;color: black’";
            StringBuilder Strbuilder = new StringBuilder();
            string strSmtpServer = System.Configuration.ConfigurationManager.AppSettings["MailServer"];
            try
            {
                if (message != string.Empty)
                {
                    // Create the Objects
                    System.Net.Mail.MailMessage objMsg = new System.Net.Mail.MailMessage();
                    System.Net.Mail.SmtpClient SmtpMail = new SmtpClient(strSmtpServer, 25);
                    SmtpMail.Credentials = new NetworkCredential("EmailId", "Password");
                    MailAddress fromAddress = new MailAddress(EmailId, "Testing");
                    objMsg.To.Add(EmailId);
                    //objMsg.CC.Add(strMailCC);
                    //objMsg.Bcc.Add(strMailBcc);
                    objMsg.From = fromAddress;
                    objMsg.Subject = subject;
                    Strbuilder.Append("<form><div " + Style1 + "> Hi,<Br><br> " + message + "</Div></form>");
                    Strbuilder.Append("<form><div " + Style1 + "> Thnx,<Br> Dev Team </Div></form>");
                    objMsg.Body = Strbuilder.ToString();
                    objMsg.IsBodyHtml = true;
                    objMsg.Priority = MailPriority.High;
                    objMsg.BodyEncoding = System.Text.Encoding.UTF8;
                    SmtpMail.Send(objMsg);
                }
                else
                {
                }
            }
            catch (SmtpException E)
            {
                AeSharedLibrary.clsMessage.Show("Error :" + E.Message.ToString() + " : " + E.InnerException.Message.ToString());
            }
            finally
            {

            }
        }

        /// <summary>
        /// procedure used to send the mail notification to the end user.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="subject"></param>
        public static void SendNewCustomerEmail(string EmailId, string message, string subject, DataSet _dsLoginDetails)
        {
            string Style1 = "style=’font-family: Verdana;font-weight: normal;font-size: 11px;color: black’";
            StringBuilder Strbuilder = new StringBuilder();
            string strSmtpServer = System.Configuration.ConfigurationManager.AppSettings["MailServer"];
            try
            {
                if (message != string.Empty)
                {
                    // Create the Objects
                    System.Net.Mail.MailMessage objMsg = new System.Net.Mail.MailMessage();
                    System.Net.Mail.SmtpClient SmtpMail = new SmtpClient(strSmtpServer,25);
                    SmtpMail.Credentials = new NetworkCredential("EmailID", "Password");
                    MailAddress fromAddress = new MailAddress(EmailId, "Testing");
                    objMsg.To.Add(EmailId);
                    //objMsg.CC.Add(strMailCC);
                    //objMsg.Bcc.Add(strMailBcc);
                    objMsg.From = fromAddress;
                    objMsg.Subject = subject;
                    Strbuilder.Append("<form><div " + Style1 + "> Hi,<Br><br> " + message + "</Div></form>");
                    Strbuilder.Append("<form><div " + Style1 + "> Regards,<Br> Dev Team </Div></form>");
                    objMsg.Body = Strbuilder.ToString();
                    objMsg.IsBodyHtml = true;
                    objMsg.Priority = MailPriority.High;
                    objMsg.BodyEncoding = System.Text.Encoding.UTF8;
                    SmtpMail.Send(objMsg);
                }
                else
                {
                }
            }
            catch (SmtpException E)
            {
                AeSharedLibrary.clsMessage.Show("Error :" + E.Message.ToString() + " : " + E.InnerException.Message.ToString());
            }
            finally
            {

            }
        }

        /// <summary>
        /// procedure used to send the mail Notification to the end user.
        /// </summary>
        /// <param name="obj_h">Hashtable</param>
        /// <param name="subject">string</param>
        public static void SendNewCustomerEmail(Hashtable obj_h, string subject, string EmailId, string message)
        {
            StringBuilder Strbuilder = new StringBuilder(5000);
            StringBuilder obj1 = new StringBuilder();
            string Strvalue = "";

            string Style1 = "style=’font-family: Verdana;font-weight: normal;font-size: 11px;color:black’";

            string Style2 = "style=’text-align: left;font-family: Verdana;font-weight: normal;font-size: 11px;width: 150px;border: 1px #EFEFEF solid;border-collapse: collapse;border-spacing: 0px’";
            try
            {

                System.Net.Mail.MailMessage objMsg = new System.Net.Mail.MailMessage();
                System.Net.Mail.SmtpClient SmtpMail = new SmtpClient(strSmtpServer,25);
                SmtpMail.Credentials = new NetworkCredential("EmailID", "Password");
                MailAddress fromAddress = new MailAddress(EmailId, "Testing");
                objMsg.To.Add("EmailAddress");
                objMsg.From = fromAddress;
                objMsg.Subject = subject;
                IDictionaryEnumerator En = obj_h.GetEnumerator();
                Strbuilder.Append("<form><div " + Style1 + "> Hello,<Br><Br></Div></form>");
                Strbuilder.Append("<table border=1 width=60%" + Style2 + ">");
                while (En.MoveNext())
                {
                    if (En.Value.ToString().Trim() != "")
                    {
                        Strvalue = En.Value.ToString();
                    }
                    else
                    {
                        Strvalue = "-";
                    }
                    Strbuilder.Append("<tr><td " + Style2 + ">" + En.Key + "</td><td " + Style2 + ">" + Strvalue + "</td> </tr>");
                }
                Strbuilder.Append("</table>");
                Strbuilder.Append("<form><div " + Style1 + "> Regards,<Br> Dev Team </Div></form>");

                objMsg.Body = Strbuilder.ToString();
                obj_h.Clear();
                objMsg.IsBodyHtml = true;
                objMsg.Priority = MailPriority.High;
                objMsg.BodyEncoding = System.Text.Encoding.UTF8;               
                SmtpMail.Send(objMsg);

            }
            catch (SmtpException E)
            {
                AeSharedLibrary.clsMessage.Show("Error :" + E.Message.ToString() + " : " + E.InnerException.Message.ToString());
            }
            finally
            {

            }

        }

        /// <summary>
        /// procedure used to send the mail Notification to the end user.
        /// </summary>
        /// <param name="obj_h">Hashtable</param>
        /// <param name="subject">string</param>
        public static void SendNewCustomerEmail(DataTable dt)
        {
            StringBuilder Strbuilder = new StringBuilder(5000);
            StringBuilder obj1 = new StringBuilder();
            string Strvalue = "";
            string Style1 = "style=’font-family: Verdana;font-weight: normal;font-size: 11px;color:black’";          
            string Style2 = "style=’text-align: left;font-family: Verdana;font-weight: normal;font-size: 11px;width: 150px;border: 1px #EFEFEF solid;border-collapse: collapse;border-spacing: 0px’";
            try
            {

                System.Net.Mail.MailMessage objMsg = new System.Net.Mail.MailMessage();
                System.Net.Mail.SmtpClient SmtpMail = new SmtpClient(strSmtpServer,25);
                SmtpMail.Credentials = new NetworkCredential("EmailID", "Password");
                MailAddress fromAddress = new MailAddress(dt.Rows[0]["EmailFrom"].ToString(), "Testing");

                objMsg.To.Add(dt.Rows[0]["EmailAddTo"].ToString());
                objMsg.CC.Add(dt.Rows[0]["EmailAddCC"].ToString());
                objMsg.Bcc.Add(dt.Rows[0]["EmailAddBCC"].ToString());
                objMsg.From = fromAddress;
                Hashtable obj_h = new Hashtable();
                IDictionaryEnumerator En = obj_h.GetEnumerator();
                objMsg.Subject = dt.Rows[0]["EmailSubject"].ToString();              
                Strbuilder.Append("<form><div " + Style1 + "> Hello,<Br><Br></Div></form>");
                Strbuilder.Append("<table border=1 width=60%" + Style2 + ">");
                while (En.MoveNext())
                {
                    if (En.Value.ToString().Trim() != "")
                    {
                        Strvalue = En.Value.ToString();
                    }
                    else
                    {
                        Strvalue = "-";
                    }
                    Strbuilder.Append("<tr><td " + Style2 + ">" + En.Key + "</td><td " + Style2 + ">" + Strvalue + "</td> </tr>");
                }
                Strbuilder.Append("</table>");
                Strbuilder.Append("<form><div " + Style1 + "> Regards,<Br> Billing Team </Div></form>");

                objMsg.Body = dt.Rows[0]["EmailBody"].ToString();
                Attachment attach = new Attachment(dt.Rows[0]["EmailAttachments"].ToString());
                objMsg.Attachments.Add(attach);
                objMsg.IsBodyHtml = true;
                objMsg.Priority = MailPriority.High;
                objMsg.BodyEncoding = System.Text.Encoding.UTF8;
               
               
               
                SmtpMail.Send(objMsg);

            }
            catch (SmtpException E)
            {
                AeSharedLibrary.clsMessage.Show("Error :" + E.Message.ToString() + " : " + E.InnerException.Message.ToString());
            }
            finally
            {

            }

        }
    }

Advertisements