Sending Emails With Multiple Bcc and CC Using ASP.Net C#

Background

In this article we will learn how to send emails from an ASP.Net C# web application with Multiple CC and BCC. Let us see step-by-step so beginners also can understand the logic to send emails from any provider. There are many techniques to send emails from ASP.Net with multiple CC and BCC but in this article we will use a SMTP server with the Gmail provider to send the emails.

Prerequisites

  • Active internet connection.
  • Email id of any provider such as Gmail, Yahoo or your organization to send emails.

Let us create the application to send the emails from ASP.Net as:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty web site" (to avoid adding a master page).
  3. Provide the project a name, such as "SendingEmailsWithMultiCCBCC" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page and one class file.
  5. Drag and drop five Text Boxes, two Buttons and one Lable onto "default.aspx".
The "default.aspx" page design will be as in the following:

defaultpage.jpg

I hope you have created the .aspx page as explained above.

To send Emails in ASP.Net, the following namespaces must be added:

  1. using System.Net.Mail;  
  2. using System.Net;
To learn about the two namespaces above refer to the MSDN.

Now, move to the web.config file and add the email credentials in the app setting 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 app settings of the web.config file. After adding the credentials, the app setting section of the web.config file looks as in the following:

web config file source code

In the preceding app settings section, we are adding the sender email details credentials such as email id and word, so we can send the emails and later on we will read these values in the C# code.

Note

In the preceding app Settings section add your valid email and word so emails can be sent from your email id to others.
 
Now add the separate class file by right-clicking on the Solution Explorer, however this is not necessary since you can also write this code in the "default.aspx.cs" file, but for flexibility we are adding it in a separate code file.

 

Create the following method inside the SendEmail class to send email with CC and BCC.

  1. public static void Email_With_CCandBCC(String ToEmail,string cc,string bcc, String Subj, string Message)  
  2. {  
  3.     //Reading sender Email credential from web.config file  
  4.   
  5.     HostAdd = ConfigurationManager.AppSettings["Host"].ToString();  
  6.     FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();  
  7.         = ConfigurationManager.AppSettings["word"].ToString();  
  8.   
  9.     //creating the object of MailMessage  
  10.     MailMessage mailMessage = new MailMessage();  
  11.     mailMessage.From = new MailAddress(FromEmailid); //From Email Id  
  12.     mailMessage.Subject = Subj; //Subject of Email  
  13.     mailMessage.Body = Message; //body or message of Email  
  14.     mailMessage.IsBodyHtml = true;  
  15.   
  16.     string[] ToMuliId = ToEmail.Split(',');  
  17.     foreach (string ToEMailId in ToMuliId)  
  18.     {  
  19.         mailMessage.To.Add(new MailAddress(ToEMailId)); //adding multiple TO Email Id  
  20.     }  
  21.     string[] CCId = cc.Split(',');  
  22.     foreach (string CCEmail in CCId)  
  23.     {  
  24.         mailMessage.CC.Add(new MailAddress(CCEmail)); //Adding Multiple CC email Id  
  25.     }  
  26.   
  27.     string[] bccid = bcc.Split(',');  
  28.   
  29.     foreach (string bccEmailId in bccid)  
  30.     {  
  31.         mailMessage.Bcc.Add(new MailAddress(bccEmailId)); //Adding Multiple BCC email Id  
  32.     }  
  33.     SmtpClient smtp = new SmtpClient();  // creating object of smptpclient  
  34.     smtp.Host = HostAdd;              //host of emailaddress for example smtp.gmail.com etc  
  35.     //network and security related credentials  
  36.     smtp.EnableSsl = true;  
  37.     NetworkCredential NetworkCred = new NetworkCredential();  
  38.     NetworkCred.UserName = mailMessage.From.Address;  
  39.     NetworkCred.word = ;  
  40.     smtp.UseDefaultCredentials = true;  
  41.     smtp.Credentials = NetworkCred;  
  42.     smtp.Port = 587;  
  43.     smtp.Send(mailMessage); //sending Email  
  44. }
In the preceding class file, we have created one simple static function to send the emails that takes the following five parameters:
 
string ToEmail: recipient email id
string Subj: subject of the email
string Message: Email body or message
string cc: cc emailId
string bcc: bcc Email id 

I assume you have completed all the preceding steps.

Note

If you are adding multiple recipient To, CC and BCC Email Ids then add the multiple Email Ids separated by commas (,) because in the email function we are splitting the Email Ids with commas (,), If you want some other logic to add or split Email Ids then make the relevant changes in the Email function.

Now call the preceding static function on the button click of the "default.aspx.cs" page as in the following:

  1. protected void btnsend_Click(object sender, EventArgs e)  
  2. {  
  3.     toEmail = txttomail.Text;  
  4.     EmailSubj = Convert.ToString(txtsub.Text);  
  5.     EmailMsg = Convert.ToString(txtsub.Text);  
  6.     ccId = Convert.ToString(txtcc.Text);  
  7.     bccId = Convert.ToString(txtBCC.Text);  
  8.     //ing parameter to Email Method  
  9.     SendEmail.Email_With_CCandBCC(toEmail,ccId ,bccId ,EmailSubj, EmailMsg);  
  10. }
I assume you have done all the preceding steps, now run the application and add the email details with valid Email Addressees in the To, CC and BCC Email Id TextBox and click on the send button as:

UIEmail.jpg

From the preceding example we learned how to send emails with Multiple CC and BCC, I hope you have done it.

Note
  • For detailed code please download the Zip file attached above.
  • Don't forget to update the "Web.config" file for your email credential details.

Summary

We learned how to send emails in ASP.Net with Multiple CC and BCC. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.


Similar Articles