Wednesday, 9 April 2014

send mail using C#

 {
        string status = "";
        try
        {
            if (!string.IsNullOrEmpty(ToEmail))
            {
                ToEmail = ConfigurationManager.AppSettings["toMailId"].ToString();
            }
            string MailSenderEmail = ConfigurationManager.AppSettings["SenderEmail"].ToString();
            string MailSenderPwd = ConfigurationManager.AppSettings["SenderPwd"].ToString();
            string smtpInfo = ConfigurationManager.AppSettings["smtpInfo"].ToString();
            string Port = ConfigurationManager.AppSettings["Port"].ToString();

            MailMessage mail = new MailMessage();
            mail.Subject = subj;
            mail.From = new MailAddress(MailSenderEmail);
            mail.To.Add(ToEmail);
            mail.Body = bodymsg;
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(smtpInfo, Util.ToInt32(Port));
            smtp.EnableSsl = true;
            NetworkCredential netCre = new NetworkCredential(MailSenderEmail, MailSenderPwd);
            smtp.Credentials = netCre;
            smtp.Send(mail);
            status = "1";
        }
        catch (Exception e)
        {
            e.Message.ToString();
            status = e.Message.ToString();
        }
        return status;
    }

No comments:

Post a Comment