繁体   English   中英

如何在ASP.NET和C#中创建联系表?

[英]How to create a contact form in asp.net and c#?

如何在asp.net和C#中单击按钮时创建联系表单以发送邮件? 下面是我所拥有的联系表单中的代码,当单击按钮(#Button1)时,我想将该表单提交给电子邮件:“ name@mail.com”,但不刷新页面,仅发送表单并进行切换div(#submit)?

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

                        <div class="input-boxes">

                            <div class="left-inputs">
                                <div class="input-place required first" style="margin-top: 0px !important;">
                                    <input name="name" id="name" type="text" placeholder=" " runat="server" required>
                                    <label for="name">Full Name *</label>
                                </div>
                                <div class="input-place required second" style="margin-bottom: 0px !important;">
                                    <input name="txtemail" id="txtemail" type="email" placeholder=" " runat="server" required>
                                    <label for="email">E-mail *</label>
                                </div>
                            </div>

                            <div class="right-inputs">
                                <textarea name="message" id="message" type="text" placeholder=" "  runat="server" required></textarea>

                                <label for="message">How can we help you? *</label>
                            </div>

                        </div>

                        <div class="first-part-end">
                            <div class="slider">
                                <div class="slider-circle"></div>
                            </div>
                            <p class="more-options">Show more options</p>
                        </div>

                         <div class="input-boxes additional">
                            <div class="left-inputs">
                                <div class="input-place">
                                    <input name="company" id="company" type="text" placeholder=" " runat="server">

                                    <label for="company">Company Name</label>
                                </div>

                                <div class="input-place" >
                                   <input name="phone" id="phone" type="tel" runat="server">
                                   <input type="hidden" name="country" id="country" value="" runat="server">

                                </div>
                                <script>
                                $("#phone").intlTelInput();
                                </script>
                            </div>

                            <div class="right-inputs">
                                <div class="input-place" style="height: 60px !important;">
                                    <p class="budget-input">Your budget:
                                        <span id="demo"></span></p>
                                    <input name="budget" type="range" min="0" max="5" value="0" step="0" class="budget-slider" id="budgetRange" list="steplist" runat="server">
                                    <datalist id="steplist">
                                        <option>0</option>
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                    </datalist>
                                </div>
                                <div class="input-place" style="display: initial !important;">
                                    <p class="checkbox-input">I'm interested in: </p>
                                    <div class="check_all">
                                        <label class="container">Web Design
                                            <input name="web-design" type="checkbox">
                                            <span class="checkmark"></span>
                                        </label>
                                        <label class="container">Web Development

                                            <asp:CheckBox ID="CheckBox1" runat="server" />
                                            <span class="checkmark"></span>
                                        </label>
                                        <label class="container">App Design

                                            <asp:CheckBox ID="CheckBox2" runat="server" />
                                            <span class="checkmark"></span>
                                        </label>
                                        <label class="container">App Prototyping

                                            <asp:CheckBox ID="CheckBox3" runat="server" />
                                            <span class="checkmark"></span>
                                        </label>
                                        <label class="container">E-Commerce

                                            <asp:CheckBox ID="CheckBox4" runat="server" />
                                            <span class="checkmark"></span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="send-button">
                          <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
                        </div>

    </form>

按下按钮时,以下是需要切换的div:

<div class="form-send" id="submit">
            <div class="dialog">
                <img src="Content/icons/success.svg">
                <h2>Thanks for reaching us!</h2>
                <p>We appreciate you contacting us. Our team will get back to you shortly.</p>
                <div class="close-confirmation">Close</div>
            </div>
        </div>

服务器端代码:

protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString(); 
    //Password of your gmail address
    const string fromPassword = "Password";
     // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}


protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //here on button click what will done 
        SendMail();
        DisplayMessage.Text = "Your Comments after sending the mail";
        DisplayMessage.Visible = true;
        YourSubject.Text = "";
        YourEmail.Text = "";
        YourName.Text = "";
        Comments.Text = "";
    }
    catch (Exception) { }
}

在“按钮提交”上,您可以执行打开确认对话框的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM