In this article, I am going to describe how to send email with attachment in ASP.NET. I am going to use System.IO, System.Net and System.Net.Mail namespaces in this article.
Download source code for Sending email with attachment in ASP.NET
This article is written as a response to the Forums thread posted at http://www.dotnetfunda.com/forums/thread3210-email-in-aspnet-happy-new-year-to-every-body.aspx where author has asked about how to send email in ASP.NET with attaching a file. To show the solution, I have prepared a sample asp.net form that looks like below.
Create a Send Email with Attachment form
Copy-paste below code in your .aspx page.
<tr><td colspan="2"><h3>Send Email with Attachment in ASP.NET</h3></td></tr>
<tr><td>To</td><td><asp:TextBox ID="txtTo" runat="server" /></td></tr>
<tr><td>Subject</td><td><asp:TextBox ID="txtSubject" runat="server" Columns="30" /></td></tr>
<tr><td>Body</td><td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="10" Columns="50" /></td></tr>
<tr style="font-weight:bold;"><td>Attachment</td><td><asp:FileUpload ID="FileUpload1" runat="server" /></td></tr>
<tr><td> </td><td><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="SendEmailWithAttachment" /></td></tr>
<asp:Label ID="lblMessage" runat="server" EnableViewState="false" />
This code snippet shall create a Send Email form that looks like above picture. Notice that on click on the button I have attached a server side click event that will fire SendEmailWithAttachment method, so let's write code for that.
protected void SendEmailWithAttachment(object sender, EventArgs e)
SendEmail(txtTo.Text.Trim(), "", "", txtSubject.Text.Trim(), txtBody.Text.Trim(), MailPriority.High, false);
In the above code, I have fired a SendEmail private method that will actually do the work of sending email. Below is the code for the SendEmail method.
Namespace to use
To work with SendEmail method code, you need to use following namespaces
SendEmail method code
private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
using (SmtpClient smtpClient = new SmtpClient())
using (MailMessage message = new MailMessage())
MailAddress fromAddress = new MailAddress("fromEmail@MyDomain.com", "FromEmail, myDomain.Com");
// You can specify the host name or ipaddress of your server
smtpClient.Host = "smtp.myDomain.com"; //you can also specify mail server IP address here
//Default port will be 25
smtpClient.Port = 25;
NetworkCredential info = new NetworkCredential("fromEmail@myDomain.com", "fromEmailPassword");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//From address will be given as a MailAddress Object
message.From = fromAddress;
message.Priority = priority;
// To address collection of MailAddress
message.Subject = subject;
if (ccAddress.Length > 0)
if (bccAddress.Length > 0)
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = isHtml;
// Message body content
message.Body = body;
// Add the attachment, if any
if (FileUpload1.PostedFile.ContentLength > 0)
Attachment attachment = new Attachment(Path.GetFullPath(FileUpload1.PostedFile.FileName));
// Send SMTP mail
lblMessage.Text = "Email sent to " + toAddress + " successfully !";
catch (Exception ee)
lblMessage.Text = ee.ToString();
In the above code snippet, I have instantiated the SmtpClient object that provides ability to send email in ASP.NET. After that I have instantiated the MailMessage object that will have necessary properties set to be used to send email (for example, to, from, network body attachment etc.). All the above procedures are same as we do for normal sending email (like setting the network credential for the SmtpClient object, specifying host name etc.)
Important lines of code to notice in the above method is related with the file attachment, notice the if condition block just above smtpClient.Send(message) line. In this if block I have first checked if any file has been selected by accessing the ContentLength property of the FileUpload control; if yes then I have instantiated the Attachment object and passed the path of the file in the constructor and then added to the message.Attachments collection. If you have more than one file to attach in the email, you can add them one by one in the message.Attachments collection.
Once the smtpClient.Send(message) line will execute successfully, an email will be sent and success message will be written on the page. In case any error, it will go to the catch block and write the actual error with complete description in the label.
I hope this article will be useful for Sathya who has asked this question in the Forums thread as well as others who are looking for sending email in ASP.NET with attachment.
Wrote this article on the 1st January 2011 at 7.38 AM. Thank you for reading and wish you a very happy new year!
I have written hundreds of .NET How to's solution (ebook + source code + video) series based on real time project problems, click here to get it.
Found interesting? Add this to: