How to Send Email from ASP .NET ?
page 1 of 1
Published: 17 Oct 2003
Abstract
Sending email is a very common task in any web application. In almost every web application (web site), their will atleast be an occassion to send email in any fashion. In classic ASP, we worked with the CDONTS object to send emails from an ASP page. The SmtpMail class in ASP .NET provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component In this article, we will see, how can we send email from an ASP .NET page. In a nut shell, today, we will be looking into the following:
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: 
Views (Total / Last 10 Days): 75066/ 1292

How to Send Email from ASP .NET ?

Sending email is a very common task in any web application. In almost every web application (web site), their will atleast be an occassion to send email in any fashion. In classic ASP, we worked with the CDONTS object to send emails from an ASP page. The SmtpMail class in ASP .NET provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component In this article, we will see, how can we send email from an ASP .NET page. In a nut shell, today, we will be looking into the following:

  1. What we need to send Email from an ASP .NET?
  2. How to send an email from an ASP .NET page?
  3. What is new in sending email? (SmtpMail.SmtpServer)
  4. How can we send attachments in an email?

What we need to send Email from an ASP .NET?

The first thing that you need is the SMTP service. SMTP service should be up and running. And you also need to import the namespace, System.Web.Mail. To create a mail object, you need to create an instance of MailMessage. MailMessage has all required properties such as To, Subject, BCC, CC etc. For a complete list of method and properties, that you can make use of, please visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp.

How to send an email from an ASP .NET page?
<%@ Import Namespace="System.Web.Mail" %>

<html>

<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)

     Dim msg as New MailMessage()

     msg.To = "das@silicomm.com"
     msg.From = "das@aspalliance.com"
     msg.Subject = "test"
     'msg.BodyFormat = MailFormat.Html
     msg.BodyFormat = MailFormat.Text
     msg.Body = "hi"

     SmtpMail.SmtpServer = "localhost"

     SmtpMail.Send(msg)
     msg = Nothing
     lblMsg.Text = "An Email has been send to " & "das@silicomm.com"

End Sub
</script>

<body style="font: 10pt verdana">
<form runat=server>
<asp:Label id=lblMsg runat=Server /> </form>
</body>
</html>

In the above example, we start the coding by importing the namespace, "System.Web.Mail". Then, in the Page_Load event, we create an instance of MailMessage object. It is through the MailMessage object, we set all the properties such as To, From, Subject, Body etc. We can either send a text message or a html message. We need to specify the bodyformat in the BodyFormat property. One we set all the properties, it is ready to send the email. Before sending the email, you have to set another important property, ie; SmtpServer. You have to set this property. You should assign the name of your SMTP server to this property. In most cases you can assign this as "localhost". If you do not set this property, then you will not be able to send email from an ASP .NET page. Finally we send the email using SmtpMail.Send. This methods expects the MailMessage as an argument. Actually the method Send is overloaded. You can also send an email by specifiying, SmtpMail.Send (From, To, subject, body). To see the complete overloaded list, please visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp

How can we send attachments in an email?

To send attachments, we need to add attachments using the Add method, which is available in the Attachments object. The only thing that we need to add to the above example is msg.Attachments.Add (New MailAttachment(Server.MapPath("filename.ext")))

<%@ Import Namespace="System.Web.Mail" %>

<html>

<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)

     Dim msg as New MailMessage()

     msg.To = "das@silicomm.com"
     msg.From = "das@aspalliance.com"
     msg.Subject = "test"
     'msg.BodyFormat = MailFormat.Html
     msg.BodyFormat = MailFormat.Text
     msg.Body = "hi"

     msg.Attachments.Add (New MailAttachment(Server.MapPath("EMAIL1.ASPX")))

     SmtpMail.SmtpServer = "localhost"

     SmtpMail.Send(msg)
     msg = Nothing
     lblMsg.Text = "An Email has been send to " & "das@silicomm.com"

End Sub
</script>

<body style="font: 10pt verdana">
<form runat=server>
<asp:Label id=lblMsg runat=Server /> </form>
</body>
</html>
Enhancements that you add to the above examples.

We can add any number of attachments to an email. To send multiple attachments, just repeat the line Msg.Attachments.Add with the file that needs to be attached. Also, it will be a good practice to add a try...catch...finally block before invoking the Send Method. Since any error can occur while sending an email such as smtp service not running, smtp server busy and many more. To know more about the try...catch...finally, exception handling mechanism, read my article Exception Handling

Links

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp

Exception Handling

Send your comments to das@aspalliance.com        


Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 2 and type the answer here:

User Comments

Title: Help   
Name: Ravi
Date: 8/22/2008 7:10:58 AM
Comment:
Hi.When I am using this code then below error occur please review this error and please send the solution to my email is ravi.bhartiya@dabur.com"
I am using the following code:

MailMessage Message = new MailMessage();
Message.To = "ravi.bhartiya@dabur.com";
Message.From = "ravi_mca2k4@yahoo.co.in";
Message.Subject = "Hi";
Message.BodyFormat = MailFormat.Text;
Message.Body = "Hi";
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(Message);

The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for ravi.bhartiya@dabur.com
Title: urgent   
Name: sitz
Date: 8/22/2008 5:25:56 AM
Comment:
sir,please send me the code for a request.
when email id is written a request has to send along with it saying accept/reject.
Title: The transport failed to connect to the server.   
Name: senthurganesh
Date: 7/11/2008 12:32:20 PM
Comment:
Dim msg As New MailMessage()

msg.To = "senthurganesh@gmail.com"
msg.From = "localhost"
msg.Subject = "test"
'msg.BodyFormat = MailFormat.Html
msg.BodyFormat = MailFormat.Text
msg.Body = "hi"
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)
msg = Nothing
lblMsg.Text = "An Email has been send to " & "senthurganesh@gmail.com"

my mailid senthurganesh@gmail.com
Title: Urgent : The transport failed to connect to the server.   
Name: senthurganesh
Date: 7/11/2008 12:26:36 PM
Comment:
Dim msg As New MailMessage()

msg.To = "senthurganesh@gmail.com"
msg.From = "localhost"
msg.Subject = "test"
'msg.BodyFormat = MailFormat.Html
msg.BodyFormat = MailFormat.Text
msg.Body = "hi"
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)
msg = Nothing
lblMsg.Text = "An Email has been send to " & "senthurganesh@gmail.com"
Title: Failure sending mail   
Name: Nitin
Date: 7/7/2008 1:38:24 AM
Comment:
Hi sir.
Everytime i try to send mail in asp.net i get following error.


System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 209.85.201.83 (209.85.201.83:25), connect error 10060 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) at _Default.btnsubmit_Click(Object sender, EventArgs e)

Plz tell me how come i remove this error.
plz reply me at nitin.gonnade@gmail.com
Title: Please reply me urgently   
Name: Anu
Date: 6/30/2008 3:34:45 AM
Comment:
Hello sir,
I want to send my .doc file to other user but attachment is not sucessful.2 times this was but not again sucessful.Please reply at ysn_malik@rediffmail.com
Title: Web Developer   
Name: YJ
Date: 6/23/2008 11:32:05 AM
Comment:
Just don't specify the mailserver:
...
...
'SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)
Title: please reply me urgently   
Name: sumalatha
Date: 6/23/2008 5:41:19 AM
Comment:
hiiiiiiii by the above process am getting the following error

"The server rejected one or more recipient addresses"
Title: Please reply   
Name: Eng.Maivel
Date: 6/19/2008 3:09:00 PM
Comment:
hi, can u please help me.
i've created a website and there is a send email page. it only worked in localhost but not when i uploaded it into the ftp. when i clicked the send button, a runtime error page will appear instead of sending the email(error on SMTP Server like "The transport failed to connect the server").i hope u can solve this problem. thank you.please email me at Comp_eng_Maivel@yahoo.com
Title: unable to send multiple emails   
Name: varsha
Date: 5/19/2008 4:35:36 AM
Comment:
am writing code in asp.net using c#.and i have created form using webform.so i want to write code for sending email.and i am also trying to get email_id from sqlserver2000,&then want to send mails to those ids& then in coming emails where i should stored and how i give them security kasatvarsha@rediff.com,kasatvarsha@gmail.com
Title: unable to send multiple emails   
Name: ravi
Date: 5/2/2008 11:55:32 AM
Comment:
i am tryng to get email id from sql database and then want to send mails to all those email ids how to do that plz tell me
reply at ravi120jobs@yahoo.co.in
Title: Engineer   
Name: Mumtaz Ali
Date: 4/9/2008 3:36:59 AM
Comment:
I learned a lot from this article thanks.
Title: Tutorial   
Name: Ashraf
Date: 3/26/2008 7:51:14 PM
Comment:
Please help how to add value in list box from drop down list in asp.net forms using java script
Title: Tutorial   
Name: sonal
Date: 3/12/2008 5:55:53 AM
Comment:
Give tutorial of how to send Email with Attachment.
Title: Wrong email   
Name: thariq
Date: 3/5/2008 6:25:06 AM
Comment:
hi i need to know what if the sending Email address is incorrect how is it possible to know that the sent address is incorrect how will i get that message
if there is an additional set of code or any link that will help me on this please send it to
thariqs@gmail.com
Title: How to send link   
Name: Hari
Date: 2/22/2008 5:10:40 PM
Comment:
Hi,
I need to send a URL as link to user. But now it is not going as link. It is displaying only as text. Everytime they need to copy that into the browser. If anyone knows tell me how to do that.

Send your response to hari.naryanan@gmail.com
Title: Needed Code for Backend coding and how to send mail to multiple email ids?   
Name: Shaili
Date: 2/21/2008 6:42:02 AM
Comment:
Hi..I want application with coding at backend side and to the multiple ids and with multiple attachments also..Pls do reply on shaili.it@gmail.com,sshhh_020@yahoo.com...Thanx in anticipation..
Title: receiving mail from another website   
Name: kanagaraj
Date: 2/2/2008 2:38:57 AM
Comment:
how to receive mail in asp.net webapplication from another website(like yahoo or gmail)... if i done, i have to install anything
Title: mail   
Name: sunil soni
Date: 1/23/2008 5:34:07 AM
Comment:
how to set reply mail code in asp.net
Title: how can i send mail to yahooId from my webpage   
Name: Pavan
Date: 1/22/2008 6:43:36 AM
Comment:
How can i send a mail message from my Web Page to a Particular Mail Id -- Like YahooId, Gmail

if anybody know this please write my e-mail. pavan_sept15@yahoo.co.in thank you. Bye
Title: Send Multiple Emails   
Name: Harish
Date: 1/6/2008 11:30:29 PM
Comment:
Hi,
i want to create a aspx page(compose mail). in this page i will give the user to send mail to other user. But how to send mail to multiple users.. Plse help me..
From,
Harish Neermarga

You can send solutions to my email_id
harish_neermarga@rediffmail.com
harishneermarga@gmail.com
Title: runtime error   
Name: finux
Date: 12/14/2007 1:12:31 AM
Comment:
hi, can u please help me.
i've created a website and there is a send email page. it only worked in localhost but not when i uploaded it into the ftp. when i clicked the send button, a runtime error page will appear instead of sending the email.i hope u can solve this problem. thank you.please email me at finux_ffa@yahoo.com.thank you very much..
Title: htmlFormatEmail   
Name: Amol Kagde
Date: 11/20/2007 3:37:54 AM
Comment:
hi,
yes im also thankful to you...to help me for email programming.
thank you..
Title: How to send an email with a link   
Name: prashanth
Date: 10/4/2007 1:29:23 PM
Comment:
Hi i need to send a webform as a link in the email please send me the code for that very urgent ....

you reply will be very much helpful

Reply to : kaluvalakp@gmail.com
Title: How to send an email with a link   
Name: prashanth
Date: 10/4/2007 1:27:59 PM
Comment:
Hi i need to send a webform as a link in the email very urgent ....

you reply will be very much helpful

Reply to : kaluvalakp@gmail.com
Title: How to send a link..??   
Name: Harish
Date: 9/11/2007 4:08:36 AM
Comment:
I want to send a link(e.g: www.google.com) to user's email so that on clicking the link, the user should open that site..

You can send solution to my email id :
harish_neermarga@rediffmail.com
harishneermarga@gmail.com
Title: How to send html page as body of e-mail   
Name: Khaled
Date: 9/6/2007 5:33:22 AM
Comment:
Hi,
I want to send html page as body of e-mail that i have prepared. So how can I do that?
please send me your suggestions.
To: k_zoabi@yahoo.com
Title: jk   
Name: hjkhj
Date: 8/31/2007 4:47:06 AM
Comment:
send mail
Title: send a HTML page as a body of the mail   
Name: Priyanka kadam
Date: 8/31/2007 2:53:07 AM
Comment:
send a HTML page as a body of the mail.
My email id:pskadam88@rediffmail.com
Title: How to send link on signup for user authentication   
Name: Ajoy Chakraborty
Date: 8/26/2007 4:31:52 AM
Comment:
I want to send a link to user's email so that on clicking the link, the user will be authenticated and can use my site only after that.
You can send solution to my email id : ajoy_1963@yahoo.co.in
Title: problem in sending mail to destination   
Name: dipen anand
Date: 8/24/2007 6:02:34 AM
Comment:
\
\
Title: Thank U   
Name: Ashok
Date: 8/24/2007 4:23:35 AM
Comment:
Good
Title: smtp server   
Name: Shadab Mirza
Date: 7/23/2007 5:47:58 AM
Comment:
Hello Sir,

I have tried with above code, but I face error on SMTP Server like "The transport failed to connect the server" .How can I solve this ?
Can you reply to me? in my Yahoo Id

Thank You Sir
Title: send html page as a body of email   
Name: sabir mohd ali
Date: 7/7/2007 7:13:15 AM
Comment:
thanks for this solution but my actual problem is that,
i wnt to send html page as body of email just like a webbrowser, without link or without attachement.
if you have a solution plz send me.
my email id:shabbu2n357@yahoo.co.in
Title: send a HTML page as a body of the mail   
Name: Sivakumar M
Date: 7/4/2007 6:54:45 AM
Comment:
I want to send a HTML page as a body of the mail. how do I do that?
Title: How do i send email to multiple email id's   
Name: george
Date: 6/10/2007 7:00:50 PM
Comment:
How do i send email to multiple email Id's like Hotmail, yahoo etc.

also, how do i get an smtp server or mail server.

secondly, if i want to send multiple email from asp.net what server do i need, mail server or web server.

Please if anyone have solution please email felix_zama123@yahoo.ca
Title: Cant send email to rediff and gmail account   
Name: Jatin Chawla
Date: 6/6/2007 5:38:37 AM
Comment:
hii, can u tell me why am i not able to recieve mails in my rediff and gmail account where as i m able to send it on yahoo. SMTP server used here is "127.0.0.1"

Help
Title: Nice, but only send mail in intranet   
Name: velu
Date: 5/20/2007 1:29:01 AM
Comment:
with in the orgination is it worked. how to send mail to other server like yahoo, gmail.
Title: How to send email to many email id's   
Name: subramanyam
Date: 4/19/2007 4:43:44 AM
Comment:
Hi,

I want to know how to send mail to multiple email-ids that are selected...from database
mail me to subramanyam.net@gmail.com
Title: Excellent   
Name: Tirupathirao
Date: 4/18/2007 4:09:53 AM
Comment:
its really excellent
Title: SMTP problem   
Name: brijesh
Date: 3/15/2007 2:14:16 AM
Comment:
if i write "Localhost" that time completly done.
but i want to send email to gmail.so what process?
how to assign name of our SMTP server
Title: About smtp server   
Name: M.Ramu
Date: 2/7/2007 6:20:57 AM
Comment:
Hello Sir,

I have tried with above code, but I face error on SMTP Server like "The transport failed to connect the server" .How can I solve this ?
Can you reply to me?
Title: how to send mails to many email-ids?   
Name: reshma
Date: 12/24/2006 4:23:57 AM
Comment:
hi..
I want to know how to send mail to multiple email-ids that are selected...
mail me to reshbhat@gmail.com
Title: smtp server   
Name: pawan
Date: 12/22/2006 4:25:48 AM
Comment:
Hello Sir,

I have tried with above code, but I face error on SMTP Server like "The transport failed to connect the server" .How can I solve this ?
Can you reply to me?

pawanpandey800@gmail.com
pawanpandey_800@yahoo.co.in
Title: E   
Name: pawan
Date: 12/22/2006 4:17:53 AM
Comment:
1.How can i send a mail message from my Web Page to a Particular Mail Id -- Like YahooId
2. I am use this code but mail was not reach the perticuler email id.
3. the error wiil be server not connect.

if anybody know this please write my e-mail. pawanpandey800@gmail.com thank you. Bye
Title: email   
Name: pawan
Date: 12/22/2006 4:15:06 AM
Comment:
How can i send a mail message from my Web Page to a Particular Mail Id -- Like YahooId

if anybody know this please write my e-mail. pawanpandey800@gmail.com thank you. Bye
Title: Good   
Name: Peter
Date: 12/20/2006 12:46:45 PM
Comment:
Nice. I liked. But the namespace System.Net.Mail is better i thi.nk
Title: how can i send mail to yahooId from my webpage   
Name: Pawan
Date: 12/14/2006 8:42:19 AM
Comment:
How can i send a mail message from my Web Page to a Particular Mail Id -- Like YahooId

if anybody know this please write my e-mail. pawanpandey800@gmail.com thank you. Bye
Title: Very simple code to send emails in asp.net 2.0 using C#   
Name: DotNetSpace
Date: 12/5/2006 9:12:30 PM
Comment:
Simple example on how to send emails using C#:
http://www.dotnetspace.com/articles/general-articles/sending-emails.html
Title: sending email failure   
Name: vijay
Date: 11/28/2006 4:03:27 AM
Comment:
i m trying to send an email,its working on local machine,but ultimately i need to send an email from some other ftp server,so could u plz tell me how to do it,do i need to provide the smtp servername of that server then also i m not able to send an email.........waiting for the reply
Title: Help   
Name: Mike
Date: 10/30/2006 8:40:39 AM
Comment:
Not working
Title: Good!!   
Name: Noel
Date: 6/15/2006 11:07:33 AM
Comment:
The code worked really well (I did not use it as it is though in principle it was the same). I want to send a HTML page as a body of the mail. how do I do that?

noel@zylemworld.com
Title: Notion .   
Name: OMAR
Date: 4/7/2006 6:22:14 PM
Comment:
thanks .
Title: its ok..   
Name: prakash Kolhe
Date: 2/24/2006 12:04:37 AM
Comment:
hi...
this code is good one but not giving what i m expecting
i want more on webserver..
that how can i send mail from my host to other like yahoo/hotmail
please let me know with code

my id is : prakash_kolhe1981@yahoo.com
Title: A1   
Name: Am
Date: 2/22/2006 2:47:23 PM
Comment:
Good
Title: SMTP SERVER   
Name: Venkat
Date: 1/30/2006 1:05:43 AM
Comment:
Hello Sir,

I have tried with above code, but I face error on SMTP Server like "The transport failed to connect the server" .How can I solve this ?
Can you reply to me?

My id- venkatmath@gmail.com
Title: good but still not working   
Name: chris
Date: 1/16/2006 6:33:21 PM
Comment:
nice article, but i have try to using SMTPserver that is not working . I have got a ASP.net webhost, i have upload the email.asp that host then
Server Error in '/' Application.
--------------------------------------------------------------------------------

“SendUsing”??????
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Please help me
chenzh1980@hotmail.com
Title: SMTP server Settings   
Name: Amit
Date: 12/15/2005 2:36:23 AM
Comment:
Respected Sir,
I have tried the above code but I am facing problems like the "SendUsing" is not configured.
How to check whether the SMTP server is running.
do we have to do initial settings

Please send response to the following e-mail id:
amit.patil@rkhs.co.in
Title: how can i send mail to yahooId from my webpage   
Name: AjeetVerma
Date: 9/29/2005 4:38:16 AM
Comment:
How can i send a mail message from my Web Page to a Particular Mail Id -- Like YahooId
Title: not working   
Name: senthil
Date: 9/12/2005 4:18:33 AM
Comment:
hi
i used this codeing but it's not working. the error shows access denied

pls help me, how to send mail
Title: Access is denied   
Name: dipankaronline@gmail.com
Date: 9/11/2005 6:15:39 AM
Comment:
your code shows some errors while i`m trying to access the page, please click the following link and have a look, i`ve used smtp server as smtp.gmail.com, is this the reason for the error, please reply as early as possible

http://www45.brinkster.com/dipankarghosh/email.aspx
Title: Access is denied   
Name: Dipankar Ghosh
Date: 9/11/2005 6:13:51 AM
Comment:
your code shows some errors while i`m trying to access the page, please click the following link and have a look, i`ve used smtp server as smtp.gmail.com, is this the reason for the error, please reply as early as possible

http://www45.brinkster.com/dipankarghosh/email.aspx
Title: It work !!   
Name: rezorat
Date: 9/10/2005 8:53:08 PM
Comment:
thinks for that article !!! very very interesting
Title: Not working   
Name: Muhammad Rafiq
Date: 6/16/2005 1:43:47 AM
Comment:
This code is not working properly
Title: About SMTP server   
Name: xyz
Date: 5/18/2005 8:13:19 AM
Comment:
Respected Sir,
We have tried the above code but we are facing problems like the "SendUsing" is not configured.
How to check whether the SMTP server is running.
do we have to do initial settings

Please send response to the following e-mail id:
csian@rediffmail.com
shwetampatil@yahoo.co.in
Title: Good!!   
Name: EnriqueK
Date: 10/18/2004 4:27:20 PM
Comment:
Nice your article, but I want to send a eamil using outlook, I want replace go to Menu File - send - Page by e-mail for other thing, for example a button that load my outlook with the page attach. you understand me???? if anybody know this please write my e-mail. kristiandem@hotmail.com thank you. Bye
Atte. EnriqueK

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 8/27/2008 11:04:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search