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

WorldofASP.NET >> ASPNET >> General ASPNET

Sending Email in ASP.NET 1

This article explain how to send email in ASP.NET 1 either by using SMTP authentication or not

Published Date : 13 Nov 2007

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

Introduction

One of the most common things we do as a webmaster is sending email to our clients. In old classic ASP, we need to rely on third party components just for sending email. Things has change a lot since Microsoft release .NET 1 framework.  We can use the library inside the .NET Framework to send email.

In .NET Framework 1, there is a class library that you can use to send email. The class is inside the System.Web.Mail namespace and the class name is SmtpMail.

Main

Sending Email in ASP.NET 1 is quite easy and straight forward, and you can send ASP.NET email in just 5 lines of code. I will write down the sample code below in both VB.NET and C# language.
First and foremost you need to add reference to System.Web.Mail namespace.

The code below will work only with .NET Framework 1.0 and not on .NET Framework 2.0. The reason is because Microsoft has move the SmtpMail class from System.Web.Mail namespace to System.Net.Mail namespace

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

[C#]
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "Testing Email.";
mail.Body = "this is my test email body";
SmtpMail.SmtpServer = "IPAddress";  //your real server goes here
SmtpMail.Send( mail );
[VB.NET]
Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "Testing Email."
mail.Body = "this is my test email body"
SmtpMail.SmtpServer = "IPAddress" 'your real server goes here

SmtpMail.Send(mail)

Replace the IPAddress with your SMTP Server IPAddress.  If you are hosting your site somewhere, you need to ask the support for the SMTP Server address.

If you see the code above, it is quite simple actually to send email in .NET Framework 1 and ASP.NET. However the code above will not work if your SMTP server is configured with SMTP Authentication.
If you are hosting your site somewhere, big chances that the code above will not work.

The reason is simple, to fight the abuse and spam. Most of the email administrators will enabled SMTP authentication on their server. And this will means only those who can authenticate successfully will be allowed to send email.

In the following code, we will look on how to send email using SMTP authentication in .NET Framework and ASP.NET 1

Basic Sample Code on Sending Email using SMTP Authentication with ASP.NET 1

[C#]

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "email username");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "email password");
SmtpMail.SmtpServer = "mail.mycompany.com";  
SmtpMail.Send( mail );

[VB.NET]
Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "Some text goes here"
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "email username") 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "email password") 
SmtpMail.SmtpServer = "mail.mycompany.com" 'your mail server goes here

SmtpMail.Send(mail)
From the code above, you can see that we provide the authentication details before we called the SMTPMail.Send method. This will allow you to send the username and password details to your mailserver so that your mailserver will know that you are a legitimate user and therefore will allow you to send email

Now, lets go through another example on Sending HTML email.  Sending HTML Email is quite easy also in the .NET 1. With just switching one properties from Mail class, you can set whether you like to send HTML email or Text Email. The code sample is below

Sample Code on Sending Email using HTML and ASP.NET 1

[C#]
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "Testing Email.";
mail.BodyFormat = MailFormat.Html;
mail.Body = "this is my test email body.<BR><B>Bold</B>"
SmtpMail.SmtpServer = "localhost";  //your real server goes here
SmtpMail.Send( mail );

[VB.NET]
Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "Testing Email."
mail.BodyFormat = MailFormat.Html
mail.Body = "this is my test email body.<BR><B>Bold</B>"
SmtpMail.SmtpServer = "localhost" 'your real server goes here

SmtpMail.Send(mail)

Troubleshooting System.Net.Mail

Few tips and tricks for you to troubleshoot if you email never arrived to the destination
  • Check if the SMTP Server is listening on port 25 (telnet ipaddress 25)
  • Check if your Mailserver ip is being blacklisted (http://mxtoolbox.com/blacklists.aspx)
  • Check if your SMTP Server is being configured using SMTP Authentication. 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.


Other Related and Popular Articles :

Working with Master Pages and Basics of Master Pages
Why do you need master pages and how it can improve the maintenance of your website
Tips to Improve Your ASP.NET Web site performance
The article contains guidelines for improving your ASP.NET Web applications
Use C# and VB.NET in the same project
Use C# and VB.NET classes in the same ASP.NET application
Publish and create RSS Feeds easily on your ASP.NET websites
This article explains the concept of creating RSS feeds for your ASP.NET website with auto updating contents
Working with HttpWebRequest and HttpWebResponse in ASP.NET
This article explain how to use WebRequest and WebResponse in ASP.NET to grab contents and screen scrape

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

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