
Introduction
This article explores the mechanism of an SMTP mail system developed in the latest ASP.NET 2.0 technology. The readers will get a fairly good idea about the mail sending system and can easily develop a good mail service on their own. I have already posted an article about the mail sending mechanism using ASP.NET 1.1 technology which describes all the SMTP concepts, configuration, and development.
If you are really interested and want to know more about the concepts and configuration, you can very well refer to my previous article: Email System Using ASP.NET.
Background
SMTP stands for Simple Mail Transfer Protocol. Mails are sent using this perfect protocol. The .NET Framework supplies the SMTP
namespace that enables you to send simple /complex mail messages. You can do advanced SMTP mail programming by utilizing the classes in the SMTP
namespace.
Code Snippets
Here is the code for sending a mail:
private void Send()
{
_mTo = txtTo.Text.Trim();
_mFrom =txtFrom.Text.Trim();
_mSubject = txtSubject.Text.Trim();
_mMessage = txtBody.Text.Trim();
_mMailServer =
ConfigurationManager.AppSettings["MailServer"].ToString();
_mPort = Int32.Parse(
ConfigurationManager.AppSettings["MailServerPort"].ToString());
try
{
objMessage = new MailMessage(_mFrom, _mTo, _mSubject, _mMessage);
Attachment attach = new Attachment(fileAttach.PostedFile.FileName);
objMessage.Attachments.Add(attach);
SmtpClient objSmtpClient = new SmtpClient(_mMailServer, _mPort);
objSmtpClient.UseDefaultCredentials = true;
objSmtpClient.Send(objMessage);
Message("Mail has been sent successfully.");
Clear();
}
catch (FormatException ex)
{
Message("Format Exception:" + ex.Message);
}
catch (SmtpException ex)
{
Message("SMTP Exception:" + ex.Message);
}
catch (Exception ex)
{
Message("General Exception:" + ex.Message);
}
}
Final solution
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
#region DECLARATIONS
private string _mMailServer;
private string _mTo;
private string _mFrom;
private string _mMessage;
private string _mSubject;
private int _mPort;
private MailMessage objMessage;
#endregion
#region EVENTS
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
Send();
}
protected void btnReset_Click(object sender, EventArgs e)
{
Clear();
}
#endregion
#region METHODS
private void Message(string strMessage)
{
Label lblMsg = new Label();
lblMsg.Text = strMessage;
phMessage.Controls.Add(lblMsg);
}
private void Send()
{
_mTo = txtTo.Text.Trim();
_mFrom =txtFrom.Text.Trim();
_mSubject = txtSubject.Text.Trim();
_mMessage = txtBody.Text.Trim();
_mMailServer =
ConfigurationManager.AppSettings["MailServer"].ToString();
_mPort = Int32.Parse(
ConfigurationManager.AppSettings["MailServerPort"].ToString());
try
{
objMessage = new MailMessage(_mFrom, _mTo, _mSubject, _mMessage);
Attachment attach = new Attachment(fileAttach.PostedFile.FileName);
objMessage.Attachments.Add(attach);
SmtpClient objSmtpClient = new SmtpClient(_mMailServer, _mPort);
objSmtpClient.UseDefaultCredentials = true;
objSmtpClient.Send(objMessage);
Message("Mail has been sent successfully.");
Clear();
}
catch (FormatException ex)
{
Message("Format Exception:" + ex.Message);
}
catch (SmtpException ex)
{
Message("SMTP Exception:" + ex.Message);
}
catch (Exception ex)
{
Message("General Exception:" + ex.Message);
}
}
private void Clear()
{
txtTo.Text = string.Empty;
txtFrom.Text = string.Empty;
txtSubject.Text = string.Empty;
txtBody.Text = string.Empty;
}
#endregion
}
You have to do some configuration settings in order to make this work. Provide the IP address of the mail server and the port which you are using for sending mail in the appsettings
section of the web.config file, as shown below:
<configuration>
<appSettings>
-->
-->
-->
<add key="MailServer" value=""/>
-->
-->
<add key="MailServerPort" value=""/>
</appSettings>
</configuration/>
Using the Solution
Download the source code files given above in the zip file and copy the contents into any directory.
Go to Internet Information Services (IIS) and create a virtual directory named SMTPMail and map the physical path to the virtual directory. Do the mail server configuration settings as mentioned above, in the web.config file.
The URL to use will be http://localhost/SMTPMail/. Happy coding..!!!