Monday, October 27, 2008

Sending Mail Using Web Form Controls

Introduction

To send e-mails, you should require access to a server with .NET Framework and SMTP enabled on it. The .NET Framework supplies a SMTP class that enables you to send a simple e-mail message. If you have to send an e-mail with added functionalities, you have to make use of the MailMessage class. With the help of this class, you can insert attachments, set priorities, and much more, very easily. You can also send HTML e-mail using this class.

Main

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 below:

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

Example:

SmtpMail.Send("mymail@mymail.com","yourmail@yourmail.com",
"Test Mail Subject","This is the test mail");

You can build a User Interface using the required WebForm controls, as shown in below.

Before we going to the next step one important thing is to check/ validate your form. Always perform validation control in every form submitted is a clever way to reduce database data stroring error because of mismatch datatype, etc. In this case it is for validating an email. For Email validation method can refer to another tutorial Here.

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 given at the end of this article, shows a list of properties of the MailMessage class. Add the code given below by double-clicking the button captioned Submit:

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());
}

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)


Below sytax is used for sending your mail in HTML format:

objEmail.BodyFormat = MailFormat.Html;

Conclusion

To send an email from your ASP.NET page, you need to: import the System.Web.Mail namespace in your ASP.NET page, create an instance of the MailMessage class, set all the properties of the MailMessage instance, and send the message with SmtpMail.Send method.

References

All the useful links or references that can help you learn about this tutorial

  1. SMTP Client Class
  2. Configure SMTP Server and Email

No comments: