Tuesday 9 April 2013

Send email

 public void SendMail(string fromId, string to, string subject, string body,string fromName)
        {
            try
            {
                string host = ConfigurationManager.AppSettings["SMTPHost"].ToString();
                string userName = ConfigurationManager.AppSettings["SMTPUserName"].ToString();
                string pwd = ConfigurationManager.AppSettings["SMTPPassword"].ToString();
                string ssl = ConfigurationManager.AppSettings["SSL"].ToString();
                int port = 25;
                int.TryParse(ConfigurationManager.AppSettings["SMTPPort"].ToString(), out port);

                MailMessage message = new MailMessage();
                SmtpClient Smtp = new SmtpClient();
               

                Smtp.Host = host;
                Smtp.Credentials = new System.Net.NetworkCredential(userName, pwd);
                Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                Smtp.Port = port;
                if (!string.IsNullOrEmpty(ssl) && ssl.ToLower().Equals("true"))
                {
                    Smtp.EnableSsl = true;
                }

                message.From = new MailAddress(fromId.Trim(),fromName);
                string[] toAddress = to.Split(',');
                foreach (object o in toAddress)
                {
                    message.To.Add(o.ToString().Trim());
                }
                message.Subject = subject;
                message.Body = body;

                message.IsBodyHtml = true;
                message.Priority = MailPriority.Normal;
                Smtp.Send(message);
            }
            catch
            {
                throw;
            }
        }

No comments:

Post a Comment