【发布时间】:2013-12-09 09:09:44
【问题描述】:
我想添加我的代码以通过 C# 发送电子邮件,但我的按钮似乎不起作用。
HTML 代码:
<div style="text-align:center">
<p style="background-color: Yellow">if you have a video that you think should be here send it to us</p>Name:
<input type="text" id="Name" />Link:
<input type="text" id="Link" />
<br />Why you think this video should be here:
<br />
<textarea id="Why" rows="5" cols="50"></textarea>
<br />
<input type="submit" id="submit" name="submit" value="Send" style="height: 25px; width: 200px" />
</div>
aspx.cs 代码:
public partial class Videos : System.Web.UI.Page
{
public string Answer;
protected void Page_Load(object sender, EventArgs e)
{
Answer = "Email Sent2";
if (Request.Form["submit"] != null)
{
Answer = "Email Sent3";
string Name = Request.Form["Name"];
string Link = Request.Form["Link"];
string Why = Request.Form["Why"];
string body = "<div dir='ltr'>";
body += ("<h4>You got a new video idea from </h4>");
body += (Name);
body += ("<br />");
body += ("Link: " + Link);
body += ("<br />");
body += ("Reason: " + Why);
body += ("</div>");
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("MY EMAIL", "MY PASS"),
EnableSsl = true
};
System.Net.Mail.MailMessage mail1 = new System.Net.Mail.MailMessage();
mail1.Body = body;
mail1.From = new System.Net.Mail.MailAddress("MY EMAIL");
mail1.IsBodyHtml = true;
mail1.Subject = "Vidoe Idea By " + Name;
mail1.To.Add("MY EMAI");
client.Send(mail1);
Answer = "Email Sent";
}
}
我不知道为什么,但是当我按下按钮时,什么也没发生。 这里有什么问题?
【问题讨论】:
-
您的键盘有问题吗?尝试使用 asp:button 控件而不是 HTML 输入
-
首先,调试您的代码,因为“我按下按钮没有发生任何事情”是用户问题,而不是程序员问题。这里是关于编程问题。其次,我不知道您是否可以使用 webforms 阅读简单的 HTML,为什么不使用 并在 C# 中将其作为 txtName.Text 阅读而不是请求。形式?所以,调试你的代码,搜索它不工作的地方,然后再告诉我们。
-
我调试了我的代码。直到 if(Request.Form..
-
或者,为了区分第一次提交或加载(我怀疑你正在尝试这样做),只需使用
IsPostBack。 -
您似乎错过了一些网络表单的要点。您是来自 PHP 还是经典 ASP 背景?也许 ASP.net MVC 会更适合你。另请查看
StringBuilder以替换您正在执行的sting concatination。