In this article I will show you that how you can send emails using ASP.NET 2.0. This article will also discuss some of the problems that I faced while sending email and how I resolved it.

Introduction:

In this article I will show you that how you can send emails using ASP.NET 2.0. This article will also discuss some of the problems that I faced while sending email and how I resolved it.

Sending Emails in ASP.NET 2.0:

So, let's get right to the business and see how sending emails functionality can be accomplished. In ASP.NET 2.0 a new namespace has been included which is System.Net.Mail. In ASP.NET 1.X we used System.Web.Mail which is now considered obsolete. Below you can see the complete code for sending an email.

MailMessage mail = new MailMessage();

string emailFrom = "someone@somewhere.com";

string hostAddress = "localhost";

string body = "This is the message body";

mail.From = new MailAddress(emailFrom);

mail.To.Add(email);

//set the content

mail.Subject = "This is a test email.";

mail.Body = body;

mail.IsBodyHtml = true;

//send the message

SmtpClient smtp = new SmtpClient(hostAddress);

smtp.Send(mail);

 

Yes, it is that simple. Now let's see some of the problems that I faced while trying to send the email.

Always put a try-catch block when sending emails. If you see an error saying "Failure Sending Email" with the inner exception saying "Error connecting to remote server" then there can be several causes for this exception. First see that you are sending the email to a valid email address. Don't put some dummy address use real addresses. Also check that if your ISP allows you to send email. My home IP does not allow me to send emails using Smtp Server. The final and the most important is that check that if there is any virus scanners are running. These virus scanners include Macfee, Norton etc. Try turning them off and send an email. If successful make the necessary settings in the anti virus application so they are able to send emails and open certain ports.

I hope you liked this article, happy coding!

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW