connectseekers.com

Wednesday, 7 September 2016

Contact Us Form With Browse File Attachment in Asp.net C#

9/07/2016 - By Pranav Singh 0


So for this article first we will create a new asp.net application and add the below code into the page.

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm13.aspx.cs" Inherits="WebApplication7.WebForm13" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Contact Us Form Wtih Browse File Attachment in Asp.net C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table width="100%" border="1">
                <tr>
                    <td align="right" colspan="2" style="text-align: center">
<strong>Contact Us Form</strong></td>
                </tr>
                <tr>
                    <td align="right">&nbsp;</td>
                    <td align="left">
                        <asp:Label ID="lblmessage" runat="server" Style="color: #FF3300">
</asp:Label>
                    </td>
                </tr>
                <tr>
                    <td align="right">Name :</td>
                    <td align="left">
                        <asp:TextBox ID="txtName" runat="server" 
Width="250px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">Your Email id :</td>
                    <td align="left">
                        <asp:TextBox ID="txtEmailId" runat="server" Width="250px">
</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">Phone No :</td>
                    <td align="left">
                        <asp:TextBox ID="txtPhoneno" runat="server" Width="250px">
</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">Subject :</td>
                    <td align="left">
                        <asp:TextBox ID="txtsubject" runat="server" Width="250px">
</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">Message Body :</td>
                    <td align="left">
                        <asp:TextBox ID="txtmessage" runat="server" Height="103px" 
TextMode="MultiLine"
                            Width="250px"></asp:TextBox>
                    </td>
                </tr>
                 <tr>
                    <td align="right">Atrtachment :</td>
                    <td align="left">
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <asp:Button ID="Button1" runat="server"
                            OnClick="Button1_Click" Text="Send Message" /></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

In above code I have created a contact us form with curtain fields like name, email id etc. Now generate the click event and add the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class WebForm13 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Send to owner
            SendEnquiry(txtName.Text, txtEmailId.Text, txtPhoneno.Text, 
txtsubject.Text, txtmessage.Text);
        }
        public void SendEnquiry(string name, string emailid, string phoneno, 
string subject, string message)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(txtEmailId.Text);
                mail.From = new MailAddress("youremail@gmail.com");
                mail.Subject = subject;
                string userMessage = "";
                userMessage = "<br/>Name :" + name;
                userMessage = userMessage + "<br/>Email Id: " + emailid;
                userMessage = userMessage + "<br/>Phone No: " + phoneno;
                userMessage = userMessage + "<br/>Message: " + message;
                string Body = "Hi, <br/><br/> A new enquiry by user. Detail is as follows:<br/></br> " + userMessage + "<br/><br/>Thanks";
                mail.Body = Body;
                mail.IsBodyHtml = true;

                //Send attachment
                if (FileUpload1.HasFile)
                {
                    System.Net.Mail.Attachment attachment;
                    string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                    /*Saving the file in server folder*/
                    FileUpload1.SaveAs(Server.MapPath("~/UploadFile/" + filename));
                    string filepathtoattach = "UploadFile/" + filename;
                    attachment = newSystem.Net.Mail.Attachment(Server.MapPath(filepathtoattach));
                    mail.Attachments.Add(attachment);
                }
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com"//SMTP Server Address of gmail
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("youremail@gmail.com""password");
                // Smtp Email ID and Password For authentication
                smtp.EnableSsl = true;
                smtp.Send(mail);
                lblmessage.Text = "Thank you for contacting us.";
            }
            catch
            {
                lblmessage.Text = "Error............";
        }
    }
}

In above code I have prepared a function for sending the mail. This function will send the enquiry to the admin of the website. Please check the highlighted part of the code. This piece of code will send the attachment.  In  this code I have checked weather any attachment is there is not. If yes then it will send the attachment otherwise only enquiry will go.

So in above code I have used the smtp of gmail for sending the email.  Now one this after execution if you are getting error message as shown below:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Then follow the below link to resolve the error.


Now check email for final output.


DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top