2013-11-24 54 views
0

我想添加我的代碼以通過C#發送電子郵件,但我的按鈕似乎不起作用。將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"; 
     } 
    } 

我不知道爲什麼,但是當我按下按鈕沒有任何happend。 這裏有什麼問題?

+1

你的鍵盤有問題嗎?嘗試使用asp:按鈕控件而不是HTML輸入 – geedubb

+0

首先,調試代碼是因爲「我按下按鈕什麼都沒有發生」是用戶問題,而不是程序員問題..這裏介紹編程問題。其次,我不知道你是否可以使用webforms閱讀簡單的HTML,爲什麼不使用並在C#中將其作爲txtName.Text讀取,而不是Request.Form ?所以,調試你的代碼,搜索它不起作用的地方並再次告訴我們。 –

+0

我打了我的代碼。它一直工作,直到if(Request.Form .. – KenigOri

回答

0

你應該做一些關於Webforms如何在ASP.net中工作的更多研究。

Webforms旨在與服務器控件一起使用,您可以在VisualStudio的工具欄中找到它們。請求對象仍然可以工作,並且是webforms的主幹。請注意,請求對象需要name屬性。在HTML對象方面,ID用於客戶端,name用於服務器端。這是一個簡單的過分簡化,但是一個有用的指導方針。

Webforms也很重視事件導向,您應該利用這一事實並使用按鈕的單擊事件。

我會使用類似以下內容:

HTML/ASPX

<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> 
    <!-- Use Label to create a label associated with the control 
    This is good practice --> 
    <asp:Label id="lblName" runat="server" AssociatedControlID="txtName" EnableViewState="false">Name:</asp:Label> 
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox> 
    <br /><!-- Not really best practice to use <br /> for formatting, use CSS instead --> 
    <asp:Label ID="lblLink" runat="server" EnableViewState="false" AssociatedControlID="txtLink">Link</asp:Label> 
    <asp:TextBox ID="txtLink" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Label ID="lblWhy" runat="server" EnableViewState="false" AssociatedControlID="txtWhy">Why you think this video should be here:</asp:Label> 
    <br /> 
    <asp:TextBox ID="txtWhy" TextMode="MultiLine" Rows="5" Columns="50" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Button ID="btnSubmit" runat="server" Text="Send" 
      style="height: 25px; width: 200px" onclick="btnSubmit_Click"/> 
<!-- A Message to display when email has been sent --> 
<asp:Panel ID="pnlSucess" runat="server" Visible="false">Your feed back is appreciated!</asp:Panel> 
</div> 

.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    //Note: Nothing Here... 
    //Use this event for setting the page up 
} 

//Use this event for handling the click 
protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    //MAKE SURE YOU USE ERROR HANDLING FOR THE REAL WORLD 

    string Answer = string.Empty; 
    Answer = "Email Sent3"; 
    StringBuilder builder = new StringBuilder(); 

    //NOTE: You should be sanitising client input 
    string Name = txtName.Text; 
    string Link = txtLink.Text; 
    string Why = txtWhy.Text; 

    //String builder is a better way to build a string 
    //or look at string.Format();    
    builder.Append("<div dir='ltr'>"); 
    builder.AppendLine("<h4>You got a new video idea from </h4>"); 
    builder.AppendLine(Name); 
    builder.AppendLine("<br />"); 
    builder.AppendLine("Link: "); 
    builder.Append(Link); 
    builder.AppendLine("<br />"); 
    builder.AppendLine("Reason: "); 
    builder.Append(Why); 
    builder.AppendLine("</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 = builder.ToString(); //Get the 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"; 

    //Display a message so the user knows it worked! 
    pnlSucess.Visible = true; 
} 

確保添加using System.Net;到using語句在代碼的頂部。這是StringBuilder所必需的。

0

如果您使用的是網頁表單,您最好使用網頁表單控件。元素:

<input type="text" id="Name" />Link: 
<input type="text" id="Link" /> 

沒有runat =「server」標籤和id,這是讀取服務器端代碼中輸入內容所必需的。

而你必須使用一個asp:按鈕,然後分配一個事件來運行發送電子郵件代碼。如果你使用這個事件頁面加載出來,如果檢查是否是回發,可以得到這個代碼發送的大量電子郵件。

我的建議是創建一個新的Web窗體,爲您的輸入添加asp:textbox,爲您的輸入添加asp:按鈕在按鈕中創建事件並在其中添加代碼。用text屬性閱讀內容並調試你的代碼。