|
|
|
|
||||||||||||||
Sending Emails from ASP .NET
Sending email is a very common task in any web application. In almost every web
application (web site), their will atleast be an occassion to send email in any
fashion. In classic ASP, we worked with the CDONTS object to send emails from
an ASP page. The SmtpMail class in ASP .NET provides properties and methods for sending
messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component
In this article, we will see, how can we send email from an ASP .NET
page. In a nut shell, today, we will be looking into the following:
What we need to send Email from an ASP .NET?The first thing that you need is the SMTP service. SMTP service should be up and running. And you also need to import the namespace, System.Web.Mail. To create a mail object, you need to create an instance of MailMessage. MailMessage has all required properties such as To, Subject, BCC, CC etc. For a complete list of method and properties, that you can make use of, please visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp. How to send an email from an ASP .NET page?
In the above example, we start the coding by importing the namespace, "System.Web.Mail". Then, in the Page_Load event, we create an instance of MailMessage object. It is through the MailMessage object, we set all the properties such as To, From, Subject, Body etc. We can either send a text message or a html message. We need to specify the bodyformat in the BodyFormat property. One we set all the properties, it is ready to send the email. Before sending the email, you have to set another important property, ie; SmtpServer. You have to set this property. You should assign the name of your SMTP server to this property. In most cases you can assign this as "localhost". If you do not set this property, then you will not be able to send email from an ASP .NET page. Finally we send the email using SmtpMail.Send. This methods expects the MailMessage as an argument. Actually the method Send is overloaded. You can also send an email by specifiying, SmtpMail.Send (From, To, subject, body). To see the complete overloaded list, please visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp How can we send attachments in an email?To send attachments, we need to add attachments using the Add method, which is available in the Attachments object. The only thing that we need to add to the above example is msg.Attachments.Add (New MailAttachment(Server.MapPath("filename.ext")))
Enhancements that you add to the above examples.We can add any number of attachments to an email. To send multiple attachments, just repeat the line Msg.Attachments.Add with the file that needs to be attached. Also, it will be a good practice to add a try...catch...finally block before invoking the Send Method. Since any error can occur while sending an email such as smtp service not running, smtp server busy and many more. To know more about the try...catch...finally, exception handling mechanism, read my article Exception Handling Links
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp
Send your comments to das@aspalliance.com Back to Tutorial | ||||||||||||||||
| Copyright © 2000-2003 ASPAlliance.com Page Rendered at
11/7/2009 10:26:32 PM |
||||||||||||||||