find and solve ||
Please wait.....

Sending CC and BCC Emails in ASP.NET Core

Download source code from here
Sending  CC and BCC Emails in ASP.NET Core

In the article, I will explain how to send emails from an ASP.NET Core web application with CC and BCC. In the article, you can understand the logic to send emails from any provider. There are many different types of techniques to send emails from .NET 5  CC and BCC but I will use an SMTP server provider to send the emails.


Throughout this blog, I will use MailMessage class. This is a part of System.Net.Mail namespace and is used to create email messages that are sent to an SMTP server. Represents an email message that can be sent using the SmtpClient class. The subject, recipient, sender and body of an email message may be specified as parameters when a MailMessage is used to initialize a MailMessage object. 

Create New Application in visual studio 2019

  1. Open your visual studio 2019 less than 2019
  2. After that click on Create a New Project
  3. Search ASP.NET Core Web App(Model-View-Controller) and next
  4. Provide a Project name and confirm or change the Location and next
  5. Select the latest version of ASP.NET Core in the drop-down (.NET Core 5.0)and then select Authentication Type from drop-down
  6. Under Authentication Type, select Change and set the authentication to Individual User Accounts and then click on Create button

Now, move to the appsetting.jsong file and add the email credentials in the EmailConfiguration section so in the future, if your email credentials are changed then there is no need to change them in your code file since you can simply change the values in your EmailConfiguration section of the appsetting.json file. After adding the credentials, the EmailConfiguration section of the appsetting.json file looks as in the following:


  "EmailConfiguration": {
    "SmtpServer": "",
    "SmtpPort": 25,
    "SmtpUsername": "[email protected]",
    "SmtpPassword": "your-password-here",
    "Name": "your mail name", 
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
  "AllowedHosts": "*"

In the preceding EmailConfiguration section, we are adding the sender email detail credentials such as email id and word, so we can send the emails and later on, we will read these values in the ASP.NET Core with c#.

Read sender Email credential from appsetting.json file

There are many types to get appsetting.json key value in .net core. The appsettings.json file is an application configuration file used to store configuration settings such as key-value or database connections strings, any application scope global variables.

For Example, create anyName.cs file and paste the following code

using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace FindAndSolve
    static class ConfigurationManager
        public static IConfiguration AppSetting { get; }
        static ConfigurationManager()
        {             AppSetting = new ConfigurationBuilder()

Don't forget to Add References in your class or controller where you want to use like as given below.

using FindAndSolve;

string smtpServer= ConfigurationManager.AppSetting["EmailConfiguration:SmtpServer"];
string smtpPort= ConfigurationManager.AppSetting["EmailConfiguration:SmtpPort"];
string smtpUserName= ConfigurationManager.AppSetting["EmailConfiguration:SmtpUsername"];
string smtpPassword= ConfigurationManager.AppSetting["EmailConfiguration:SmtpPassword"];
string name= ConfigurationManager.AppSetting["EmailConfiguration:Name"];

for more details click here

After creating a new project, Next, I will design a view(index.cshtml) page where you will add the following HTML email template code as given below.

<div class="container">
    <form asp-action="SendMail" asp-controller="Home">
        <div class="bodycard">
            <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">To</label>
                <input type="email" name="TO" class="form-control" placeholder="Send To">
            <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">BCC</label>
                <input type="email" name="BCC" class="form-control" placeholder="BCC">
            <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">CC</label>
                <input type="email" name="CC" class="form-control" placeholder="CC">
            <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">Subject</label>
                <input type="email" name="Subject" class="form-control" placeholder="Subject">
            <div class="mb-3">
                <label for="exampleFormControlTextarea1" class="form-label">Message</label>
                <textarea class="form-control" name="Message" id="exampleFormControlTextarea1" rows="3"></textarea>
            <div class="mb-3">
               <input type="submit" value="Send Mail" class="btn btn-primary" />

Open your HomeController.cs controller and add this code as given below.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using SendingCCandBCCEmails.Models;
using System;
using System.Net;
using System.Net.Mail;
namespace SendingCCandBCCEmails.Controllers
    public class HomeController : Controller
    {         private readonly IConfiguration config;
        public HomeController(IConfiguration _config)
            config =_config;
        public IActionResult Index()
           return View();         }
        public IActionResult SendMail(EmailViewModel model)
            string smtpServer = config.GetSection("EmailConfiguration").GetSection("SmtpServer").Value;
            int smtpPort = Convert.ToInt16(config.GetSection("EmailConfiguration").GetSection("SmtpPort").Value);
            string smtpUsername = config.GetSection("EmailConfiguration").GetSection("SmtpUsername").Value;
            string smtpPassword = config.GetSection("EmailConfiguration").GetSection("SmtpPassword").Value;
            string name = config.GetSection("EmailConfiguration").GetSection("Name").Value;
            var message = new MailMessage();
            message.From = new MailAddress("[email protected]", name);
            //message.To.Add(new MailAddress(email));
            message.Subject = model.Subject;             message.IsBodyHtml = true;
            message.Body = model.Message;
            message.To.Add(new MailAddress(model.To));
            message.Bcc.Add(new MailAddress(model.BCC));
            message.CC.Add(new MailAddress(model.CC));
            using (var client = new System.Net.Mail.SmtpClient())
                client.UseDefaultCredentials = false;
                client.Host = smtpServer;                 client.Port = smtpPort;
                client.Credentials = new NetworkCredential()
                    UserName = smtpUsername,
                    Password = smtpPassword
                client.EnableSsl = false;
            return RedirectToAction("Index");

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials


Report Response