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:
- 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”.
- 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!
- 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] .
- Attach the file to our mail object with mail.Attachments.Add(mailAttach);
No Comments