Sending Emails Using ASP.Net

Almost all 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 namespace.




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,

To: someperson@somesite.com
From: me@mysite.com
Subject: Testing SMTP Server in my system.

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:

1. Import the System.Web.Mail namespace
2. Create an instance of the MailMessage class
3. Set the properties of the MailMessage class like To, From, Subject, and Body
4. Use the Send method of the SmtpMail class to send the email message by passing the instance of the MailMessage class as the parameter to the Send method.

The code for sending emails will look somewhat like this after you follow the above steps:

using System.Web.Mail;

protected MailMessage emailMesg;

emailMesg = new MailMessage();
emailMesg.From = txtFrom.Text;
emailMesg.To = txtTo.Text;
emailMesg.Subject = txtSubject.Text;
emailMesg.Body = txtBody.Text;
emailMesg.Priority = MailPriority.High;
SmtpMail.Send(emailMesg);
By default the mail messages are sent as Plain text. It is possible to send mails in the HTML format which allows you to sent mails as web pages which is rich in look. To send emails in the HTML format you need to set the BodyFormat property of the MailMessage instance to MailFormat.Html. If you need to send mail in HTML format the above code would be added one more line like,

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.

protected MailAttachment emailAttach;
emailAttach = new MailAttachment(@"path-to-the-document");
emailMesg.Attachments.Add(emailAttach)

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.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

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


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.