Sending E-mails with Attachments using ASP.NET 2.0
page 4 of 6
by Rich Crawford
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36026/ 30

Simple Scenario

You want to make a simple WebForm that allows your students to submit a simple e-mail message with a file attachment to submit a term paper.  Since the attachment is being sent via e-mail to a recipient, there is no need for you to store the attachment on the file system.

To achieve this, I would simply make a WebForm with the appropriate fields.

Figure 1 - WebForm for message

<img width=355 height=355 src="/ArticleFiles/913/image001.gif">

The listing does not include any error checking and also assumes that the student will be uploading a MS Word document.  I kept this short and sweet for demonstration purposes, but it is functional.

Listing 2 - Attaching a file with contentstream

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                            Handles Button1.Click
 
  Dim msg As New System.Net.Mail.MailMessage()
  Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")
  Dim fromadx As New System.Net.Mail.MailAddress("sample@yourdomain.com""Term
  Paper Mailer")
  Dim toadx As New System.Net.Mail.MailAddress("recipient@yourdomain.com",
  "Professor Plumb")
  msg.From = fromadx
  msg.To.Add(toadx)
  msg.Subject = txtSubject.Text
  msg.Body = txtBody.Text
   If FileUpload1.HasFile = True Then
     msg.Attachments.Add(New
     System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, "Final Term
     Paper.doc"))
   End If
  smtp.Send(msg)
End Sub

Another good use for the contentstream attachment is when the file exists in a byte() array.  The byte() data can be put into a System.IO.MemoryStream which is then readable by the attachment object.

I have used that technique with SQL database image fields.

Listing 3 - MemoryStream example

'Assumes you have a datatable with SQL image data in it.
Dim ms as new System.IO.MemoryStream(Table1.rows(0).item("ImageFieldName"))

The MemoryStream now contains the contents of the image data from Table1.  It can be read by the System.Net.Mail.Attachment object.


View Entire Article

User Comments

Title: Good   
Name: Neetu Sachan
Date: 2010-10-01 12:49:24 PM
Comment:
Plz send me asp.net language
Title: good   
Name: vimalraj
Date: 2010-09-18 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: 2009-05-01 10:40:11 AM
Comment:
\
\
Title: NVP   
Name: Nisheeth
Date: 2009-01-09 5:15:40 AM
Comment:
Nice one...
Title: Re: help me   
Name: Rich
Date: 2009-01-07 7:50:32 PM
Comment:
Eshwar,

What kind of error do you get?
Title: help me   
Name: Eshwar
Date: 2009-01-07 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: 2009-01-02 2:43:39 AM
Comment:
This is really helpful.
Title: Thanks for posting this   
Name: KnightWing19
Date: 2008-12-03 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: 2008-09-08 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: 2008-07-08 6:22:27 AM
Comment:
very good articles......
Title: Don't forget to dispose your attachment   
Name: Greg Graham
Date: 2008-05-13 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: 2008-05-07 10:50:35 PM
Comment:
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
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: 2008-05-01 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: 2008-04-17 1:54:29 AM
Comment:
This code is helpful, but need little descrpition on the topic.
Title: Thank you   
Name: jgonzalez
Date: 2008-02-26 6:01:10 PM
Comment:
It was very helpful, thanks =)
Title: Great   
Name: Kalyana Sundaram
Date: 2008-02-06 6:46:03 AM
Comment:
Usefull one!!
Title: Thanks   
Name: Rob
Date: 2008-01-07 12:40:12 PM
Comment:
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
Comment:
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
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: 2007-07-24 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: 2007-02-23 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: 2007-02-13 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: 2006-12-27 2:10:45 AM
Comment:
Thanks for the code. its the best one I am using.
Title: email   
Name: dave
Date: 2006-10-10 2:08:31 PM
Comment:
email in effect! excellent article!
Title: Good one   
Name: Amrit
Date: 2006-07-25 9:34:24 AM
Comment:
Really to the point.
Title: Thanks!   
Name: Jason
Date: 2006-07-17 1:55:56 PM
Comment:
This was a great help! Simple and to the point.
Title: Code   
Name: Dharmesh
Date: 2006-07-14 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-2024 ASPAlliance.com  |  Page Processed at 2024-03-19 6:34:53 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search