Sending E-mail Using ASP.NET v2.0
page 1 of 1
Published: 01 Jun 2006
Unedited - Community Contributed
Abstract
In this article, we will demonstrate how to send an e-mail using ASP.NET 2.0.
by Web Team at ORCS Web
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 15095/ 19

 

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!



User Comments

Title: Programmer   
Name: vishal dhole
Date: 2008-06-18 5:08:58 AM
Comment:
can i check if email exist on not!!!!1
Title: sd   
Name: sd
Date: 2007-06-25 9:48:00 AM
Comment:
Dim MailObj As New System.Net.Mail.SmtpClient
MailObj.Host = "localhost"
MailObj.Send(EmFrom.Text, EmTo.Text, EmSubj.Text, EmMsg.Text)
Title: Hi   
Name: Hanan
Date: 2007-02-21 4:00:59 AM
Comment:
how can i check if the email exists or not
Title: Looks good   
Name: Looks good
Date: 2006-11-21 2:51:24 AM
Comment:
Looks good
Title: Web Designer   
Name: Santosh Kamble
Date: 2006-11-19 5:17:56 AM
Comment:
Is ASP.Net forms runs on Linux server
Title: Web Designer   
Name: Santosh Anandrao Kamble
Date: 2006-11-19 5:09:47 AM
Comment:
I require more information about Ajax with ASP.Net
Title: Eila   
Name: Taurus
Date: 2006-10-13 10:49:26 AM
Comment:
Good
Title: email send   
Name: yogi
Date: 2006-08-08 5:51:59 AM
Comment:
realy it works
Title: It Gives error   
Name: pranav
Date: 2006-06-22 7:58:03 AM
Comment:
"No connection could be made because the target machine actively refused it" at line :
MailObj.Send("pranav_mca@india.com", "pranav_mca2006@yahoo.com", "Testing", "Have u got it?");
Title: Trial in asp.net   
Name: Scott Forsyth
Date: 2006-06-02 8:23:01 AM
Comment:
Hi Gurmeet. Now that Visual Web Developer has come out, you may want to consider it as it has full support for ASP.NET v2.0. http://msdn.microsoft.com/vstudio/express/vwd/download/

This includes all the of modern bells and whistles and is also free.
Title: TRIAL IN ASP.NET   
Name: GURMEET
Date: 2006-06-02 5:22:33 AM
Comment:
I AM TRYING TO MAIL THROUGH ASP.NET WEB MATRIX

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-10-23 5:41:39 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search