LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Sending E-mail Using ASP.NET v2.0
by Web Team at ORCS Web
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 9272/ 20


Sending e-mail from ASP.NET 1.0 and 1.1 was very easy.  It is still easy with ASP.NET 2.0 - assuming you know the new class names.  I am not sure why Microsoft changed these classes; certainly there is a reason, I just do not know it yet.

The System.Net.Mail class is what replaced the System.Web.Mail class.  There are a number of classes within this class and it was not exactly clear to me at first what was needed to send e-mail.  I messed with a number of the classes trying to Dim a New object and then set properties to the way I am 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 just wound up passing the values I wanted into this method - doing all the "real" work on a single line.

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

I will not bore you with the simple form I created to test this.  I had four textboxes: "EmFrom" for the sender email address, "EmTo" for the email address I am sending to, "EmSubj" for the subject of the email, and "EmMsg" for the body text of the email.

These 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 on the machine where this code is running.  You can also specify a remote host, but I do not know (yet) how to authenticate against a remote host with SMTP-Auth (which most SMTP hosts are running now).

Hopefully this will help someone else and save them the time that I spent digging into this.  If anyone reading this understands the value of all the other System.Net.Mail classes, please either send the feedback to me and I will write something up or put it in an article for the rest of us to see.

Editor's Note - You can also set your mail settings in your web.config, as this Scott Guthrie blog entry demonstrates.

Happy coding!

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-02 11:32:53 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search