Sending E-mails with Attachments using ASP.NET 2.0
page 1 of 6
Published: 14 Jul 2006
Unedited - Community Contributed
Abstract
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
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 81363/ 104

Introduction

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: 10/1/2010 12:49:24 PM
Comment:
Plz send me asp.net language
Title: good   
Name: vimalraj
Date: 9/18/2010 12:42:24 AM
Comment:
i used your sending mail coding for asp.net . 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: 5/1/2009 10:40:11 AM
Comment:
\
\
Title: NVP   
Name: Nisheeth
Date: 1/9/2009 5:15:40 AM
Comment:
Nice one...
Title: Re: help me   
Name: Rich
Date: 1/7/2009 7:50:32 PM
Comment:
Eshwar,

What kind of error do you get?
Title: help me   
Name: Eshwar
Date: 1/7/2009 12:47:36 AM
Comment:
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: 1/2/2009 2:43:39 AM
Comment:
This is really helpful.
Title: Thanks for posting this   
Name: KnightWing19
Date: 12/3/2008 4:21:23 PM
Comment:
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: 9/8/2008 1:52:14 AM
Comment:
It is a very good utitlity. I need it on my project. So Thanksssssssssssss.
Title: Excellent Articles   
Name: Mohammad Javed
Date: 7/8/2008 6:22:27 AM
Comment:
very good articles......
Title: Don't forget to dispose your attachment   
Name: Greg Graham
Date: 5/13/2008 6:14:54 PM
Comment:
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: 5/7/2008 10:50:35 PM
Comment:
Can this code work on ASP.NET 2.0 Medium trust share hosting?
Title: Re Problem   
Name: Rich
Date: 5/1/2008 10:49:33 AM
Comment:
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: 5/1/2008 1:46:31 AM
Comment:
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: 4/17/2008 1:54:29 AM
Comment:
This code is helpful, but need little descrpition on the topic.
Title: Thank you   
Name: jgonzalez
Date: 2/26/2008 6:01:10 PM
Comment:
It was very helpful, thanks =)
Title: Great   
Name: Kalyana Sundaram
Date: 2/6/2008 6:46:03 AM
Comment:
Usefull one!!
Title: Thanks   
Name: Rob
Date: 1/7/2008 12:40:12 PM
Comment:
Precise, to the point, accurate, and the code works!
Thanks very much Rich
Title: Thanks   
Name: Arindam
Date: 11/29/2007 2:05:56 PM
Comment:
Its really cool...
sending attachments and all stuff really awesome..

Thanks a lot!!!
Title: Error?   
Name: Sudipta
Date: 9/1/2007 2:06:31 AM
Comment:
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: 7/24/2007 5:06:17 AM
Comment:
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: 2/23/2007 4:51:41 AM
Comment:
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: 2/13/2007 1:33:01 PM
Comment:
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: 12/27/2006 2:10:45 AM
Comment:
Thanks for the code. its the best one I am using.
Title: email   
Name: dave
Date: 10/10/2006 2:08:31 PM
Comment:
email in effect! excellent article!
Title: Good one   
Name: Amrit
Date: 7/25/2006 9:34:24 AM
Comment:
Really to the point.
Title: Thanks!   
Name: Jason
Date: 7/17/2006 1:55:56 PM
Comment:
This was a great help! Simple and to the point.
Title: Code   
Name: Dharmesh
Date: 7/14/2006 2:35:28 PM
Comment:
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-2014 ASPAlliance.com  |  Page Processed at 4/20/2014 4:45:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search