Xây dựng form liên hệ cho website trong ASP.Net

Xây dựng form liên hệ cho website trong ASP.Net

Trong các website thường có phần liên hệ để người dùng liên hệ với quản lý website đó. Trong bài viết này, WEBXAULA làm form liên hệ như WEBXAULA đang dùng 

Xây dựng form liên hệ cho website trong ASP.Net

 Đầu tiên, trong trang aspx bạn chuẩn bị code html như sau:

<div class="content">


    <h2>liên hệ</h2>

   

    <div style="text-align:left; padding:10px;">

        <strong>Địa chỉ</strong>: G7, Mỹ Hoà 2, Xuân Thới Đông, Hóc Môn, TPHCM

 <br />

<strong>Điện thoại</strong>: (08)6250.1751 - 0909.017.015

 <br />

<strong>Email</strong>: <a href="mailto:[email protected]">

[email protected]</a> - <ahref="mailto:[email protected]">[email protected]</a>

<br />

<br />

- Tài khoản: 0101922953 tại Ngân hàng Đông Á. <br />

Chủ tài khoản: Nguyễn Toàn Khoa.<br />

<br />

- Tài khoản: 126700699 tại Ngân hàng Á Châu.<br />

Phòng giao dịch: Nguyễn Ảnh Thủ.<br />

Chủ tài khoản: Nguyễn Toàn Khoa.<br />
    </div>   

    <div style="padding:5px 0px 5px 0px; font-weight:bold; margin-bottom:10px;">

        Cảm ơn quý khách đã ghé thăm website của chúng tôi.

Nếu có nhu cầu cần trao đổi vui lòng điền đầy đủ thông tin vào biểu mẫu sau.

Chúng tôi sẽ trả lời quý khách trong thời gian sớm nhất.

        <br /><br />

        (*) Quý khách vui lòng gõ tiếng Việt có dấu.

    </div>

    <div class="dangky_left">

        * Họ tên

    </div>   

    <div class="dangky_right">

        <asp:TextBox ID="txtHoTen" CssClass="dangky_input" runat="server"></asp:TextBox>

    </div>   

    <div class="dive"></div>

   

    <div class="dangky_left">

        * Email

    </div>   

    <div class="dangky_right">

        <asp:TextBox ID="txtEmail" CssClass="dangky_input" runat="server"></asp:TextBox>

    </div>   

    <div class="dive"></div>   

    <div class="dangky_left">

        * Địa chỉ

    </div>   

    <div class="dangky_right">

        <asp:TextBox ID="txtDiaChi" CssClass="dangky_input" runat="server"></asp:TextBox>

    </div>   

    <div class="dive"></div>

   

    <div class="dangky_left">

        * Điện thoại

    </div>   

    <div class="dangky_right">

        <asp:TextBox ID="txtDienThoai" CssClass="dangky_input" runat="server"></asp:TextBox>

    </div>

   

    <div class="dive"></div>  

    <div class="dangky_left">

        * Nội dung liên hệ

    </div>

   

    <div class="dangky_right" style="background:url(../hinh/textbox_gop_y.png) no-repeat; height:119px;">

        <asp:TextBox ID="txtThongTinLienHe" TextMode="MultiLine" Width="200px"

 Height="100px" BorderWidth="0px" runat="server"></asp:TextBox>

    </div>   

    <div class="dive"></div>   

    <div style="height:auto; color:Red; font-style:italic;">

        <asp:Literal ID="lblThongBao" runat="server"></asp:Literal>

    </div>

        <div class="dangky_left">       

    </div>
   

    <div class="dangky_right" style="background:none; height:auto;">

        <asp:Button ID="cmdGui" runat="server" Text="Gửi" Width="100px" Height="30px"

            onclick="cmdGui_Click" />

    </div>   

    <div class="dive"></div>

Trong code C#, bạn viết như sau. Ở đây tôi dùng smtp.gmail để gửi mail nên bạn cần một tài khoản gmail để thực hiện. Trong code bên dưới, bạn thay đổi "your email" và "your password" thành email và mật khẩu email của bạn

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.Web.Mail;


public partial class Usercontrol_lienhe : System.Web.UI.UserControl

{

    private void GuiMail(string pNoiDung, string pNguoiNhan)

    {

        MailMessage pMail = new MailMessage();

        pMail.From = "[email protected]";

        pMail.Subject = "Thông tin liên hệ từ website www.webxaula.com";

        pMail.Body = pNoiDung + "<br/><br/>";

        pMail.To = pNguoiNhan;


        pMail.BodyEncoding = System.Text.Encoding.UTF8;

        pMail.BodyFormat = MailFormat.Html;

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtsperver"] = "smtp.gmail.com";

        //dùng port 465 nếu lỗi thì đổi sang port 587;

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465;

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "your email";

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "your password";

        pMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = true;


        SmtpMail.SmtpServer = "smtp.gmail.com";

        SmtpMail.Send(pMail);

    }   

  protected void cmdGui_Click(object sender, EventArgs e)

    {

        string pDiaChi = txtDiaChi.Text.Trim().Replace("", "").Replace("", "");

        string pDienThoai = txtDienThoai.Text.Trim().Replace("", "").Replace("", "");

        string pEmail = txtEmail.Text.Trim().Replace("", "").Replace("", "");

        string pHoten = txtHoTen.Text.Trim().Replace("", "").Replace("", "");

        string pNoiDung = txtThongTinLienHe.Text.Trim().Replace("", "").Replace("", "");


        string pNoiDungGui = "<b>Họ tên: </b>" + pHoten + "<br/><br/>" +

            "<b>Email: </b>" + pEmail + "<br/><br/>" +

            "<b>Điện thoại: </b>" + pDienThoai + "<br/><br/>" +

            "<b>Địa chỉ: </b>" + pDiaChi + "<br/><br/>" +

            "<b>Nội dung liên hệ: </b>" + pNoiDung;


       

        GuiMail(pNoiDungGui, "[email protected]");


        lblThongBao.Text = "Bạn đã gửi thông tin liên hệ thành công.";

        txtThongTinLienHe.Text = "";

        txtHoTen.Text = "";

        txtEmail.Text = "";

        txtDienThoai.Text = "";

        txtDiaChi.Text = "";

    }

}
Bạn thấy bài viết này như thế nào?: 
Average: 10 (2 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
5 Facebook plugin tốt nhất nên có cho WordPress

Giới thiệu 5 Facebook plugin tốt nhất nên có cho WordPress

Không cần phải nói nhiều về Facebook – mạng xã hội lớn nhất thế giới hiện nay – với sức ảnh hưởng sâu rộng của nó. Theo ý kiến chủ quan của Việt Coding

Giới thiệu Drupal Views Templates và Theming

Giới thiệu sự kết hợp giữa Drupal Views Templates và Theming

One of our students is learning Drupal and trying to master Views. They wanted to know how to style different areas of each Views. We wrote this tutorial as an introduction for them to templates and theming for Views.

TRUST RANK - Đánh giá mức độ uy tín của website

TRUST RANK - Đánh giá mức độ uy tín của website

Làm thế nào khi thương hiệu, website của chúng ta chưa có mức độ nổi tiếng, tên tuổi nhưng vẫn giúp khách hàng khi thăm website tin tưởng, yên tâm khi mua hàng và dịch vụ trên website?

Công ty diệt chuột T&C

 

Diet con trung