Aspx内容页面,无法执行发送邮件代码
Aspx content page, unable to execute send email code
我正在使用一个使用站点主文件的 asp
内容页面。我对带有标签的 runat=server
和让 vb.net
执行感到特别困惑。我试过了,但它不起作用:
<div class="card-body">
<asp:Label ID="Label1" runat="server" />
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" aria-describedby="emailHelp" placeholder="Enter name" required>
</div>
<div class="form-group" runat="server">
<asp:Label for="email" runat="server" Text="Email address"></asp:Label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" required>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" rows="6" required></textarea>
</div>
<div class="mx-auto">
<asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit"></asp:Button>
</div>
我尝试将 html
代码放在一个表单中,但错误提示不能有嵌套表单,所以很困惑如何将 html
发送到 vb.net
代码
VB.NET
代码:
Protected Sub Btn_SendMessage_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim Email As String = FindControl("email").ToString
Dim Name As String = FindControl("name").ToString
Dim Message As String = FindControl("message").ToString
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = Name
Mail.From = New MailAddress(Email)
SMTP.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "xxxxxxxx") '<-- Password Here
Mail.To.Add("xxxxxxx@gmail.com")
Mail.Body = Message
SMTP.EnableSsl = True
SMTP.Port = "587"
Try
SMTP.Send(Mail)
Label1.Text = "Message sent"
Catch ex As Exception
Label1.Text = ex.ToString
End Try
End Sub
页面刷新后没有任何反应,我什至不认为 VB.NET
执行了。
这是一个基于您的代码的工作示例:
ASPX
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="card-body">
<asp:Label ID="Label1" runat="server" />
<div class="form-group">
<label for="name">Name</label>
<asp:Label ID="Label4" runat="server" AssociatedControlID="TextBoxName" Text="Name"></asp:Label>
<asp:TextBox ID="TextBoxName" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ErrorMessage="*" ControlToValidate="TextBoxName"></asp:RequiredFieldValidator>
</div>
<div class="form-group" runat="server">
<asp:Label ID="Label3" runat="server" AssociatedControlID="TextBoxEmail" Text="Email address"></asp:Label>
<asp:TextBox ID="TextBoxEmail" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="*" ControlToValidate="TextBoxEmail"></asp:RequiredFieldValidator>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<asp:Label ID="Label2" runat="server" AssociatedControlID="TextBoxMessage" Text="Message"></asp:Label>
<asp:TextBox ID="TextBoxMessage" runat="server" TextMode="MultiLine" CssClass="form-control" Rows="6"></asp:TextBox>
</div>
<div class="mx-auto">
<asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit" OnClick="Btn_SendMessage_Click"></asp:Button>
</div>
</div>
</form>
</body>
</html>
代码隐藏
Protected Sub Btn_SendMessage_Click(sender As Object, e As EventArgs) Handles Btn_SendMessage.Click
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = TextBoxName.Text
Mail.From = New MailAddress(TextBoxEmail.Text)
SMTP.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "xxxxxxxx") '<-- Password Here
Mail.To.Add("xxxxxxx@gmail.com")
Mail.Body = TextBoxMessage.Text
SMTP.EnableSsl = True
SMTP.Port = "587"
Try
SMTP.Send(Mail)
Label1.Text = "Message sent"
Catch ex As Exception
Label1.Text = ex.ToString
End Try
End Sub
具有 runat="server"
属性的标签是 ASP.NET 服务器端控件,在发送到浏览器的结果网页中呈现为 HTML 对应项。在页面的服务器端处理期间,您可以直接使用它们的 ID 在您的代码中访问它们。
请注意,我对必填字段使用了服务器端验证控件。您可能需要在 Web.config
文件中添加以下设置,以防您收到有关非侵入式验证的运行时错误:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
希望对您有所帮助。我正在使用 Visual Studio 2017,目标框架是 .NET 4.6.1。
我正在使用一个使用站点主文件的 asp
内容页面。我对带有标签的 runat=server
和让 vb.net
执行感到特别困惑。我试过了,但它不起作用:
<div class="card-body">
<asp:Label ID="Label1" runat="server" />
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" aria-describedby="emailHelp" placeholder="Enter name" required>
</div>
<div class="form-group" runat="server">
<asp:Label for="email" runat="server" Text="Email address"></asp:Label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" required>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" rows="6" required></textarea>
</div>
<div class="mx-auto">
<asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit"></asp:Button>
</div>
我尝试将 html
代码放在一个表单中,但错误提示不能有嵌套表单,所以很困惑如何将 html
发送到 vb.net
代码
VB.NET
代码:
Protected Sub Btn_SendMessage_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim Email As String = FindControl("email").ToString
Dim Name As String = FindControl("name").ToString
Dim Message As String = FindControl("message").ToString
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = Name
Mail.From = New MailAddress(Email)
SMTP.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "xxxxxxxx") '<-- Password Here
Mail.To.Add("xxxxxxx@gmail.com")
Mail.Body = Message
SMTP.EnableSsl = True
SMTP.Port = "587"
Try
SMTP.Send(Mail)
Label1.Text = "Message sent"
Catch ex As Exception
Label1.Text = ex.ToString
End Try
End Sub
页面刷新后没有任何反应,我什至不认为 VB.NET
执行了。
这是一个基于您的代码的工作示例:
ASPX
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="card-body">
<asp:Label ID="Label1" runat="server" />
<div class="form-group">
<label for="name">Name</label>
<asp:Label ID="Label4" runat="server" AssociatedControlID="TextBoxName" Text="Name"></asp:Label>
<asp:TextBox ID="TextBoxName" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ErrorMessage="*" ControlToValidate="TextBoxName"></asp:RequiredFieldValidator>
</div>
<div class="form-group" runat="server">
<asp:Label ID="Label3" runat="server" AssociatedControlID="TextBoxEmail" Text="Email address"></asp:Label>
<asp:TextBox ID="TextBoxEmail" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="*" ControlToValidate="TextBoxEmail"></asp:RequiredFieldValidator>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<asp:Label ID="Label2" runat="server" AssociatedControlID="TextBoxMessage" Text="Message"></asp:Label>
<asp:TextBox ID="TextBoxMessage" runat="server" TextMode="MultiLine" CssClass="form-control" Rows="6"></asp:TextBox>
</div>
<div class="mx-auto">
<asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit" OnClick="Btn_SendMessage_Click"></asp:Button>
</div>
</div>
</form>
</body>
</html>
代码隐藏
Protected Sub Btn_SendMessage_Click(sender As Object, e As EventArgs) Handles Btn_SendMessage.Click
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = TextBoxName.Text
Mail.From = New MailAddress(TextBoxEmail.Text)
SMTP.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "xxxxxxxx") '<-- Password Here
Mail.To.Add("xxxxxxx@gmail.com")
Mail.Body = TextBoxMessage.Text
SMTP.EnableSsl = True
SMTP.Port = "587"
Try
SMTP.Send(Mail)
Label1.Text = "Message sent"
Catch ex As Exception
Label1.Text = ex.ToString
End Try
End Sub
具有 runat="server"
属性的标签是 ASP.NET 服务器端控件,在发送到浏览器的结果网页中呈现为 HTML 对应项。在页面的服务器端处理期间,您可以直接使用它们的 ID 在您的代码中访问它们。
请注意,我对必填字段使用了服务器端验证控件。您可能需要在 Web.config
文件中添加以下设置,以防您收到有关非侵入式验证的运行时错误:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
希望对您有所帮助。我正在使用 Visual Studio 2017,目标框架是 .NET 4.6.1。