SMTP Mailing Application

The Code

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

  // Indicate that the message has been sent
 MessageBox.Show("Message Sent to " + ToTextBox.Text);

catch(Exception ex)


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.

You might also like...


About the author

Mike Gold United States

Mike Gold is President of Microgold Software Inc. and Creator of WithClass 2000 a UML Design Tool for C#. In the last few years Mike has consulted for companies such as Merrill Lynch and Chase M...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth