Sending Emails Using ASP.Net
the websites in the internet will be having features to send emails to
someone. Most of the feedback sent to the Administrator of the website
from a user of the website is sent through emails. The feedback sent to
the WebMasters of the websites is also mostly sent through emails. Classes
are available in ASP.Net to send emails. The classes used to send emails
are the MailMessage and the SmtpMail classes found in the System.Web.Mail
To send emails your SmtpServer should be running. This can be checked using the Internet Services Manager found in Start->Programs->Administrative Tools. When you open the Internet Services Manager you can see whether the Default SMTP Virtual Server is running or not. If the ‘Play’ icon in the toolbar is disabled it means that the Default SMTP Virtual Server is running. You can also check by Right clicking the Default SMTP Virtual Server and see whether ‘Start’ is disabled. If it is so then the Default SMTP Virtual Server is running. This SMTP Service is shipped with Windows XP Professional and Windows 2000 by default.
It is better to test your SMTP Server whether it functions properly before developing any application in ASP.Net to send emails. The easiest way to test the SMTP Server is to open a text editor like Notepad and write,
Make sure that you are typing some valid email address for the To and From fields in the text file. Now save this text file in c:\inetpub\mailroot\pickup folder. Once you save this file it will disappear indicating that the mail has been sent. If the SMTP Server is not functioning properly it will not disappear indicating that the mail is not sent. The SMTP Service uses the c:\inetpub\mailroot folder for all the mail services.
Now we will see how to send mails from your web application. Create a web application using your Visual Studio.Net IDE. Now drop four text fields in the design page and make the fourth field to accept multi-line content by setting the TextMode property to MultiLine. Drop down a submit button which can be used to send mails when clicked. Now double click the button on the form to open the code window and write code in the Button1_Click() event to send emails. The steps involved in code are:
the System.Web.Mail namespace
The code for sending emails will look somewhat like this after you follow the above steps:
protected MailMessage emailMesg;
= new MailMessage();
emailMesg.BodyFormat = MailFormat.Html;
The Body property of the MailMessage instance would be in the HTML code as given in the following example.
emailMesg.Body = “<HTML><HEAD><TITLE>Email in HTML Format</TITLE></HEAD><BODY bgcolor="Maroon"><h3>Hello Friend! </h3><p><font face='Arial' size='2' color=white>I am sending my mail in HTML format...</font></p></BODY></HTML>”;
The above HTML code will send the mail message in white color with Maroon background color for the page. You can also send attachments with an email message. For sending attachments you have to use the MailAttachment class available. The code given below will add a file as attachment to the email message.
This code has to be given before using the Send method. The path to the document to be attached can also be given from the File Input field in the HTML code.
Thus it is
easy to send emails through a web page in your web application. You may
try to send emails by following the above methods in your websites.
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
|Designing applications in .Net using Service Oriented Architecture | How to Create and Use Anonymous Delegates in C# (C Sharp) |How to Define Custom Attributes in C# (C Sharp)| How To Handle Errors While Developing ASP.NET Applications | How to Use Indexer in Classes and Interfaces of C# (C Sharp)| Illustration of Access Keywords (base, this) with Examples in C# (C Sharp) | Illustration of Null Coalesce Operator (??) in C# (C Sharp) |Indexers Vs Properties in C# (C Sharp) | More about Reserved Attributes of C# (C Sharp)| Overview of Unified Type System of C# (C Sharp) | Purpose of Delegates in C# (C Sharp) |How does Simple Object Access Protocol (SOAP) help in Internet Communication |How to Create and Use Anonymous Delegates in C# (C Sharp)|