无法获取HTML文本框值

时间:2015-10-23 02:54:21

标签: c# html asp.net smtp html-email

我有一个像这样的html表单:

 <form id="main-contact-form" name="contact-form" method="post" action="test.aspx">
                            <div class="form-group">
                                <input id="txtname" type="text" class="form-control" placeholder="Name" required>
                            </div>
                            <div class="form-group">
                                <input id="txtemail" type="email" class="form-control" placeholder="Email" required>
                            </div>
                            <div class="form-group">
                                <input id="txtsubject" type="text" class="form-control" placeholder="Subject" required>
                            </div>
                            <div class="form-group">
                                <textarea id="txtmessage" class="form-control" rows="8" placeholder="Message" required></textarea>
                            </div>
                            <button type="submit" class="btn btn-default">Send Message</button>
                        </form>

现在,我想要做的是在用户按下发送按钮后将用户输入到文本框中。现在我将一个aspx集成到此标记中,以便它可以发送到电子邮件。这是:

MailMessage mail = new MailMessage ();
        mail.From = new System.Net.Mail.MailAddress ("username@domain.com");
        // The important part -- configuring the SMTP client
        SmtpClient smtp = new SmtpClient ();
        smtp.Port = 587;   // [1] You can try with 465 also, I always used 587 and got success
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
        smtp.UseDefaultCredentials = false; // [3] Changed this
        smtp.Credentials = new NetworkCredential ("username@domain.com", "password");  // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
        smtp.Host = "smtp.gmail.com";

        //recipient address 
        mail.To.Add (new MailAddress ("user1@gmail.com"));
        mail.To.Add (new MailAddress ("user2@gmail.com"));

        //Formatted mail body 
        mail.IsBodyHtml = true;
        mail.Body = Request.Form ["txtmessage"];
        mail.Subject = Request.Form ["txtsubject"];
        string st = "This is a Test  Message" ;

        mail.Body = st;
        smtp.Send (mail);

对于记录,一旦按下html中的按钮,它就已经发送了电子邮件,但它只在我的c#代码中发送了硬编码字符串,并且它没有获得其他文本框的值。

你可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

尝试按名称获取

<input id="txtname" name="txtname" type="text" class="form-control" placeholder="Name" required>

答案 1 :(得分:0)

使用runat="server"为您需要从后面的代码访问的每个控件

<input id="txtemail"  runat="server" type="email" class="form-control" placeholder="Email" required>

然后你可以得到如下的值

 mail.From = new System.Net.Mail.MailAddress(txtemail.Value);