Click here to Skip to main content
15,443,549 members
Articles / Web Development / ASP.NET
Posted 10 Oct 2004


83 bookmarked

ASP.NET email with multiple attachments

Rate me:
Please Sign up or sign in to vote.
4.28/5 (33 votes)
25 Jan 20053 min read
A simple web form that lets the user upload multiple attachments to create and send email.

Image 1


This application demonstrates the use of some simple but useful things you can do with ASP.NET; uploading, saving and deleting files, and sending email.


There are three things we need to do to be able to run this code and I'm going to assume that you will be trying out this code at home on a Windows desktop computer.

First, you need to create a virtual directory in IIS to host this web application and set write permission because we will be saving and deleting files that will be email attachments. In the virtual directory, create a folder called "attachments". This is where we will temporarily store the uploaded files to attach to the email.

The second thing to do is to make sure that the SMTP server is running and that it can relay mail. From the IIS MMC window, with "local computer" selected on the left, you should see the "Default SMTP Virtual Server", if not you need to install it. Right-click it, go into "Properties", the "Access" tab, and click the "Relay" button. With the "only the list below" radio button selected, you should see the local IP address:, if it's not there, you need to add it.

The last thing is to set up the SMTP server in the application. In the code, look for this line (line # 149):

SmtpMail.SmtpServer = "localhost";

You need to replace "localhost" with the name or IP address of your SMTP mail server. On a Windows desktop computer, "localhost" is the default value and usually works.

Using the code

First of all, we create a new MailMessage object and we call it "email". Then we set the From, To, Subject and Body properties as well as the BodyFormatof of this object to the values on our web form.

// Create a new blank MailMessage
 MailMessage email = new MailMessage ();

// Set the properties of the MailMessage to the
// values on the form
 if (rblMailFormat.SelectedItem.Text == "text")
  email.BodyFormat = MailFormat.Text;
  email.BodyFormat = MailFormat.Html;
 email.From = txtSender.Text;
 email.To = txtReceiver.Text;
 email.Subject = txtSubject.Text;
 email.Body = txtBody.Text;

Now for the meat and potatoes of this little web application; the following block of code checks the first of three Open File Dialogs of our web form (the Open File Dialog is an HTML File Field control to which we've added the runat="server" property). If there is a value, the file is uploaded, saved on the server, and added as an attachment to the email. The processing of the two other Open File Dialogs are just the same.

// Beginning of attachments processing
// Check the first open file dialog for a value
 if (ofdAttachment1.PostedFile != null)
  // Get a reference to PostedFile object
  HttpPostedFile ulFile = ofdAttachment1.PostedFile;
  // Get size of the file
  int nFileLen = ulFile.ContentLength; 
  // Make sure the size of the file is > 0
  if( nFileLen > 0 )
   // Get the file name
   strFileName = Path.GetFileName(ofdAttachment1.PostedFile.FileName);
   // Preced the file name with "attachments/" so 
   // the file is saved to our attachments directory
   strFileName = "attachments/" + strFileName;
   // Save the file on the server
   // Create the email attachment with the uploaded file
   MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
   // Attach the newly created email attachment
   // Store filename so we can delete it later
   attach1 = strFileName;

Then we send the email and finish by deleting the attachments.

// Set the SMTP server and send the email
 SmtpMail.SmtpServer = "localhost";
 SmtpMail.Send (email);

// Delete the attachements if any
 if (attach1 != null)

Points of Interest

This little project shows a practical use of the System.Web.HttpPostedFile and the System.Web.Mail.SmtpMail objects. The HttpPostedFile object is probably the simplest way to let a web user upload a file. I also tried to create the file attachment using a FileStream, hoping that I would be able to create the attachment without having to first save the file. I haven't figured that one out yet.


I've made a minor update to this article and the code, but it's basically the same application. Someone pointed out that I hadn't included the line that adds "attachments/" to the beginning of the filename for the second and third attachments. That's the only thing that has changed in the code. I've also made a few small changes to my explanations and comments.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
Canada Canada
I have been working with different web technologies (ASP, ASP.NET, ColdFusion, Perl/CGI) for the past 15 years. I live in Montreal, where I work for a little consultant group specializing in Microsoft technologies.

Comments and Discussions

QuestionSending Attached mail Pin
Member 105374254-Feb-14 19:12
MemberMember 105374254-Feb-14 19:12 
Suggestionyou should have used the System.Net.Mail its in .Net 2.0 Web.mail is older Pin
dnxit25-Jun-12 12:12
Memberdnxit25-Jun-12 12:12 
GeneralRe: you should have used the System.Net.Mail its in .Net 2.0 Web.mail is older Pin
Luc Archambault26-Jun-12 5:33
MemberLuc Archambault26-Jun-12 5:33 
GeneralMy vote of 2 Pin
Member 786771214-Aug-11 1:56
MemberMember 786771214-Aug-11 1:56 
GeneralMy vote of 5 Pin
Monjurul Habib1-Mar-11 7:39
professionalMonjurul Habib1-Mar-11 7:39 
Generaluisng it in a clas file [modified] Pin
svknair17-Nov-10 0:49
Membersvknair17-Nov-10 0:49 
GeneralCalculate Age in Java Script Pin
tojitendrapatel25-Sep-09 1:55
Membertojitendrapatel25-Sep-09 1:55 
QuestionI cant get this to work :( Please help? Pin
disloxic23-Sep-09 6:16
Memberdisloxic23-Sep-09 6:16 
Questionhow to clear multiple attachments?? Pin
zecko_asA_pyscho9-Jun-09 3:12
Memberzecko_asA_pyscho9-Jun-09 3:12 
GeneralMulitiple Email Send on a single click Pin
ashishshar12-May-09 0:03
Memberashishshar12-May-09 0:03 
GeneralMailAttachment obsolete Pin
navyjax25-Nov-08 11:32
Membernavyjax25-Nov-08 11:32 
GeneralError this coding ( please reply immediatly ) Pin
sivavivekanantha8-Jul-08 2:37
Membersivavivekanantha8-Jul-08 2:37 
GeneralRe: Error this coding ( please reply immediatly ) Pin
Member 857113216-Jan-12 5:31
MemberMember 857113216-Jan-12 5:31 
Questionemail sending Pin
vishnoithegreat12-May-08 22:47
Membervishnoithegreat12-May-08 22:47 
GeneralThe FileUpLoad controls Pin
raremother14-Sep-07 6:20
Memberraremother14-Sep-07 6:20 
GeneralWrapping an email with attachments as one attachment in C# .net 2005 Pin
Fire starter12-Jul-07 21:56
MemberFire starter12-Jul-07 21:56 
Questionwhat if it is hosted in a website Pin
lovedotnet7-Jun-07 1:17
Memberlovedotnet7-Jun-07 1:17 
AnswerRe: what if it is hosted in a website Pin
Luc Archambault7-Jun-07 12:31
MemberLuc Archambault7-Jun-07 12:31 
GeneralYou don't need to save posted file on the disk Pin
Simon Mourier2-Apr-07 4:07
MemberSimon Mourier2-Apr-07 4:07 
Questiontell me how to create inbox?? Pin
saeee4-Mar-07 23:23
Membersaeee4-Mar-07 23:23 
Questionhow can i recieve email at local server Pin
saeee27-Feb-07 21:02
Membersaeee27-Feb-07 21:02 
AnswerRe: how can i recieve email at local server Pin
Luc Archambault28-Feb-07 7:28
MemberLuc Archambault28-Feb-07 7:28 
GeneralRe: how can i recieve email at local server Pin
saeee4-Mar-07 18:22
Membersaeee4-Mar-07 18:22 
GeneralRe: how can i recieve email at local server Pin
saeee8-Mar-07 21:28
Membersaeee8-Mar-07 21:28 
GeneralCompilation error Pin
Rolf6729-Jul-06 20:32
MemberRolf6729-Jul-06 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.