Sending Attachments Directly From a FileUpload Control

With previous versions of ASP.NET you couldn't attach an uploaded file directly to a mail message. Instead, you needed to save the file to disk, attach it to the mail message and then when sending the message succeeded you had to clean up the old file again.

This not only meant a lot of work, it also had some security implications. Because the file needed to be written to disk, the account used by the web server required write permissions to a folder to store the attachments.

Now, with the new constructors for the Attachment class from the System.Net.Mail namespace, these problems are gone....

Attaching the PostedFile to an E-mail Message

The Attachment class has a constructor that allows you to pass an IO.Stream and a file name. Conveniently, the FileUpload control has a FileContent property that returns the uploaded file as an IO.Stream. All that's left to do is to get the file name from the uploaded file using Path.GetFileName and you're good to go. The code below shows a working example of sending an attachment from an asp:FileUpload control directly. The download at the end of the chapter contains a web site with two pages: working examples of this code in VB.NET and C#. For the code to work, you need to import the System.Net.Mail and System.IO namespaces.

C#
if (FileUpload1.HasFile) { string toAddress = "you@yourprovider.com"; string fromAddress = "you@yourprovider.com"; string mailServer = "smtp.yourprovider.com"; MailMessage myMailMessage = new MailMessage(); myMailMessage.To.Add(toAddress); myMailMessage.From = new MailAddress(fromAddress); myMailMessage.Subject = "Test Message"; string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); Attachment myAttachment = new Attachment(FileUpload1.FileContent, fileName); myMailMessage.Attachments.Add(myAttachment); SmtpClient mySmtpClient = new SmtpClient(mailServer); mySmtpClient.Send(myMailMessage); } VB.NET
If FileUpload1.HasFile Then Dim toAddress As String = "you@yourprovider.com" Dim fromAddress As String = "you@yourprovider.com" Dim mailServer As String = "smtp.yourprovider.com" Dim myMailMessage As MailMessage = New MailMessage() myMailMessage.To.Add(toAddress) myMailMessage.From = New MailAddress(fromAddress) myMailMessage.Subject = "Test Message" Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) Dim myAttachment As New Attachment(FileUpload1.FileContent, fileName) myMailMessage.Attachments.Add(myAttachment) Dim mySmtpClient As New SmtpClient(mailServer) mySmtpClient.Send(myMailMessage) End If

Download Files

Source Code for this Article

Where to Next?

Wonder where to go next? You can post a comment on this article.

Doc ID 412
Full URL https://imar.spaanjaars.com/412/sending-attachments-directly-from-a-fileupload-control
Short cut https://imar.spaanjaars.com/412/
Written by Imar Spaanjaars
Date Posted 12/08/2006 16:25
Date Last Updated 06/15/2007 21:16
Date Last Reviewed 06/15/2007 21:16

Comments

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.