Sending emails through ASP.Net2.0  is very easy. Just a few lines of code. But when u get an error you will be surprised.  So before you write up the few lines of code you have to do the following.

Configure IIS for sending emails. Its simple not a big deal. Check if SMPT is already installed. If not installed than go to Add Remove Windows Components and double click IIS. Click SMTP. Install it and restart your system.  Now your local web server is ready for sending emails. The next thing is the coding part.

Add this line of code to the top of the page.

using System.Net.Mail;

To send an email you need the following parts of an email.

From (address who is sending the email)

To (to whom the emails is being sent)

Subject  (subject of the email)

Body (body text of the email)

Write the following code


string strFrom, strTo, strSubject, strBody;

strFrom = "sender@gmail.com";

strTo = "rece<a href="mailto:receiver@gmail.com">iver@gmail.com</a>";

strSubject = "Test Email";

strBody = "You have a test email.";

MailMessage eProEmail = new MailMessage(strFrom, strTo, strSubject, strBody);

SmtpClient emailClient = new SmtpClient("localhost");

emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

emailClient.Send(eProEmail);

emailClient.Timeout = 500;

Run the code and check your email. I hope you will not encounter those errors saying “Mailbox not found” etc etc.

Advertisements