Posted by : Anwar Hossain
Category : Mail sending in asp.net c#

Send mail with attachment file in ASP.Net C #

Dear viewers I will show how to create feedback from with attachment file in ASP.Net C #. Sometimes we need to create feedback from need to send message with file in this case we can use FileUpload control and need to  write extra code for file attachment. In this tutorial I have given complete code for sending mail with attachment in ASP.Net.

Feedback form with Attachment in ASP.Net C#

HTML

<html >

<head runat="server">

    <title></title>

    <style type="text/css">

        .style1

        {

            color: #FF3300;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div >

        <fieldset style="width: 40%; background-color: #E2ECF5;">

            <legend>Feedback</legend>

            <table cellpadding="2" cellspacing="5" >

               

                <tr>

                    <td width="80px">

                    </td>

                    <td>

                    </td>

                </tr>

                <tr>

                    <td width="80px">

                        Name<span style="color: #CC3300"> *</span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

                            ErrorMessage="Name Required" ForeColor="#FF3300"

                            ControlToValidate="txtName"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td>

                        Subject <span class="style1">*</span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

                            ErrorMessage="Subject Required" ForeColor="#FF3300"

                            ControlToValidate="txtSubject"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td>

                        E-mail<span style="color: #CC3300"> *</span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtEmail" runat="server" Width="300px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

                            ErrorMessage="Email Required" ForeColor="#FF3300"

                            ControlToValidate="txtEmail"></asp:RequiredFieldValidator>

                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

                            ErrorMessage="Not a vailed email" ControlToValidate="txtEmail"

                            ForeColor="#FF3300"

                            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

                    </td>

                </tr>

                <tr>

                    <td>

                        Inquiry<span style="color: #CC3300"> </span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtInquiry" runat="server" Height="100px" TextMode="MultiLine" Width="400px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>

                        Attachment</td>

                    <td>

                        <asp:FileUpload ID="fldAttchment" runat="server" />

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Button ID="btnSubmit" runat="server" Text="Send" Width="100px" OnClick="btnSubmit_Click1" />

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

            </table>

        </fieldset>

    </div>

    </form>

</body>

</html>

 

CODE BEHIND

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Net.Mail;

using System.Net;

using System.Drawing;

using System.IO;

 

public partial class Feedback : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

 

 

    private void EmailFeedback()

    {

 

        //create the mail message

        MailMessage mail = new MailMessage();

 

        //set the addresses

        mail.From = new MailAddress(txtEmail.Text.Trim());

 

        mail.To.Add("anwar@csharpexample.com");

 

        //set the content

        mail.Subject = "Enquiry Form";

 

        //Body to be displayed

        mail.Body = "<h2>" + "Enquiry from " + " " + txtName.Text + "</h2>" + "<br><br>" +

                "Subject: " + txtSubject.Text + "<br>" +

                "Email : " + txtEmail.Text.Trim() + "<br>" +

                "InQuiry: " + txtInquiry.Text.Trim() + "<br>" +

                "Thank You";

 

        //Attachment

 

        if (fldAttchment.HasFile)

        {

            string fileName = Path.GetFileName(fldAttchment.PostedFile.FileName);

            Attachment myAttachment =

                           new Attachment(fldAttchment.FileContent, fileName);

            mail.Attachments.Add(myAttachment);

        }

 

        mail.IsBodyHtml = true;

 

        // smtp settings

        SmtpClient smtp = new SmtpClient("mail.csharpexample.com");

        NetworkCredential SMTPUserInfo = new NetworkCredential("anwar@csharpexample.com", "Abcd123adsf@");

        smtp.Credentials = SMTPUserInfo;

        smtp.Send(mail);

 

 

 

 

    }

 

    private void Clear()

    {

        txtName.Text = string.Empty;

        txtSubject.Text = string.Empty;    

        txtEmail.Text = string.Empty;  

        txtInquiry.Text = string.Empty;

 

    }

     

    protected void btnSubmit_Click1(object sender, EventArgs e)

    {

      

            EmailFeedback();

            lblMsg.ForeColor = Color.Green;

            Clear();

            lblMsg.Text = "Thank You for Your Inquiry";

 

       

    }

}

 

Out Put

feedback-with-attchment-in-ASP-Dot-Net-C-Sharp



Realted Article Headline

Send mail with multiple selections in ASP.Net C #.
Feedback form with Yes or No Option in ASP.Net C#
Send mail with attachment file in ASP.Net C #
Send mail using Gmail account in ASP.Net C#.
Feedback form example in ASP.Net C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection