LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Sending Email from ASP.NET 2.0
by Web Team at ORCS Web
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 15074/ 21

Sending email from ASP.NET 1.0 and 1.1 was very easy. It is still easy with ASP.NET 2.0, but you need to learn the new class names. I'm not sure why Microsoft changed these classes--certainly there is a reason, I just don't know it yet.

The System.Net.Mail namespace in ASP.NET 2.0 has replaced the System.Web.Mail namespace in ASP.NET 1.x. There are a number of classes within this namespace, and it wasn't clear to me at first what was needed to send an email. I messed with a number of the classes, trying to create a new mail object and then set its properties in the way I'm used to doing it, but no combinations I tried would work.

What I did find was the System.Net.Mail.SmtpClient class, which has a method named Send. Rather than setting properties and calling this method, I wound up simply passing the values I wanted into this method, doing all the "real" work on a single line.

One property of this class that you do need to set before calling Send is the Host property, so that the class knows where to send the SMTP email for delivery.

I won't bore you with the simple form that I created to test this. The form had four textboxes: "EmFrom" for the sender's email address, "EmTo" for the recipient's email address, "EmSubj" for the subject of the email, and "EmMsg" for the body of the email.

The following three lines do all of the work:

Dim MailObj As New System.Net.Mail.SmtpClient
MailObj.Host = "localhost"
MailObj.Send(EmFrom.Text, EmTo.Text, EmSubj.Text, EmMsg.Text)

The above assumes you are using the local SMTP service of the machine on which this code is running. You can also specify a remote host, but I don't yet know how to authenticate against a remote host with SMTP-Auth (which most SMTP hosts are now running).

Hopefully this will help someone else and save them the time that I spent digging into the process of sending an email with ASP.NET 2.0.

Happy coding!

By Brad Kingsley, founder and president of ORCS Web, Inc. - a company that provides complex managed hosting services for clients who develop and deploy their applications on Microsoft Windows platforms.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-02-25 3:25:44 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search