Sending E-mails with Attachments using ASP.NET 2.0
page 1 of 6
Published: 14 Jul 2006
Unedited - Community Contributed
In this article Rich demonstrates how to send e-mails with attachments using ASP.NET 2.0 with the help of simple code samples.
by Rich Crawford
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36869/ 56


Download Sample Code

In this article we will look at two demonstrations for sending e-mail messages with attachments in ASP.NET 2.0. 

In earlier versions of ASP.NET (1.0 and 1.1), we used the System.Web.Mail class to send e-mail messages.  ASP.NET 2.0 replaced the System.Web.Mail class with the new and improved System.Net.Mail class.

The System.Web.Mail class uses the System.Web.Mail.SmtpMail class to handle the actual delivery of the e-mail message.  This has also been replaced by a new class (System.Net.Mail.SmtpClient) for the System.Net.Mail class. 

Sending attachments is just as simple in the new class as it was in the old.  What excites me about the new class is the ability to use attachments by filename or contentstream.  The latter of the two comes in very handy.

Attachments are handled by the System.Net.Mail.Attachment and System.Net.Mail.AttachmentCollection classes.  We will see how these are used in the following demonstrations.

View Entire Article

User Comments

Title: Good   
Name: Neetu Sachan
Date: 2010-10-01 12:49:24 PM
Plz send me language
Title: good   
Name: vimalraj
Date: 2010-09-18 12:42:24 AM
i used your sending mail coding for . it is very helpful for me. before that i searched sending mail coding for other websites. they did not give a correct format. i am extremely happy to say this well done.
Title: Re: Re: help me   
Name: Alex
Date: 2009-05-01 10:40:11 AM
Title: NVP   
Name: Nisheeth
Date: 2009-01-09 5:15:40 AM
Nice one...
Title: Re: help me   
Name: Rich
Date: 2009-01-07 7:50:32 PM

What kind of error do you get?
Title: help me   
Name: Eshwar
Date: 2009-01-07 12:47:36 AM
It is working fine in my local machine. But when i am trying to execute it from remote machine it is not working.Please help me
Title: Nice article   
Name: Firoz
Date: 2009-01-02 2:43:39 AM
This is really helpful.
Title: Thanks for posting this   
Name: KnightWing19
Date: 2008-12-03 4:21:23 PM
This was a real help thank you so much!

I was wondering if you knew what the max file size that this could send was?
Title: Very Good   
Name: Very Good
Date: 2008-09-08 1:52:14 AM
It is a very good utitlity. I need it on my project. So Thanksssssssssssss.
Title: Excellent Articles   
Name: Mohammad Javed
Date: 2008-07-08 6:22:27 AM
very good articles......
Title: Don't forget to dispose your attachment   
Name: Greg Graham
Date: 2008-05-13 6:14:54 PM
In Listing 1 above that uses a filename to initialize an Attachment, don't forget to explicitly call Dispose() on either the attachment or the message after it has been sent. The reason is that the attachment file will be silently opened in the Attachment constructor and held open even after the message is sent.

In Listing 2, an open stream is passed to the Attachment constructor. Normally, you would remember to close the stream explicitly; but the assumption here is that ASP.Net closes the stream when its done processing the request.
Title: medium trust   
Name: Lee
Date: 2008-05-07 10:50:35 PM
Can this code work on ASP.NET 2.0 Medium trust share hosting?
Title: Re Problem   
Name: Rich
Date: 2008-05-01 10:49:33 AM
If the code didn't return any errors, it probably has to do with your smtp gateway. The gateway needs to allow relaying for your server's IP address.

Try using your own email address as the from address to see if the smtp server sends you an NDR message.
Title: problem   
Name: Ravish
Date: 2008-05-01 1:46:31 AM
hello i had try this code in my application it saw its working but i am not getting mail so is there any system configuration please tell me
Thanks in advance
Title: Feed-Back   
Name: Balu
Date: 2008-04-17 1:54:29 AM
This code is helpful, but need little descrpition on the topic.
Title: Thank you   
Name: jgonzalez
Date: 2008-02-26 6:01:10 PM
It was very helpful, thanks =)
Title: Great   
Name: Kalyana Sundaram
Date: 2008-02-06 6:46:03 AM
Usefull one!!
Title: Thanks   
Name: Rob
Date: 2008-01-07 12:40:12 PM
Precise, to the point, accurate, and the code works!
Thanks very much Rich
Title: Thanks   
Name: Arindam
Date: 2007-11-29 2:05:56 PM
Its really cool...
sending attachments and all stuff really awesome..

Thanks a lot!!!
Title: Error?   
Name: Sudipta
Date: 2007-09-01 2:06:31 AM
It gives me error on smtp.Send(msg). can you help me out of this critical situation. when i am putting some value over email recipent address and sender address it gives me error on pressing send button. pls help me out..
Title: Mr.   
Name: Mekonnen
Date: 2007-07-24 5:06:17 AM
Hi Rich Crawford;

I got the article very intersting. But the problem I have is when I used the code to send mails using emails from the SQL SERVER 2000 Database, the mail is treated as Junk mail.

Can anybody help me? Thanks
Title: Spam   
Name: Hare Ram Yadav
Date: 2007-02-23 4:51:41 AM
Why mails sent by this method are declared spam by McaFee SpamKiller ? How can we protect it from being spammed ?
Title: Virus Scan?   
Name: curious
Date: 2007-02-13 1:33:01 PM
Using contentstream, do I need to worry about potential virus in theattachment? How do I do virus scan on it?
Title: Best Code   
Name: Rajiv
Date: 2006-12-27 2:10:45 AM
Thanks for the code. its the best one I am using.
Title: email   
Name: dave
Date: 2006-10-10 2:08:31 PM
email in effect! excellent article!
Title: Good one   
Name: Amrit
Date: 2006-07-25 9:34:24 AM
Really to the point.
Title: Thanks!   
Name: Jason
Date: 2006-07-17 1:55:56 PM
This was a great help! Simple and to the point.
Title: Code   
Name: Dharmesh
Date: 2006-07-14 2:35:28 PM
How to send e-mail in ASP.NET 1.0 with visual studio 2003 version.
if you have this on in this version plz send we.
i will wait for your reply.

Product Spotlight
Product Spotlight 

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

©Copyright 1998-2023  |  Page Processed at 2023-09-28 4:13:37 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search