Geeks With Blogs
Mayank Sharma
I wanted to give users an ability to send multiple attachments from the web application. I did not want anything fancy, just a few FileUpload controls on the page and then send the email. So I dropped five FileUpload controls on the web page and created a function to send email with multiple attachments.
Here’s the code:
public static void SendMail(string fromAddress, string toAddress, string subject, string body, HttpFileCollection fileCollection)
        // CREATE THE MailMessage OBJECT
        MailMessage mail = new MailMessage();
        // SET ADDRESSES
        mail.From = new MailAddress(fromAddress);
        // SET CONTENT
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = false;
        // ATTACH FILES FROM HttpFileCollection
        for (int i = 0; i < fileCollection.Count; i++)
            HttpPostedFile file = fileCollection[i];
            if (file.ContentLength > 0)
                Attachment attachment = new Attachment(file.InputStream, Path.GetFileName(file.FileName));
        // SEND MESSAGE
        SmtpClient smtp = new SmtpClient("");
And here’s how you call the method:
protected void uxSendMail_Click(object sender, EventArgs e)
        HttpFileCollection fileCollection = Request.Files;
        string fromAddress = "";
        string toAddress = "";
        string subject = "Multiple Mail Attachment Test";
        string body = "Mail Attachments Included";
        HelperClass.SendMail(fromAddress, toAddress, subject, body, fileCollection);       
Posted on Wednesday, February 9, 2011 10:16 AM C# , ASP.NET | Back to top

Comments on this post: Send Multiple InMemory Attachments Using FileUpload Controls

# re: Send Multiple InMemory Attachments Using FileUpload Controls
Requesting Gravatar...
Thanks for the code. saved my loads of time.
Left by Anjana on Aug 29, 2011 12:58 AM

Your comment:
 (will show your gravatar)

Copyright © bullpit | Powered by: