简体   繁体   English

ASP.NET联系表单-输出到电子邮件和访问数据库

[英]ASP.NET Contact Form - output to email and access database

I am new to ASP.NET, and I am trying to create a contact form which sends an email at submission as well storing the data in a database. 我是ASP.NET的新手,我正在尝试创建一个联系表单,该表单在提交时发送电子邮件,以及将数据存储在数据库中。

I have read numerous tutorials, and I am pretty comfortable with a basic contact form to email setup - but I am having trouble with the database part of it. 我已经阅读了许多教程,并且对使用电子邮件设置基本联系表感到很满意-但是我在数据库部分遇到了麻烦。

Please let me know if you have ever done this, or if you could provide any information that would assist me with this task. 如果您曾经做过此事,或者可以提供任何有助于我完成此任务的信息,请告诉我。

I am running ASP.NET 2.0 我正在运行ASP.NET 2.0

Thanks in advance. 提前致谢。

I'm very new to both C# and ASP.NET (first year IT student). 我对C#和ASP.NET(IT专业一年级学生)都是新手。 Up until a month ago, I'd never even viewed C# code, much less programmed anything in C#. 直到一个月前,我什至从未看过C#代码,更不用说用C#编写任何程序了。 I, too, have been combing the internet for a solution to this problem. 我也一直在梳理互联网来解决这个问题。 After a week of tinkering with some code, I finally figured it out. 经过一周的修改,我终于弄明白了。 The below code will allow you to create an ASP.NET "Contact Us" email form, and will send the information in the form to a SQL Server database. 下面的代码将允许您创建一个ASP.NET“联系我们”电子邮件表单,并将该表单中的信息发送到SQL Server数据库。 I hope this helps someone to avoid the same aggravation I went through! 我希望这可以帮助某人避免我经历的同样的烦恼! (If anyone knows a way to program this more efficiently, I'd love to hear your thoughts.) (如果有人知道更有效地编程的方法,我很想听听您的想法。)

HERE IS THE CODE FOR THE ASPX.CS FILE ATTACHED TO THE FORM: 这是表单上附加的ASPX.CS文件的代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

//Code for the Reset event (will reset form data):

protected void Reset(object s, EventArgs e)
{
    fname.Text = "";
    lname.Text = "";
    email.Text = "";
    phone.Text = "";
    comments.Text = "";
}

//Code for the SendMail Event; will send email and write info in email into database:

protected void SendMail(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(email.Text);
    mail.To.Add("EMAIL ADDRESS WHERE YOU'D LIKE THE MESSAGE SENT");
    mail.Subject = "Contact Us";
    mail.IsBodyHtml = true;
    mail.Body += "First Name: " + fname.Text + "<br />";
    mail.Body += "Last Name: " + lname.Text + "<br />";
    mail.Body += "Comments: " + comments.Text + "<br />";
    mail.Body += "Phone Number: " + phone.Text + "<br />";

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "NAME OF SMTP RELAY SERVER";
    smtp.Send(mail);
}

protected void insertInfo(object sender, EventArgs e)
{
    SqlConnection myConnection = new SqlConnection             (ConfigurationManager.ConnectionStrings["WEB.CONFIG CONNECTION STRING NAME"].ToString());

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT INTO TABLE NAME (fname, lname, email, phone, comment)    
VALUES (@fname, @lname, @email, @phone, @comments)";
    cmd.Connection = myConnection;

    cmd.Parameters.Add("@fname", fname.Text);
    cmd.Parameters.Add("@lname", lname.Text);
    cmd.Parameters.Add("@email", email.Text);
    cmd.Parameters.Add("@phone", phone.Text);
    cmd.Parameters.Add("@comments", comments.Text);


    myConnection.Open();
    cmd.ExecuteNonQuery();
    myConnection.Close();
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM