javascript - 如何在 asp.net 和 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) { }
}
在按钮提交上,您可以执行打开确认对话框的操作。
推荐阅读
- python - 将格式从 Adobe Acrobat pdf 更改为 Jupyter Notebook ipynb
- r - 合并数据框的第一列
- java - 如何使用 Jackson 将原始 JSON 反序列化为 Java 对象
- c# - 将两种不同的变量类型添加到字典中以获取 GET 请求的多个自定义标头
- julia - 读取 .osm.pbf 或将 .osm.pbf 转换为 .osm
- python - 使用 WSGI-Mod 托管在 Ubuntu-Apache 上的 Python Flask 应用程序上的 FileNotFoundError
- activemq-artemis - Artemis 联邦优先级调整是如何工作的?
- python - 双下划线文本 Tkinter
- java - What is the elegant way to parse a map into an object?
- html - How to remove an outline from a selected Card Bootstrap 5