How to upload multiple files from a static html form and attach them in an email using C#

Bookmark and Share
Monday, April 06 2009

I've come across a few forums posts where people have asked this question under special circumstances.

The scenario: A web application with some sort of web service or a simple web handler needs to request multiple files and email them, but where the data is posted from disparate clients.  These clients could be static html forms, desktop applications, etc.

The following is one simplified example of how you might achieve this..

First, let's look at the html form.

<form id="MyForm" method="post" enctype="multipart/form-data" action="http://mydomain.com/FeedMe.ashx">
<p>Name: <input type="text" id="name" name="name" ></input></p>
<p>Email: <input type="text" id="email" name="email" ></input></p>
<p>File 1: <input type="text" id="file1" name="file1" ></input></p>
<p>File 2: <input type="text" id="file2" name="file2" ></input></p>
<p><input type="submit" id="submit" name="submit" value="Submit" ></input></p>
</form>

 

We are submitting two files, file1 and file2, to a web handler called FeedMe.ashx.

Now let's take a look at the code within our web hander:

using System;
using System.Collections;
using System.Web;
using System.Net.Mail;
 
namespace MyWebHandler
{
 public class EmailFiles : IHttpHandler
 {
   public void ProcessRequest(HttpContext context)
    {
     string name = context.Request.Form["name"]
     string email = context.Request.Form["email"];
 
      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(email, name);
      mail.Subject = “You haz a filez!”;
      mail.Body = “You can haz multiple filez now!”;
      mail.To.Add(new MailAddress(“me@email.com”, “me”));
 
      HttpFileCollection MyFiles context.Request.Files;
      foreach (String fileKey in MyFiles)
      {
        mail.Attachments.Add(new Attachment(MyFiles[fileKey].InputStream, MyFiles[fileKey].FileName.ToString()));
      }
 
      SmtpClient client = new SmtpClient("localhost");
      client.Send(mail);
      context.Response.Write(“response to client”);
    }
 
    public bool IsReusable
      {
        get
          {
            return false;
          }
      }
    }
 }
 

Looking at the web handler, first we request our name and email form elements from the form submission:

     string name = context.Request.Form["name"]
     string email = context.Request.Form["email"];
 
Next, we create our email: 
 
      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(email, name);
      mail.Subject = “You haz filez!”;
      mail.Body = “You can haz multiple filez now!”;
      mail.To.Add(new MailAddress(“me@email.com”, “me”));
 
 

Now, we'll request the submitted files from context and assign them to MyFiles.

Using a foreach loop, we loop through the collection and grab the key for each posted file.

Once we have the key, we Add a new mail attachement, passing the InputStream of MyFiles for the given key as the File, along with the name of that file.

(Note, you will want to filter out the local path in the FileName)

 

 

      HttpFileCollection MyFiles context.Request.Files;
      foreach (String fileKey in MyFiles)
      {
        mail.Attachments.Add(new Attachment(MyFiles[fileKey].InputStream, MyFiles[fileKey].FileName.ToString()));
      }

 

Once we've cycled through all the files posted and attached them to the email, we'll send the email:

      SmtpClient client = new SmtpClient("localhost");
      client.Send(mail);
      context.Response.Write(“response to client”);

 

Tagged as: , , , , , ,

Bookmark and Share

3 comment(s) so far

Post your comment

Thanks for your comments


To use your gravatar, enter the email or username associated with that gravatar.

  • Comment

 

Office Cam

Search

My Tweets

  • johnny861: A man with explosives has taken hostages at the Discovery Channel bldg. I guess he didn't like shark week.
  • johnny861: Ford's bet on Facebook pays off: http://tinyurl.com/2cjs3as
  • johnny861: Why does "Killer Klowns from Outer Space" have 3.5 stars on netflix?
  • johnny861: US Government legalizes jail breaking for smart phones. http://tinyurl.com/397qbk6
  • johnny861: Being fairly rooted in all things Microsoft, I'm surprised to discover that the majority of my software purchases this year are Apple based.
  • johnny861: It's amazing how many people fall for this... http://i.imgur.com/VlYaV.jpg I bet their grandparents were hunting martians in New Jersey
  • johnny861: If the late night test of the Emergency Broadcast System no longer alerts you with its abrupt sound, you might just have a sleeping problem.
 

Powered By: