Reader Level:
Article
ASP.NET Programming

Send Emails With Attachment in ASP.NET

By Neha Sharma on Feb 16, 2013
In this article you will see how to send an email with an attachment using C# in ASP.NET.

Introduction

Send emails using Simple Mail Transfer Protocol (SMTP) with attachments.

Requirement

Namespace:
using System.Net.Mail;

Source code

<html xmlns="http://www.w3.org/1999/xhtml">
<
head id="Head1" runat="server">
    <title>Send Mail using asp.net</title>
    <style type="text/css">
        .auto-style1 {
           
color: #FFFFFF;
           
font-weight: bold;
           
background-color: #3399FF;
        } 
       
.auto-style2 {
           
background-color: #3399FF;
        }
   
</style>
</
head>
<
body>
    <form id="form1" runat="server">
        <div>
            <table style="border-style: solid; border-color: inherit; border-width: 1px; background-color: #49A3FE;" align="center">
                <tr>
                    <td colspan="2" align="center" class="auto-style1">Send Mail with Attachment using asp.net
                    
</td>
                </tr>
                <tr>
                    <td class="auto-style1">From:
                   
</td>
                    <td>
                        <asp:TextBox ID="txt_from" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">To:
                   
</td>
                    <td>
                        <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">Subject:
                   
</td>
                    <td>
                        <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">Attach a file:
                   
</td>
                    <td>
                        <asp:FileUpload ID="fileUpload1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td valign="top" class="auto-style1">Body:
                   
</td>
                    <td>
                        <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2"></td>
                    <td>
                        <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" Style="height: 26px" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</
body>
</
html>

Design

sendmail.png

Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
   
protected void Page_Load(object sender, EventArgs e)
    { 
    }
   
protected void btnSubmit_Click(object sender, EventArgs e)
    {
      
MailMessage mail = new MailMessage(); 
        mail.To.Add(txtTo.Text);
 
       
//mail.To.Add
        mail.From = new MailAddress(txt_from.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtBody.Text;
        mail.IsBodyHtml =
true;
 
       
//Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(
new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }
       
SmtpClient smtp = new SmtpClient();
        smtp.Host =
"smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("your email id", "your password");
       
//Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}

Run

Press "Ctrl + S" or save all the work then click "F5" to run the page. The page looks as in the following images:

sendmail1.png

Using this form you can send emails with attachments.