Florence Blogspot about asp.net tutorials and web design and web development

Monday, February 4, 2008

Sending Email in ASP.NET 2.0

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 = "<H2>Testing HTML Email

";
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 = "

Testing HTML Email

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

Testing HTML Email

"
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 = "<H2>Testing HTML Email

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

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.

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 !

No comments:

Search