ASP.NET联系表 - 输出到电子邮件和数据库的访问 [英] ASP.NET Contact Form - output to email and access database

查看:114
本文介绍了ASP.NET联系表 - 输出到电子邮件和数据库的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的ASP.NET和我想创建一个在提交以及存储在数据库中的数据发送一封电子邮件联系表单。

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.

我看过无数的教程和我pretty舒适的基本联系表格电子邮件设置 - 但我有它的数据库的一部分麻烦

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.

我正在运行ASP.NET 2.0

I am running ASP.NET 2.0

先谢谢了。

推荐答案

我很新的C#和ASP.NET(第一年的学生)。直到一个月前,我从来没有看到甚至C#code,更不用说编程在C#什么。我也一样,已梳理上网解决这一问题。一些code摆弄的一个星期后,我终于想通了。下面code将允许您创建一个ASP.NET联系我们电子邮件的形式,并将在表单中的信息发送到SQL Server数据库。我希望这可以帮助别人,以避免我经历了同样的恶化! (如果有人知道的方式来更有效地设定此,我很想听听你的看法。)

I'm very new to both C# and ASP.NET (first year IT student). Up until a month ago, I'd never even viewed C# code, much less programmed anything in 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. 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.)

这里是code为连接到形成ASPX.CS文件:

HERE IS THE CODE FOR THE ASPX.CS FILE ATTACHED TO THE FORM:

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();
}

}

这篇关于ASP.NET联系表 - 输出到电子邮件和数据库的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆