请帮忙......
我刚从互联网上获得以下代码,并且不知道如何解决问题。
问题是: - 即使表单为空或其中一个字段为空,我仍然会收到该电子邮件。
我想我需要在这里添加一些验证,但我不知道如何。我不是程序员,也不是asp的新手。
这是代码。
ASPX是:
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest = "false" CodeFile="contact.aspx.cs" Inherits="_Default" %>
<html>
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div class="inp_title"><asp:Label ID="Label1" runat="server" Text="Name*" /></div>
<div>
<asp:TextBox ID="txtName" runat="server" ValidationGroup = "contact" CssClass="inp_h" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your name." ControlToValidate = "txtName" Display="Dynamic" /></div>
<div class="inp_title"><asp:Label ID="Label2" runat="server" Text="Subject*" /></div>
<div>
<asp:TextBox ID="txtSubject" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate = "txtSubject" Display="Dynamic" /></div>
<div class="inp_title"><asp:Label ID="Label3" runat="server" Text="Email Address*" /></div>
<div>
<asp:TextBox ID="txtEmail" runat="server" CssClass="inp_h" Display="Dynamic" /><br />
<asp:RegularExpressionValidator id="valRegEx" runat="server" ControlToValidate="txtEmail" ValidationExpression=".*@.*\..*" ErrorMessage="Please enter a valid email address." Display="Dynamic" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your email address." ControlToValidate = "txtEmail" />
</div>
<div class="inp_title-2"><asp:Label ID="Label4" runat="server" Text="Message*" /></div>
<div>
<asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" CssClass="inp_h" Display="Dynamic" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please enter your message." ControlToValidate = "txtBody" />
</div>
<div class="inp_h"><asp:FileUpload ID="FileUpload1" runat="server" /></div>
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="form-buttons"/>
<asp:Label ID="lblMessage" runat="server" Text="" ForeColor = "Green" />
</form>
</body>
</html>
代码隐藏是:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage mm = new MailMessage("email1@domain.com", "email2@domain.com");
mm.Subject = txtSubject.Text;
mm.Body = "Name: " + txtName.Text + "<br /><br />Email: " + txtEmail.Text + "<br /><br />Message:<br />" + txtBody.Text;
if (FileUpload1.HasFile)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName) ;
mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.office365.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "email1@domain.com";
NetworkCred.Password = "emai1password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
lblMessage.Text = "Email Sent Sucessfully.";
}
}
答案 0 :(得分:1)
你的问题在这里:
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="form-buttons"/>
在您的按钮中使用此代码:CausesValidation="true"
。这将在发布事件之前验证您的必填字段。
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="form-buttons" CausesValidation="true"/>
答案 1 :(得分:1)
在这里,如果您没有使用验证字段尚未填写,则不会发送电子邮件 - 否则只需按照之前的答案将'causevalidation = true'添加到您的提交按钮
protected void btnSend_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtName.Text) && !string.IsNullOrWhiteSpace(txtEmail.Text) && !string.IsNullOrWhiteSpace(txtBody.Text) )
{
MailMessage mm = new MailMessage("email1@domain.com", "email2@domain.com");
mm.Subject = txtSubject.Text;
mm.Body = "Name: " + txtName.Text + "<br /><br />Email: " + txtEmail.Text + "<br /><br />Message:<br />" + txtBody.Text;
if (FileUpload1.HasFile)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName) ;
mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.office365.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "email1@domain.com";
NetworkCred.Password = "emai1password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
lblMessage.Text = "Email Sent Sucessfully.";
}
else
{
lblMessage.Text = "You need to fill in the fields.";
}
}
答案 2 :(得分:0)
您需要为文本框
设置必填字段验证程序