Monday, October 27, 2008

Sending Email in ASP.NET 1.0

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.
Bold"
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.
Bold"
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.

No comments: