頁代碼如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//下面兩個是新引入的命名空間,1是郵件,2是IO
using System.Web.Mail;
using System.IO;
namespace mation
{
/**//// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.TextBox Textbox4;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
FileStream fs=new FileStream(Server.MapPath("db.XML"),FileMode.Open,FileAccess.Read);//創建文本流
StreamReader sr=new StreamReader(fs);//創建讀取器
DataSet ds=new DataSet();//聲明一個本地表
ds.ReadXML(sr);//將內容裝載到本地表中
this.DropDownList1.DataSource=ds.Tables[0];//設置數據源
this.DropDownList1.DataTextFIEld="bumen";//設置顯示的文本字段
this.DropDownList1.DataValueFIEld="addr";//對應的值
this.DropDownList1.DataBind();//綁定
}
//這裡是窗體設計器生成的代碼
private void Button1_Click(object sender, System.EventArgs e)
{
//創建消息文本
string the="新的消息<br>名字:"+TextBox1.Text.ToString()+"<br>郵件地址:"+TextBox2.Text.ToString()+"<br>意見是:"+TextBox3.Text.ToString();//構造郵件正文內容
// Response.Write(the);
MailMessage mm=new MailMessage();//實例化一個Email對象
mm.From=TextBox2.Text.ToString();//返回發信人的地址
mm.To=this.DropDownList1.SelectedItem.Value;//收信人的地址,也就是郵件的目標地址
mm.Subject=this.Textbox4.Text.ToString();//郵件標題
mm.Body=the;//郵件正文
SmtpMail.Send(mm);//發送
this.Table1.Visible=false;//隱藏表格
this.Label1.Text="您的郵件已經送出!";//顯示一個提示信息
}
}
}
下面是XML文件代碼
1<?XML version="1.0" encoding="utf-8" ?>
2<NewDataSet>
3 <email>
4 <id>1</id>
5 <bumen>網站事業部</bumen>
6 <addr>thcjp@126.com</addr>
7 </email>
8 <email>
9 <id>2</id>
10 <bumen>銷售部</bumen>
11 <addr>xx@xxx.xx</addr>
12 </email>
13 <email>
14 <id>3</id>
15 <bumen>質量監管部</bumen>
16 <addr>zz@xxx.com</addr>
17 </email>
18 <email>
19 <id>4</id>
20 <bumen>財務部</bumen>
21 <addr>cw@xxx.com</addr>
22 </email>
23 <email>
24 <id>5</id>
25 <bumen>天轟穿</bumen>
26 <addr>110535808@QQ.com</addr>
27 </email>
28</NewDataSet>