Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 61,928 times

Contents

Downloads

Related Categories

SMTP Mailing Application - The Code

Mike Gold

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)
{
 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.

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 Manhattan Bank in New York.  He is been active in developing Visual C++ Applications for 10 years and looks forward to the possibilities in C#.

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 Manhattan Bank in New York. He is been active in developing V

Comments

  • Posted by cicada on 07 Sep 2005

    Hi prazathin
    Not at all, good days

  • Posted by prazathin on 06 Sep 2005

    hi cicada
    Thank u

  • Posted by cicada on 05 Sep 2005

    and aspx page html part <%@ Import Namespace="System.Web.Mail" %>
    ok?

  • hi

    Posted by cicada on 05 Sep 2005