Later Today: ASP.NET Tutorial 2: Introduction to C#

(C#) Sending an Email with an Attachment (via Form)

Prerequisites: An understanding of how basic emails are sent with ASP.NET.

This article explains how to send an email that includes an attachment from a web form. This is particularly useful if you need to create a web form that allows users to upload their own file attachments. The examples in this article assume that you already have a form with a FileUpload element in it, named “fuAttachment”. This particular method is especially useful since it does not require you to save any files to the server before sending the email. The posted data is instead handled directly.

You should already by familiar with sending emails from ASP.NET before reading this article.

(C#) Required Framework Namespace:
using System.Net.Mail; //For sending an email
using System.IO; //For handling attachments

v Email Attachment Basics in C#

This section shows a basic example of sending an email attachment with an email message.

The method for sending an email with an attachment is virtually the same as sending an email without an attachment. The only difference is that you are adding to your mail object. The following example assumes that there is a FileUpload element on your webpage with an id of “fuAttachment”.

(C#) Simple HTML Email Example:
//Create the key email objects
MailMessage myemail = new MailMessage(); //Create message object
SmtpClient mysmtpserver = new SmtpClient("mail.example.com"); //Set mail server

//Set mail recipients
myemail.To.Add("user1@example.com");
myemail.To.Add("user2@example.com");

//Set email properties
myemail.From = new MailAddress("website@example.com", "Example.com Website");
myemail.Subject = "This is the Email Subject";
myemail.Body = "<h1>Email Notice!</h1><p>Hi there!</p><p>This is the body of the email. I hope it's helpful!</p><p>-Mr. Example </p>";
myemail.IsBodyHtml = true; //Send this as plain-text

//START ATTACHMENT CODE
if (fuAttachment.PostedFile != null
   && fuAttachment.PostedFile.ContentLength > 0)
{
   //Build an array with the file path, so we can get the file name later.
   string[] strAttachname = fuAttachment.PostedFile.FileName.Split('\\');

   //Create a new attachment object from the posted data and the file name
   Attachment mailAttach = new Attachment(
      fuAttachment.PostedFile.InputStream,  //Data posted from form
      strAttachname[strAttachname.Length - 1] //Filename (from end of our array)
   );

   //Add the attachment to our mail object
   mail.Attachments.Add(mailAttach);
}
//END ATTACHMENT CODE

//Send the email
mysmtpserver.Send(mail);

As you can see, the code for sending an email WITH an is exactly the same as sending one without, with some relatively simply additions.

mail.Attachments.Add() will add an attachment to your mail object. That attachment must be an Attachment object. This isn’t really a big deal, as you simply instantiate the Attachments class and pass in the data and the filename. But how do you get from a FileUpload element to an Attachment object with data? Here’s the logic behind the process:

  1. Validation: Only execute the attachment code if two conditions are met:
    • Data must have been posted for the FileUpload element.
    • The posted data cannot be “blank”.
  2. Get the file name. By default, fuAttachment.PostedFile.FileName will return the entire file path (ie: C:\\My Documents\myfile.doc). However, the only thing we want is the file name (ie: myfile.doc). The solution is easy-peasy… simply delimit the file path with backslash, and store the now-delimited data in an array. When we need the file name later, we simply grab it from the end of our array!
  3. Instantiate a new Attachment object. The class can take two parameters – the first is the data itself (easily obtained from the FileUpload’s posted data with fuAttachment.PostedFile.InputStream), and the second is a string for the filename (which also includes the file extension). As mentioned earlier, we simply get the filename from the end of our array with strAttachname[strAttachname.Length - 1] .
  4. Attach the file to our mail object with mail.Attachments.Add(mailAttach);

And that’s really all there is to it! If you want more details on sending en email from ASP.NET without the attachments, view the article "(C#) Sending an Email (Plain-Text & HTML)" instead.

No Comments

Post a Comment

You must be logged in to post a comment.

RSS Twitter LinkedIn Facebook
Doing neato things with JavaScript, please wait...