In this article I will explain with an example, how to send email with multiple attachments using Multiple FileUpload control in ASP.Net 4.5 using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of:
TextBox – For capturing the values of Recipient Email address, Subject, Body, Gmail account email address, Gmail account password.
FileUpload – For attaching multiple files to the email.
The FileUpload control has been assigned with the AllowMultiple property which is set to true.
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px">To:</td>
        <td><asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>Subject:</td>
        <td><asp:TextBox ID="txtSubject" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td valign="top">Body:</td>
        <td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="150" Width="200"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>File Attachments:</td>
        <td><asp:FileUpload ID="fuAttachment" runat="server" AllowMultiple="true"/></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>Gmail Email:</td>
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>Gmail Password:</td>
        <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnSend" Text="Send" OnClick="SendEmail" runat="server"/></td>
    </tr>
</table>
 
 
Mail Server Settings in Web.Config file
The following Mail Server settings need to be saved in the Web.Config file.
<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network
                host="smtp.gmail.com"
                port="587"
                enableSsl="true"
                defaultCredentials="true/>
        </smtp>
    </mailSettings>
</system.net>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Net.Configuration;
 
VB.Net
Imports System.IO
Imports System.Web
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Imports System.Net.Configuration
 
 
Sending email with attachment using Gmail SMTP Account
MailMessage Class Properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) email address.
CC – Carbon Copies (if any).
BCC – Blind Carbon Copies (if any).
Subject – Subject of the email. 
Body – Body of the email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any).
ReplyTo – ReplyTo email address.
 
SMTP Class Properties
Following are the properties of the SMTP class.
Host – SMTP Server URL (Gmail: smtp.gmail.com).
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True).
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password).
Port – Port Number of the SMTP server (Gmail: 587).
When the SendEmail Button is clicked, the Recipient email address (to), the Sender email address (from), Subject and Body values are fetched from their respective fields and are set into an object of the MailMessage class.
A FOR EACH loop is executed over a FileUpload control and if it has attachment then all the selected files is added to the Attachments List of the MailMessage Object.
Then, object of the SmtpClient class is created and the settings of the Mail Server such has HostPortDefaultCredentials and EnableSsl are fetched from the mailSettings section of the Web.Config file and are set in respective properties of the SmtpClient class object.
And Username and Password values are passed as parameter to NetworkCredential class from their respective fields.
Finally, the email is being sent and success the message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void SendEmail(object sender, EventArgs e)
{
    //Read SMTP section from Web.Config.
    SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
 
    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        if (fuAttachment.HasFile)
        {
            foreach (HttpPostedFile file in fuAttachment.PostedFiles)
            {
                string fileName = Path.GetFileName(file.FileName);
                mm.Attachments.Add(new Attachment(file.InputStream, fileName));
            }
        }
        mm.IsBodyHtml = false;
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.Host = smtpSection.Network.Host;
            smtp.EnableSsl = smtpSection.Network.EnableSsl;
            NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
            smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
            smtp.Credentials = NetworkCred;
            smtp.Port = smtpSection.Network.Port;
            smtp.Send(mm);
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
        }
    }
}
 
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
    'Read SMTP section from Web.Config.
    Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
 
    Using mm As MailMessage = New MailMessage(txtEmail.Text, txtTo.Text)
        mm.Subject = txtSubject.Text
        mm.Body = txtBody.Text
        If fuAttachment.HasFile Then
            For Each file As HttpPostedFile In fuAttachment.PostedFiles
                Dim fileName As String = Path.GetFileName(file.FileName)
                mm.Attachments.Add(New Attachment(file.InputStream, fileName))
            Next
        End If
 
        mm.IsBodyHtml = False
        Using smtp As SmtpClient = New SmtpClient()
            smtp.Host = smtpSection.Network.Host
            smtp.EnableSsl = smtpSection.Network.EnableSsl
            Dim NetworkCred As NetworkCredential = New NetworkCredential(txtEmail.Text, txtPassword.Text)
            smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
            smtp.Credentials = NetworkCred
            smtp.Port = smtpSection.Network.Port
            ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
        End Using
    End Using
End Sub
 
 
Screenshot
Email Form
Send email with multiple attachments using Multiple FileUpload control in ASP.Net 4.5
 
The received email
Send email with multiple attachments using Multiple FileUpload control in ASP.Net 4.5
 
 
Downloads