Send Email with CC and BCC using ASP.NET 4.0 and C#
Sending a email with CCs and BCCs using ASP.NET 4.0 and C# is actually very simple and easy to accomplish.
First, you will need to import the System.Net.Mail namespace.
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and specify the CCs.
The front end .aspx page will contain a table to hold all of our input forms. These forms will accept the variables we will eventually use in our code behind. We have placed the code in a table to be able to easily have our form line up and access the information.
We use the btnSubmit_Click event to do the work. To access the event, go to design view and double click on the button. This will take you into the code behind for our aspx page.
Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.
We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page. We use the CC and Bcc methods to add the emails we would like to CC to. We will also be declaring the other variables, such as grabbing the sender and receiver, and accepting a body value for the email.
In this tutorial we learned about Sending Email with CC and BCC using ASP.NET 4.0 and C#. While this is a real simple example you can expand on this to build your own mailing system. This simple example can turn into a powerful tool and explains how to use the mail functions included in ASP.NET.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
SendEmailCCBCCCsharp2005.zip
First, you will need to import the System.Net.Mail namespace.
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and specify the CCs.
The front end .aspx page will contain a table to hold all of our input forms. These forms will accept the variables we will eventually use in our code behind. We have placed the code in a table to be able to easily have our form line up and access the information.
Code Block
Default.aspx
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
To
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtTo" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
From
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtFrom" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">
CC
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtCC" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">
BCC
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtBCC" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">
SMTP Server
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtSMTPServer" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
Subject
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtSubject" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
Body
</td>
<td bgcolor="#FFFFFF">
<asp:textbox id="txtBody" runat="server" columns="40" textmode="MultiLine"></asp:textbox>
</td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">
Action
</td>
<td bgcolor="#FFFFFF">
<asp:button id="btnSubmit" runat="server" text="Send Email" onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
Status
</td>
<td bgcolor="#FFFFFF" class="basix">
<asp:literal id="litStatus" runat="server"></asp:literal>
</td>
</tr>
</table>
Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.
We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page. We use the CC and Bcc methods to add the emails we would like to CC to. We will also be declaring the other variables, such as grabbing the sender and receiver, and accepting a body value for the email.
Code Block
Default.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailAddress SendFrom = new MailAddress(txtFrom.Text);
MailAddress SendTo = new MailAddress(txtTo.Text);
MailAddress SendCC = new MailAddress(txtCC.Text);
MailAddress SendBCC = new MailAddress(txtBCC.Text);
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
MyMessage.CC.Add(SendCC);
MyMessage.Bcc.Add(SendBCC);
MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(MyMessage);
litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
SendEmailCCBCCCsharp2005.zip
