WorldofASP.NET : ASP.NET Tutorial, Hosting, and Source Code

WorldofASP.NET >> ASPNET >> Applications

Sending Email in ASP.NET 2.0

Send email in ASP.NET 2.0 Framework with or without SMTP Authentication

Published Date : 24 Aug 2007

Author : Handy Chang
Language : VB.NET,C#
Platform : .NET
 
Technology : Visual Studio,ASP.NET
Views : 11560
Rating : (1 votes so far)
Email to a Friend | Print this Article | Add to Favourites | Report Error

Introduction

A lot of times, we are facing some issues when sending email through the mailserver. Especially if we host our websites in shared hosting and the mail will never get to the recipient. There are lots of reason for this to happen. In this article I would like to explain how to Send Email using .NET 2.0 Framework and how to troubleshoot your code if your email never reach the recipient. This article will only cover sending Email using ASP.NET 2.0 and will not work in ASP.NET 1.0.

Microsoft has improved the SMTP library in their .NET Framework 2.0 and therefore the code you find in this article will not work in .NET 1.0.

Main

Sending Email in .NET 2.0 is quite easy and straight forward and you can send Email with just five lines of code. In .NET 2.0, Sending Email library in ASP.NET 2.0 is called System.Net.Mail.SmtpClient library. Therefore your code need to Import System.Net.Mail or Using System.Net.Mail
in your code

Before you use the code below, please make sure that you know the ipaddress or the hostname of your mailserver. You can actually detect if the ipaddress of your mailserver is infact a mailserver by using telnet command.
In command prompt, type telnet ipaddress 25.
If you got replied from the telnet command, then the Ipaddress of your mail server has  the SMTP Service started, and if not, then you cannot use the Mail Server to send email. SMTP Server is listening on port 25 and therefore you need to be able to telnet in before you can send email.

Basic Sample Code to Send Text Email in ASP.NET 2.0

C#

    SmtpClient smtpClient = new SmtpClient("IPaddress");
    MailMessage message = new MailMessage("sender@sender.com", "rcpt@recipient.com");
    message.Subject = "Subject Email";
    message.Body = "Body Email";
    smtpClient.Send(message);

VB.NET

     Dim message As New MailMessage("sender@sender.com", "rcpt@recipient.com")
     Dim SmtpClient As New SmtpClient("ipaddress")
     message.Subject = "Subject Email"
     message.Body = "Body Email"
     SmtpClient.Send(message)

As you can see that you only need 5 lines of code to send email. And thats all you need. However the code above will not work if your SMTP server is configured with authentication. Lots of mailserver these days has been configured with SMTP authentication to prevent spam and abuse. SMTP Authentication means that before you can send email against the mailserver, you need to provide your login id and password. This is to prevent spam and abuse from unauthorized users. The login ID and password will normally be your email user and password or your NT Login. You need to check with your MailServer administration for further information about this.

Basic Sample Code on Sending HTML Email in ASP.NET 2.0
C#

    SmtpClient smtpClient = new SmtpClient("IPaddress");
    MailMessage message = new MailMessage("sender@sender.com", "rcpt@recipient.com");
    message.Subject = "Subject Email";
    message.Body = "&lt;H2>Testing HTML Email</H2>";
    message.IsBodyHtml = true;
    smtpClient.Send(message);

VB.NET
     Dim message As New MailMessage("sender@sender.com", "rcpt@recipient.com")
     Dim SmtpClient As New SmtpClient("ipaddress")
     message.Subject = "Subject Email"
     message.Body = "<H2>Testing HTML Email</H2>"       
     message.IsBodyHtml = True
     SmtpClient.Send(message)

As you can see that Sending HTML email from .NET framework is very easy. You just need to set the property IsBodyHtml = true and you are done!.

Basic Sample Code on Sending Email using Attachment
C#

message.Attachments.Add(new Attachment(@"C:\Data.txt"));


VB.NET
message.Attachments.Add(new Attachment("C:\Data.txt"))

For including attachments in your email, you just need to include the attachments in your MailMessage object. Adding above code to the previous code will allow you to send attachment email.

Sending Email through SMTP Authentication

If using above sample code doesn't let you send email, then you might need to try sending email using SMTP Authentication.

Basic Sample Code on Sending Email using SMTP Authentication

VB.NET

     Dim message As New MailMessage("sender@sender.com", "rcpt@recipient.com")
     Dim SmtpClient As New SmtpClient("IPAddress")
     message.Subject = "Subject Email"
     message.Body = "<H2>Testing HTML Email</H2>"       
     message.IsBodyHtml = True
     SmtpClient.Credentials = new NetworkCredentials("sender@sender.com","password")
     SmtpClient.Send(message)

C#

    SmtpClient smtpClient = new SmtpClient("IPaddress");
    MailMessage message = new MailMessage("sender@sender.com", "rcpt@recipient.com");
    message.Subject = "Subject Email";
    message.Body = "&lt;H2>Testing HTML Email</H2>";
    message.IsBodyHtml = true;
    smtpClient.Credentials = new NetworkCredentials("sender@sender.com","password");
    smtpClient.Send(message);

As you can see from the code above, you need to pass your email username and password before you can send email. Please note that the email username and password must belong to the mailserver or else it will reject if the mailserver couldn't validate the logon details.

Troubleshooting System.Net.Mail

Few tips and tricks for you to troubleshoot if you email never arrived to the destination

  1. Check if the SMTP Server is listening on port 25 (telnet ipaddress 25)
  2. Check if your Mailserver ip is being blacklisted (http://mxtoolbox.com/blacklists.aspx)
  3. Check if your SMTP Server is being configured using SMTP Authentication.
  4. If using SMTP Authentication, then check if your username and password is correct.
    You can try login using the username and password using outlook and try to send email from outlook. Remember to check using SMTP Authentication is your outlook settings.

Conclusion

Sending Email in ASP.NET 2.0 is extremely easy and straight forward. Thanks to Microsoft Team that has made this library very easy to use. Feel free to add any comments to my article and Happy Programming !



Other Related and Popular Articles :

URL Rewriting with ASP.NET 2.0
How to implement URL Rewriting and Improve your SEO rankings.
Building a Photo Tagging Application using ASP.NET 2.0, LINQ, and Atlas
In this article, I will examines how to build a photo tagging application using ASP.NET 2.0, LINQ and Atlas framework.
Using LINQ to XML (and how to build a custom RSS Feed Reader with it)
In this article, Scott examines how to work with LINQ using XML. He also demonstrates how to build a custom RSS Feed Reader using these technologies.
Creating Contact Us Form easily using ASP.NET and SMTP
This article explain how to create a simple contact us form by using ASP.NET and System.Net.Mail to send email to the website owner
Build a DotNetNuke FileManager in ASP.NET
Tutorial and Code Sample on building a DotNetNuke FileManager in ASP.NET

Author Profile : Handy Chang

I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course.
I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use.
I love .NET programming and am doing it almost every day now.

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

#Smtp Athentication NewtworkCredentials
28 Dec 2007 12:11 by : Huw Davies

I am trying to implement your Smtp authentication for email form and entered line from code

SmtpClient.Credentials = New NetworkCredentials("User@host.com", "password")

only visual studio says networkcredentials is not defined. Have I missed something?

#All code is working fine but not on Remote Server.
30 Nov 2007 3:43 by : Sunny

HI ,
I developed similar program & its working fine from my local pc , but when I upload this on mail server. This wont work. I contacted service provider. They said to use 127.0.0.1 as a ip but still emails are not delivering, any idea why this is happening ?

Thank you.

 
#Re: All code is working fine but not on Remote Server.
30 Nov 2007 4:22 by : Handy Chang

You probably need to check whether they are using smtp authentication or not.

The problem mostly because of the SMTP authentication being turned on in their mail server.

#Helpfull Tutorial
27 Nov 2007 1:42 by : Majid Malik

It is helpful. Thanks

#ms
14 Nov 2007 2:30 by : Ruth

will you give me a full version of this code - how to send email using asp.net 2.0 (vb.net)
Thanks
ruth
dikoware@yahoo.com

 
#Re: ms
30 Nov 2007 4:22 by : Handy Chang

The code shown in the tutorial already have full version.
Copy and paste the code above will work fine.

Leave New Comments


Article Content copyright by Handy Chang
Everything else Copyright © by WorldofASP.NET 2008
 
Announcements
Earn Cash Now







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | Contact Us | SiteMap | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | FREE ASP.NET CMS | Phone Card | PHP Directory | Bangkok Hotels |Calling Card