Sending Email from ASP.NET 2.0
page 1 of 1
Published: 27 May 2005
Unedited - Community Contributed
Abstract
If you are moving to ASP.NET 2.0, you should know that the method of sending emails has changed slightly. Follow Brad Kingsley as he sends his first email from an ASP.NET 2.0 application.
by Web Team at ORCS Web
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 11928/ 18

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.



User Comments

Title: thnxs   
Name: reda
Date: 2012-12-04 11:34:31 AM
Comment:
thank you
Title: ali   
Name: rajper
Date: 2009-12-20 10:20:52 AM
Comment:
yes it is
Title: low volt fairy lights   
Name: john Cyan
Date: 2009-10-12 2:43:02 AM
Comment:
I would like to thank you for the efforts you have made in writing this article.
Title: Failure Sending Mail   
Name: Nitin
Date: 2008-07-05 2:56:13 AM
Comment:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'www.gmail.com' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.btn_Click(Object sender, EventArgs e)

Hi Sir.
I'm receiving this error whenever i try to send mail.
Plz help me to solve this prob.
Title: sending email   
Name: vijay
Date: 2008-01-01 3:48:15 AM
Comment:
its giving error as " mail box " unavailable
Title: Send_Email Class - Addition   
Name: Mark
Date: 2007-10-27 7:33:32 AM
Comment:
sorry forgot the To and From addresses...

update this line:
MailMessage objEmail = new MailMessage(mailFrom, mailTo);

Also the code was truncated in my last post so just to complete that include this at the end of my last post:


this.ErrorMessage = error.ToString();
}
}
}
}
Title: Send_Email Class   
Name: Mark
Date: 2007-10-27 6:51:01 AM
Comment:
using System;
using System.Configuration;
using System.Net.Mail;


namespace your_Library_Name
{
public class Send_Email
{
public string strErrorMessage = "", strEmailTo, strEmailFrom, strEmailSubject, strEmailBody;


public string ErrorMessage
{
get
{
return strErrorMessage;
}
set
{
strErrorMessage = value;
}
}

public string EmailTo
{
get
{
return strEmailTo;
}
set
{
strEmailTo = value;
}
}

public string EmailFrom
{
get
{
return strEmailFrom;
}
set
{
strEmailFrom = value;
}
}

public string EmailSubject
{
get
{
return strEmailSubject;
}
set
{
strEmailSubject = value;
}
}

public string EmailBody
{
get
{
return strEmailBody;
}
set
{
strEmailBody = value;
}
}


private void sendEmail()
{
try
{
MailAddress mailTo = new MailAddress(strEmailTo);
MailAddress mailFrom = new MailAddress(strEmailFrom);

MailMessage objEmail = new MailMessage();
objEmail.Subject = strEmailSubject;
objEmail.IsBodyHtml = true;
objEmail.Body = strEmailBody;

SmtpClient objEmailSender = new SmtpClient();
objEmailSender.Send(objEmail);
}
catch (Exception error)
{
Title: Managed vs Unmanaged   
Name: Jeffrey Cardona
Date: 2006-07-05 11:49:54 AM
Comment:
I am pretty sure that the old mailer classes are calling unmanaged code (CDOSYS). The new classes where added, must likely, to have it all in managed code.
Title: Thank You   
Name: Frank
Date: 2006-05-27 12:52:25 PM
Comment:
I wanted to thank you for posting this. So, Thank You.
Title: example   
Name: a
Date: 2006-03-08 1:55:45 PM
Comment:
http://www.codeproject.com/useritems/EmailApplication.asp#xx1331206xx
Title: Failure sending mail.   
Name: Vinicius Nyari
Date: 2006-01-26 11:46:20 PM
Comment:
\
\
\
\
\
\
\

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 4:56:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search