Sending Emails in ASP.NET 2.0
By AzamSharp
Views: 4692

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

 

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: best
Name: ozgur
Date: 6/20/2007 5:12:48 AM
Comment:
Hi,

Best articles.
Subject: best artical
Name: vikrant
Date: 9/4/2007 9:51:48 PM
Comment:
HI

Helpful articles
Subject: Hi
Name: jun2
Date: 10/17/2007 11:13:45 PM
Comment:
Nice article.. pretty straigthforward.
Subject: Email sending fail
Name: Asghar Ali
Date: 2/4/2008 3:13:16 AM
Comment:
Hi,

It is helpful but i'm getting some error
for example in smpt if i included "mail.aliwebdev.com" and then i can send email to any email related to aliwebdev but if i try to send to yahoo or hotmail or gmail then i'm getting email "email sending fail"

i want to send to all the email ex:hotmail,yahoo,gmail,mydomain & etc. Would you plz let me know

It would be appreciate.

Thanks
Asghar Ali
Subject: RE: Email Sending Fail
Name: AzamSharp
Date: 2/4/2008 8:24:00 AM
Comment:
Hi Asghar Ali, This error can be due to anything. Check that your firewall is off. Also check the relay settings and if any virus blocker is blocking to send emails.
Subject: Email,ExchangeServer
Name: Sabyasachi
Date: 2/4/2008 10:11:12 PM
Comment:
Hi,
can u help me....
how can i send email in an Intranet using Exchange Server......

is it like that...

string hostAddress = "MyExchangeServerName";
Subject: hi
Name: Layeeq Shaik
Date: 3/31/2008 3:39:32 AM
Comment:
tnxs for providing this article.. but i have some doubt that.. i am using internet with proxy server, and i dont know the smtp server name (host name) can i use localhost instead of remote host name. in fact when i am using local host name its giving error"sending mail failure " and my email goes in Bad mail directory. please give any tips or suggestion.

Thnx in Advance



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008