Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Send Email with CC and BCC using ASP.NET 2.0 and C#.NET (0 Comments)
Admin: Posted Date: April 4, 2010

This tutorial will show you how to send a simple email message with CCs and BCCs using ASP.NET 2.0 and C#.NET.

Send Email with CC and BCC using ASP.NET 2.0 and C#.NET


This tutorial will show you how to send a simple email message with CCs and BCCs using ASP.NET 2.0 and C#.NET

Sending an email with CCs and BCCs using ASP.NET 2.0 and VB.NET is actually very simple.

First, you will need to import the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and specify the CCs.

using System.Net.Mail;

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.

We use the btnSubmit_Click event to do the work.

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.



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();
}
}

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The front end .aspx page looks something like this:

<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>

The flow for the code behind page is as follows.


 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

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();
}
}
}

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: