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”);
3 comment(s) so far
in my computer i have also use email using C
Wonder post...
Great post. I like this.