Smtp E-Mailing Appliction




Figure 1 - The Smtp Mailing Application 

This is an article on how to use the Mail Components in .NET to create a simple application to send e-mail.  You can also refer to Mahesh's article on this site for some additional details and understanding of Smtp Mail in .NET.

The Smtp Mail functionality is contained in the System.Web.Mail namespace and contains three classes:

System.Web.Mail Classes Description
SmtpMail Class used to send Mail via Windows 2000 SMTP services.
MailMessage This class contains everything contained in an e-mail message. An instance of this can be sent by the SmtpMail class.
MailAttachment This is a class representing an attachement to an email.

Table 1 - System.Web.Mail Namespace

The SmtpMail Class gives you two static methods to send mail: the quick and easy way, and the more detailed way.

SmtpMail.Send(FromString, ToString, SubjectString, MessageString)

or

SmtpMail.Send(aMailMessage)

I opted to send email using the MailMessage object in this example because you can send a lot more information.  Some of the properties of the MailMessage Object are shown below:

MailMessage Property Description
From who the email is from
To who the email is to
Cc who the email is copied to
Bcc who the email is copied to without seeing the recipients
Attachments A collection of Attachments to the email
Subject Subject of the email
Body Message Body of the email
BodyFormat The format of the Message Body (Currently Text or Html)
Priority MailPriority of the Message (High, Low, or Normal)
 
Table 2 - MailMessage Properties for an E-mail Message

Using this knowledge, we can quickly create a mail application in a Window Form.  First we create a Windows Form Application by going to the New->Project Menu:



Figure 2 - Creating the Windows Application in the IDE

In order to set up your application to run SmtpMail, you need to add a reference by right clicking on the namespace in the solution explorer->Add Reference.



Figure 3 - Adding a Reference to your Application from the Solution Explorer

and then choosing System.Web.dll in the Add Reference dialog:



Figure 4 - Using the Add Reference Dialog

We also need to add the using statements in our application for the mail components:

Listing 1 - using statements for web mail

using System.Web;
using System.Web.Mail;

Next we design our mail application by placing textboxes for From, To, Copy, Blind Copy, Subject, Attachment, and the Message Body.  We then add corresponding labels for each of the textboxes. Also, we add a button for sending the message and a button to exit the application.  As an added feature, we placed a browse button next to the attachment so we can browse for attachments on our file system.  The browse button uses the OpenFileDialog component for navigating for an attachment (as seen at the bottom of the designer.)



Figure 5 - E-Mail Sender Design View

Below is the code used to send the email we construct in our form. The code is executed in the SendButton_Click Event Handler.   The method simply creates a new MailMessage instance and populates it with the text typed into the various fields by the user.  The MailMessage object is then sent using the SmtpMail.Send static function:

Listing 2 - Send Button Event Handler

private void SendButton_Click(object sender, System.EventArgs e)
{
try
{
// Construct a new mail message and fill it with information from the form
MailMessage aMessage = new MailMessage();
aMessage.From = FromTextBox.Text;
aMessage.To = ToTextBox.Text;
aMessage.Cc = CCTextBox.Text;
aMessage.Bcc = BCCTextBox.Text;
aMessage.Subject = SubjectTextBox.Text;
aMessage.Body = MessageTextBox.Text;
// if an attachment file is indicated, create it and add it to the message
if (AttachmentTextBox.Text.Length > 0)
aMessage.Attachments.Add(
new MailAttachment(AttachmentTextBox.Text, MailEncoding.Base64));
// Now send the message
SmtpMail.Send(aMessage);
// Indicate that the message has been sent
MessageBox.Show("Message Sent to " + ToTextBox.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

Below is the button event handler for populating the Attachment field through the Browse button.

It simply calls ShowDialog on the OpenFileDialog instance and if the result is ok it assigns the filename chosen by the user to the attachment field:

Listing 3 - Browse  Button Event Handler

private void BrowseButton_Click(object sender, System.EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
AttachmentTextBox.Text =
this.openFileDialog1.FileName;
}
}

I suspect you'd be more likely to use Microsoft Outlook then this simple application, but you can utilize the code in this application to customize it to suit your specific needs.  For example, you might consider creating a database in access and writing C# and ADO.NET for accessing groups of people you'd like to send emails.  You could change this into a component and have it pop up when you need to send emails in your application.  In any case, this will get you started in your quest to send emails using .NET.