Sunday, March 29, 2015

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

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

Description:

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 asp.net

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="http://www.w3.org/1999/xhtml">
<head runat="server">
      <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
 <script src="js/jquery.MultiFile.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <fieldset style="width:400px">
    <legend>Send Email With multiple Attachment</legend>  
     <table>
        <tr><td>Receiver Email ID :</td><td><asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td>Subject :</td><td><asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td>Message :</td><td><asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine"></asp:TextBox></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td>Attachment :</td><td>
            <asp:FileUpload ID="FileUpload1" runat="server" CssClass="multi"/></td></tr>
            <tr><td></td><td></td></tr>
        <tr><td></td><td>
            <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" /></td></tr>
    </table>
     </fieldset>
    </div>
    </form>
</body>
</html>

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)
    {
        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("Sender Email ID", "Sender name");
            message.To.Add(txtemail.Text);
            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.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "password");
            smtp.EnableSsl = true;
            smtp.Send(message);
            message.Dispose();
            Clear();
            foreach (string file in S1)
            {
                File.Delete(file);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    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.[To].Add(txtemail.Text)
        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
        Next
        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))
        Next
        message.IsBodyHtml = True
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential("sender@gmail.com", "password")
        smtp.EnableSsl = True
        smtp.Send(message)
        message.Dispose()
        Clear()
        For Each file__1 As String In S1
            File.Delete(file__1)
        Next
    End Sub
    Public Sub Clear()
        txtemail.Text = ""
        txtmessage.Text = ""
        txtsubject.Text = ""

    End Sub

download

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

    ReplyDelete