In this article we will learn how to send emails from an ASP.Net C# web application with 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 CC and BCC but in this article we will use a SMTP server with the Gmail provider to send the emails.
Prerequisites
I hope you have created the .aspx page as explained above.
To send Emails in ASP.Net, the following namespaces must be added:
using System.Net.Mail;
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:
In the preceding app settings 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 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.cs class to send the email with CC and BCC .
public static void Email_With_CCandBCC(String ToEmail,string cc,string bcc, String Subj, string Message)
{
//Reading sender Email credential from web.config file
HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
= ConfigurationManager.AppSettings["word"].ToString();
//creating the object of MailMessage
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(FromEmailid); //From Email Id
mailMessage.Subject = Subj; //Subject of Email
mailMessage.Body = Message; //body or message of Email
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(ToEmail)); //receiver's TO Email Id
mailMessage.CC.Add(new MailAddress(cc)); //Adding CC email Id
mailMessage.Bcc.Add(new MailAddress(bcc)); //Adding BCC email Id
SmtpClient smtp = new SmtpClient(); // creating object of smptpclient
smtp.Host = HostAdd; //host of emailaddress for example smtp.gmail.com etc
//network and security related credentials
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = mailMessage.From.Address;
NetworkCred.word = ;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage); //sending Email
}
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
Now call the preceding static function on the button click of the "default.aspx.cs" page as in the following:
protected void btnsend_Click(object sender, EventArgs e)
{
toEmail = txttomail.Text;
EmailSubj = Convert.ToString(txtsub.Text);
EmailMsg = Convert.ToString(txtsub.Text);
ccId = Convert.ToString(txtcc.Text);
bccId = Convert.ToString(txtBCC.Text);
//ing parameter to Email Method
SendEmail.Email_With_CCandBCC(toEmail,ccId ,bccId ,EmailSubj, EmailMsg);
}
I hope you have done all the preceding steps, now run the application and add the email details with valid Email Adresses in To,CC, BCC and Email Id TextBoxes and click on the send button as:
From the preceding example we learned how to send emails with CC and BCC, I hope you have done it.
Note
We learned how to send emails in ASP.Net with 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.
with 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.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty web site" (to avoid adding a master page).
- Provide the project a name, such as "SendingEmailsWithCCBCC" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page and one class file.
- Drag and drop five Text Boxes and two buttons and one Lable onto "default.aspx".
I hope you have created the .aspx page as explained above.
To send Emails in ASP.Net, the following namespaces must be added:
using System.Net.Mail;
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:
In the preceding app settings 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 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.cs class to send the email with CC and BCC .
public static void Email_With_CCandBCC(String ToEmail,string cc,string bcc, String Subj, string Message)
{
//Reading sender Email credential from web.config file
HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
= ConfigurationManager.AppSettings["word"].ToString();
//creating the object of MailMessage
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(FromEmailid); //From Email Id
mailMessage.Subject = Subj; //Subject of Email
mailMessage.Body = Message; //body or message of Email
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(ToEmail)); //receiver's TO Email Id
mailMessage.CC.Add(new MailAddress(cc)); //Adding CC email Id
mailMessage.Bcc.Add(new MailAddress(bcc)); //Adding BCC email Id
SmtpClient smtp = new SmtpClient(); // creating object of smptpclient
smtp.Host = HostAdd; //host of emailaddress for example smtp.gmail.com etc
//network and security related credentials
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = mailMessage.From.Address;
NetworkCred.word = ;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage); //sending Email
}
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
Now call the preceding static function on the button click of the "default.aspx.cs" page as in the following:
protected void btnsend_Click(object sender, EventArgs e)
{
toEmail = txttomail.Text;
EmailSubj = Convert.ToString(txtsub.Text);
EmailMsg = Convert.ToString(txtsub.Text);
ccId = Convert.ToString(txtcc.Text);
bccId = Convert.ToString(txtBCC.Text);
//ing parameter to Email Method
SendEmail.Email_With_CCandBCC(toEmail,ccId ,bccId ,EmailSubj, EmailMsg);
}
I hope you have done all the preceding steps, now run the application and add the email details with valid Email Adresses in To,CC, BCC and Email Id TextBoxes and click on the send button as:
From the preceding example we learned how to send emails with CC and BCC, I hope you have done it.
Note
- Don't forget to update the "Web.config" file for your email credential details.
We learned how to send emails in ASP.Net with 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.
Post a Comment