codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

Simple ASP.NET Email function

Article by:  Jeff Anderson  ( 1362 ) (1/21/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: Standard code for sending emails from within an ASP.NET application
Viewed: 86785 times Rating (39 votes): 
 3.8 out of 5
 Rate this Article  Read Comments  Post Comments

Simple ASP.NET Email function



Sending an email from within ASP.NET is easy as long as you have access to an SMTP server to deliver it. When using this code replace the line

mail.yoursmtpmailserver.com


with the name of your smtp mail server.

Select All Code


You can integrate the code further into your site, for example by setting the .to property of the mail object to a variable, perhaps read in from a form or a database. As an example, you could loop through all the email fields in a database table and send a mailshot to all your clients.

FORMAT

The alternative to the HTML email format shown here is plain text, in which case you set the format as follows:

Select All Code


To acheive line breaks within text format use VBCRLF (VB Carriage Return Line Feed). As in :

Select All Code







CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Simple ASP.NET Email function'
Posted by :  Archive Import (Weng Kin,Cheah) at 02:32 on Monday, January 27, 2003
It's cool that we can send email via web through smtp? but can ASP.net retrieve mail via POP or IMAP? i cant seem to get it to retrieve mail..please help
Posted by :  Archive Import (Gokul) at 09:55 on Tuesday, February 18, 2003
I want to know is it possible to know the status of the mail sent (whether it reached or bounced back)
Posted by :  Archive Import (ruby) at 20:39 on Wednesday, March 12, 2003
hi! i was wondering how i could email the page in html. how can i get the html body part of the page, all the samples i checked had the body hard coded. is there a way to do that?

i'm desperate..
Posted by :  Archive Import (dave) at 03:46 on Thursday, March 20, 2003
ruby - this sample does send html - you can see from the line objMail.BodyFormat = MailFormat.Html
Posted by :  Archive Import (Webmist) at 13:56 on Thursday, April 10, 2003
Code is great but where do you exactly place it. I am not sure where it would go. Thanks!
Posted by :  Archive Import (Marko) at 17:51 on Wednesday, April 16, 2003
It would be interesting to see a demo on sending email in HTML format sending an image as an embedded object within the mail body. This is a simple operation in outlook express, but when using the source code from there in a dotnet class it sends the image with a path to original file location, such as "c:\inetpub\wwwroot\emailproj\images\myimage.gif" This of cource will not be seen by the recipient and so is not successful.
Posted by :  Archive Import (Chefy Tim) at 11:23 on Saturday, May 31, 2003
This seems like exactly what I am looking for but need to add the database idea you referenced above. I am new to ASP.NET and am wondering if you could provide an example of how to use this code to send an email to a list of users from a database.

Thanks,
Tim
Posted by :  Archive Import (Shobhit) at 06:29 on Saturday, June 21, 2003
Hi Toady,
I tried the same come and my company's smtp server is smtp.smtpserver.com
It is giving this error. Please can you help me why this error is coming?

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: The transport failed to connect to the server.
Posted by :  Archive Import (Nanda Kishore) at 01:24 on Monday, June 30, 2003
Hi !

Sending Mail From DotNet Is Easy But How To Receive Mails From Inbox and Also From Different Folders, Is it Possible.

Posted by :  Archive Import (cycloneboy) at 22:10 on Friday, August 22, 2003
I keep getting the same error as Shobhit.

System.Runtime.InteropServices.COMException: The transport failed to connect to the server.

This code worked earlier. The only thing is I updated windows - bad idea?
Posted by :  Archive Import (Jahan Shah) at 06:58 on Sunday, August 24, 2003
Hi all, I copied the code and save it as text2.aspx but when am trying to run it , it gives the following error

Error Type:
Active Server Pages, ASP 0221 (0x80004005)
The specified 'Import Namespace="System.Web.Mail"' option is unknown or invalid.
/test2.asp, line 1

Can any one help me to come out.
Posted by :  Archive Import (Ausama) at 06:00 on Thursday, September 04, 2003
I used the code above to send emails ...
i works well when i send emails to me collegues in the same company (i.e to the same domain as mine) but when i try to send to a yahoo mail for example an error occurs "Unable to access CDO.Message object"
i hope you can help me in that
Posted by :  Archive Import (adey) at 10:50 on Tuesday, September 09, 2003
hi toady,
the code is working fine, but i did not receive the mail in the address i sent it to. (i.e i sent it to dexooral@yahoo.co.uk and it wasn't there). what do you think i should do? please help me!!!
my email address is: dexoora10@aol.com.
Thanks for your help
Adey
Posted by :  Hussain at 02:12 on Tuesday, December 23, 2003
Hi

This code works great for only exchange server users. Mails does not go to outside world. Like hotmail, yahoo..etc. Works for the SMTP id's only.

Do i need to set any thing at the Exchange server.?

Error follows..

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.

Exception Details: System.Runtime.InteropServices.COMException: The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for somename@hotmail.com

Cheers!!
Hussain
z3smh@se.com.sa
Posted by :  ThaoGen999 at 23:11 on Friday, May 28, 2004
sdf
Posted by :  vishnutejak at 04:16 on Wednesday, January 12, 2005
Hi,
I am using the same code that you have given in this site.
But it is giving me an error saying
"SERVER HAS REJECTED ONE OR MORE RECIPIENT ADDRESSES. Relaying is temporarily denied"
"Error No 450 4.7.1"

I donot know where the error is coming because i am giving the correct SMTPMail Server address only.

Kindly help me out.

Thanks in advance,
Vishnu
Posted by :  Atit at 00:17 on Thursday, February 10, 2005
Actually I can't get any error by writing that code but I never get the answer from the destination address.What could be the problem.

<Added>

and hoe can I get the response?

<Added>

and how can I get the response?
Posted by :  ryan_sen at 06:25 on Thursday, March 17, 2005
This is the error i am getting, i user SMTP server as "localhost"


The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for .......................

What to do :-(

Posted by :  ryan_sen at 22:58 on Thursday, March 17, 2005
Hey i got the solution, i was not specifing the servername corertcly,
if any one getting the sam error as i got, check ur servername.
I fails to relay if the srver name is not corerct.

Enjoi
Posted by :  anoopKK at 22:07 on Wednesday, March 30, 2005



Hi Toady,


i want to create a mailserver like yahoo ,for sending and receiving mails in mime.so which are the components need for my project please send more details.
my toool----C#,asp.net,exchange server,windows 2000 server
Posted by :  eclipse at 13:00 on Monday, May 30, 2005
Hi,
I have been getting the same error as ryan_sen where it displays

The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for .....

I was using "localhost" initially and i have changed it to "notebook" but to no avail. Please advice. Thanks
Posted by :  ravindra at 01:34 on Thursday, August 18, 2005
Hi,

I tried the same come and my company's smtp server is smtp.smtpserver.com
It is giving this error. Please can you help me why this error is coming?

I used the code above to send emails ...
i works well when i send emails to me collegues in the same company (i.e to the same domain as mine) but when i try to send from my client system with another DNS Ip Address, an error occurs "Unable to access CDO.Message object"

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: The transport failed to connect to the server


<b>i hope you can help me in that </b>

Posted by :  Multitrax at 09:44 on Friday, August 19, 2005
guys,

did you add the ip address for the server you are hosting the page on to your mailserver.

on exchange server you need to set the relay restrication as well as the connection controls.

to test this: <b>telnet yoursmtp.yourserver.com 25</b>

it should let you connect to port 25, if it doesn't check with your network admin.

Multitrax

<Added>

to test this: telnet yoursmtp.yourserver.com 25

it should let you connect to port 25, if it doesn't check with your network admin.

Multitrax
Posted by :  zahra at 04:02 on Saturday, August 20, 2005
good, but how can i have access to an SMTP server ?i hava visual studio.net installed,is that enough?in addition i have windows xp sp2.
Posted by :  manu_priyam at 23:29 on Tuesday, March 21, 2006
i have an ASP.Net application that has to send an XML file through an Intranet server to the exchange server at a remote location. What all configurations do i need to do at the servers. I dont have any access to the exchange server.
Posted by :  byju at 04:35 on Monday, June 05, 2006


IT SEEMS TO BE SIMPLE BUT HOW WHOULD I GET AN ACCESS TO THE SMTP SERVER
Posted by :  Mike Anderson at 04:04 on Wednesday, August 30, 2006
Hi,

Can anyone please help.

My email function works 100% when I run the site locally on VS 2005 but as soon as its uploaded and live the function doesn't work.

<Added>

When submitting the form the user sends his details through an email. Like I said it works locally but not live.

Any help appreciated!
Posted by :  VenkataN at 23:47 on Wednesday, September 06, 2006
Hi I have tried to send the mail I have used the same code and I mentioned localhost as SMTP server. I am able send the mail. But I am not getting the mail in inbox of the sent address. I have mentioned the same address as from address and I receiveing the Delayed Notifications.
Please help me...
Posted by :  dhaval_c# at 04:03 on Monday, January 01, 2007
this is great code.. thaks
Posted by :  kornieme at 10:26 on Wednesday, February 07, 2007
Question, how would I get response back if I needed to add in CheckBoxList features?

<asp:CheckBoxList runat="server" ID="contact" RepeatColumns="2">
<asp:ListItem value="1">email</asp:ListItem>
<asp:ListItem value="2">phone</asp:ListItem>
</asp:CheckBoxList>

Need to have the information from the checkbox come back in the email as well.
Posted by :  e.sumilang at 22:01 on Thursday, June 07, 2007
i used this code using dreamweaver 8 and its work! but my problem is the email will not sent and it stock to queue folder? i test my smtp and it work properly! what was the error then?


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject








Recent Forum Threads
• Re: Perl Script - File Handling.
• Open a file from website
• Re: to open 5 terminals from one and also execute different commands on each terminal
• read a selected multiple line, those should match some of the values read in the files..
• Help me please
• Empty the contents of a file.
• Month Start, End dates
• show hide problem in dynamic table creation
• Re: ASP Sendmail has huge delay - ANYONE??


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008