Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Email - Send Email with Priorities using ASP.NET 2.0 and C#.NET
Send Email with Priorities using ASP.NET 2.0 and C#.NET


ASP.NET Email Tutorial

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

Sending a email with Priorities using ASP.NET 2.0 and C# 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 Priority.

using System.Net.Mail;

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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 then use a switch statement to determine the desired level of priority and set the Priority property of our MailMessage object accordingly.

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailAddress SendFrom = new MailAddress(txtFrom.Text);
MailAddress SendTo = new MailAddress(txtTo.Text);

MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;

switch (ddPriority.SelectedValue)
{
case "Low" :
MyMessage.Priority = MailPriority.Low;
break;
case "Normal" :
MyMessage.Priority = MailPriority.Normal;
break;
case "High" :
MyMessage.Priority = MailPriority.High;
break;
}
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(MyMessage);

litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
<tr>
<td height="50" align="center" class="lgHeader1">How to set Email Priority using ASP.NET 2.0 and C#</td>
</tr>
</table>
<br />
<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"> 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 align="right" bgcolor="#eeeeee" class="header1">
 Priority</td>
<td bgcolor="#ffffff">
<asp:DropDownList ID="ddPriority" runat="server">
<asp:ListItem>Low</asp:ListItem>
<asp:ListItem Selected="True">Normal</asp:ListItem>
<asp:ListItem>High</asp:ListItem>
</asp:DropDownList></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>

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

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

MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;

switch (ddPriority.SelectedValue)
{
case "Low" :
MyMessage.Priority = MailPriority.Low;
break;
case "Normal" :
MyMessage.Priority = MailPriority.Normal;
break;
case "High" :
MyMessage.Priority = MailPriority.High;
break;
}
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(MyMessage);

litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C#.NET 2005 Here!

Looking for the VB 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks