Thursday, 16 July 2015

Code to send email from Gmail in .net

        private void SendEmail(string emailBody)
        {
            try
            {
                MailMessage message = new MailMessage();

                //SmtpClient smtp = new SmtpClient();

                message.From = new MailAddress(ConfigurationManager.AppSettings["FROMEMAIL"].ToString());

                message.To.Add(new MailAddress(emailTo));

                message.CC.Add(new MailAddress(emailCC));

                message.Subject = emailSubject;

                message.Body = emailBody.ToString();

                message.IsBodyHtml = true;

                var client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"].ToString(), ConfigurationManager.AppSettings["FROMPWD"].ToString()),
                    EnableSsl = true
                };
                client.Send(message);
            }
            catch (Exception ex)
            {
                objLogWriter.LogWrite(ex.Message);
            }
        }

No comments:

Post a Comment