Sunday, March 29, 2015

Send email with multiple attachments in (C#, VB)

Introduction: In this article I will explain how to send email with multiple attachments in


To implement this concept we can use the jQuery MultipleFile Upload PluginFollow the below given steps to implement this functionality:

Send email with multiple attachments in

Step 1: In your application add a new folder upload.
Step 2: Download the jQueryMultiple File Upload Plugin and add the file to js folder of application.

Html Markup Of page:
<html xmlns="">
<head runat="server">
      <script src=""></script>
 <script src="js/jquery.MultiFile.js" type="text/javascript"></script>
    <form id="form1" runat="server">
     <fieldset style="width:400px">
    <legend>Send Email With multiple Attachment</legend>  
        <tr><td>Receiver Email ID :</td><td><asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
        <tr><td>Subject :</td><td><asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td></tr>
        <tr><td>Message :</td><td><asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine"></asp:TextBox></td></tr>
        <tr><td>Attachment :</td><td>
            <asp:FileUpload ID="FileUpload1" runat="server" CssClass="multi"/></td></tr>
            <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" /></td></tr>

Write the below given code on button click event:

C# Code:
using System.Net.Mail;
using System.IO;

 protected void Button1_Click(object sender, EventArgs e)
            MailMessage message = new MailMessage();
            message.From = new MailAddress("Sender Email ID", "Sender name");
            message.Subject = txtsubject.Text;
            message.Body = txtmessage.Text;
            message.Priority = MailPriority.High;

            HttpFileCollection fileCollection = Request.Files;
            for (int i = 0; i < fileCollection.Count; i++)
                HttpPostedFile uploadfile = fileCollection[i];
                string fileName = Path.GetFileName(uploadfile.FileName);
                if (uploadfile.ContentLength > 0)
                    uploadfile.SaveAs(Server.MapPath("~/upload/") + fileName);
            string Uplodefile = Request.PhysicalApplicationPath + "upload\\";
            string[] S1 = Directory.GetFiles(Uplodefile);
            foreach (string files in S1)
                message.Attachments.Add(new Attachment(files));

            message.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("", "password");
            smtp.EnableSsl = true;
            foreach (string file in S1)
        catch (Exception ex)
    public void Clear()
        txtemail.Text = "";
        txtmessage.Text = "";
        txtsubject.Text = "";

VB Code:
Imports System.Net.Mail
Imports System.IO

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim message As New MailMessage()
        message.From = New MailAddress("Sender Email ID", "Sender name")
        message.Subject = txtsubject.Text
        message.Body = txtmessage.Text
        message.Priority = MailPriority.High

        Dim fileCollection As HttpFileCollection = Request.Files
        For i As Integer = 0 To fileCollection.Count - 1
            Dim uploadfile As HttpPostedFile = fileCollection(i)
            Dim fileName As String = Path.GetFileName(uploadfile.FileName)
            If uploadfile.ContentLength > 0 Then
                uploadfile.SaveAs(Server.MapPath("~/upload/") & fileName)
            End If
        Dim Uplodefile As String = Request.PhysicalApplicationPath + "upload\"
        Dim S1 As String() = Directory.GetFiles(Uplodefile)
        For Each files As String In S1
            message.Attachments.Add(New Attachment(files))
        message.IsBodyHtml = True
        Dim smtp As New SmtpClient()
        smtp.Host = ""
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential("", "password")
        smtp.EnableSsl = True
        For Each file__1 As String In S1
    End Sub
    Public Sub Clear()
        txtemail.Text = ""
        txtmessage.Text = ""
        txtsubject.Text = ""

    End Sub


Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

1 comment:

  1. Though Google Mail (Gmail) takes all the necessary steps, yet attacks on Gmail Security happens by the people with malicious intention. So, always beware and careful in relation of your Gmail Account, about configuration of Gmail Security Settings, about setting up your account with correct options for Gmail Privacy Settings.Gmail Support Service