• Skip to primary navigation
  • Skip to main content

Nikhil Mittal

|| Software Developer || App Developer ||

  • Home
  • About Me
  • Blog
  • Testimonials
  • Achievements
  • Portfolio
  • Interview Q&As
    • Angular – QA Series 1
    • Angular – QA Series 2
    • Angular – QA Series 3
    • Angular – QA Series 4
    • Convert Angular App to Mobile APK
    • C# TYPICAL QUESTIONS – SERIES
    • Advanced Q & A – Series 1
    • Advanced Q&A – Series 2
    • Advanced Q&A Series – 3
  • Free Video Tutorials
    • ASP.NET C#
    • C# Tutorial – In Hindi
    • C# Tutorial – English
    • C#-Advanced
    • MVC DOT NET 4.8
    • Web API Dot Net Core 3.1
    • Azure DevOps
    • HTML/CSS – In Hindi
    • SQL Developer
    • Angular 10+
    • Corporate Skills
  • Consulting
  • Contact Us

Email Send Code in C#

To Send an Email using SMTP Credentials you can use below code to send effectively.

For Email Providers such has GMAIL you need to “Allow Less Secure Apps” from your Account to send email without failure.

Below is a working and tested code in C#.

          
          System.Net.Mail.MailMessage emailMsg = new MailMessage();
          emailMsg.Subject = Subject;
          emailMsg.Body = Body;
          emailMsg.From = ConfigurationManager.AppSettings["FromEMail"];
          emailMsg.To.Add(ToemailID);
          emailMsg.Bcc.Add(bccemailID);
                    emailMsg.CC.Add(ccemailID);

            emailMsg.IsBodyHtml = true;

    emailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = ConfigurationManager.AppSettings["smtpHost"];
            smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
            smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUserId"], ConfigurationManager.AppSettings["smtpPassword"]);
            smtpClient.EnableSsl = true;
       ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;

            try
            {
                smtpClient.Send(emailMsg);
                Response = "Email sent";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
Visits: 16993
  • Home
  • About Me
  • You Tube
  • LinkedIn