Sending E-Mails in .NET

March 11, 2008 at 6:08 am Leave a comment

Sending a Simple E-Mail Message

To send an e-mail with a simple text message, you have to use the Send() method of SMTP class. The syntax of the Send() method is shown in Listing 1.1 and an example is shown in Listing 1.2:

Listing 1.1

[Visual C# .NET]

SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY");

[Visual Basic .NET]

SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY")

Listing 1.2

[Visual C# .NET]

SmtpMail.Send("info@mydomain.com","hello@hello.com","Thank You",               "We look forward to working with you again in the                future");

[Visual Basic .NET]

SmtpMail.Send("info@mydomain.com","hello@hello.com","Thank You", _               "We look forward to working with you again in the _                future")

You can place the above code either on the form’s Load() event or in a button control.

Sending E-Mail Messages Using WebForm Controls

The main problem with the code given in Listing 1.2 is that you have to change the parameter values each time for sending different mail messages. To solve this problem, you should build a User Interface using the required WebForm controls, as shown in Figure 1.

Figure 1: The User Interface

As you can see, the above User Interface is made up of two text boxes, one multiline text box, and a button. I have also applied ASP.NET validation controls to the above GUI to avoid errors. For more information regarding the usage of validation controls, refer to my previous articles Performing Validations with ASP.NET — Part 1 and Performing Validations with ASP.NET — Part 2.

Instead of supplying all parameters in the Send() method, you can define properties and their corresponding values separately by creating an instance of the MailMessage class. With the help of this class, you can easily add attachments, set priorities, BCC, CC values, and much more. Table 1, given at the end of this article, shows a list of properties of the MailMessage class. Add the code given in Listing 1.3 by double-clicking the button captioned Submit:

Listing 1.3

[Visual C# .NET]

MailMessage objEmail          = new MailMessage();             objEmail.To       = txtTo.Text;             objEmail.From     = txtFrom.Text;             objEmail.Cc       = txtCc.Text;             objEmail.Subject  = "Test Email";             objEmail.Body     = txtName.Text + ", " +                                 txtComments.Text;             objEmail.Priority = MailPriority.High;   //SmtpMail.SmtpServer = "localhost";   try{     SmtpMail.Send(objEmail);     Response.Write("Your Email has been sent sucessfully -                     Thank You");   }   catch (Exception exc){     Response.Write("Send failure: " + exc.ToString());   }

[Visual Basic .NET]

  Dim objEmail as New MailMessage()       objEmail.To       = txtTo.Text       objEmail.From     = txtFrom.Text       objEmail.Cc       = txtCc.Text       objEmail.Subject  = "Test Email"       objEmail.Body     = txtName.Text & ", " &txtComments.Text       objEmail.Priority = MailPriority.High   'SmtpMail.SmtpServer  = "localhost"   try     SmtpMail.Send(EMail)     Response.Write(Your E-mail has been sent sucessfully - _                    Thank You)    catch exc as Exception     Response.Write("Send failure: " + exc.ToString())   End Try

Note: If you are using your local system (Server = localhost) instead of a real live server, you should properly enable relying on the Internet Information Server (IIS).

When you execute the above code, the server not only displays a confirmation message but also sends an e-mail to the address mentioned in the To and Cc text boxes with the information you entered in the respective fields. It is not necessary for you to enter a Cc address, but it is shown here as part of the explanation. Further, the e-mail will be sent with the highest priority.

Table 1: MailMessage class properties

Property Description
Attachments Used for sending e-mails with attachments
From Sender’s e-mail address
To Recipient’s e-mail address
Cc Recipient’s e-mail address (Carbon Copy)
Bcc Recipient’s e-mail address (Blind Carbon Copy)
Body Text of the e-mail message
BodyFormat Specifies the format of an e-mail message (Possible Values: Text, Html)
Priority Specifies the priority of an e-mail message (Possible Values: High, Low, and Normal)
Subject Denotes the subject of an e-mail message
Headers Denotes a collection of acceptable headers (Example: Reply-To)
BodyEncoding Specifies the method of encoding an e-mail message (Possible Values: Base64 and UUEncode)
Advertisements

Entry filed under: My Family.

.NET Frequently Asked Questions Its Me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

March 2008
M T W T F S S
     
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Most Recent Posts


%d bloggers like this: