1
2 实现邮件群发功能
3
4 //邮件群发功能
5 界面主要有TextBox(TextBox_name),ListBox(ListBox_name),Button(Button_sent)等一些控件,
6 ListBox里面为所有的用户或是好友,TextBox里面为所选择的用户或好友(可以多选择,用逗号分隔开),Button为发送按钮
7 //添加引用
8 using System.Data.SqlClient;
9 using System.Web.Mail;
10 using System.IO;
11 //执行发送
12 private void Button_sent_Click(object sender, System.EventArgs e)
13 {
14 //取到用户名
15 string TextBox_name = this.Text_to.Text;
16 //显示所有接收人,去逗号
17 string [] split = TextBox_name.Split(new Char [] {','});
18 SqlConnection myConn = new SqlConnection(conn);
19 DataSet mySet = new DataSet();
20 ////变量个数
21 //for (int i=0;i<split.LongLength;i++)
22 //一个一个的取到里面的单个的值
23 foreach (string s in split)
24 {
25 //添加数据表(Info)
26 if (s.Trim() != "")
27 {
28 try//异常处理
29 {
30 //SQL语句
31 string mySql = "INSERT INTO Info(Info_To)VALUES('"+s+"')";
32 SqlCommand myCommand = new SqlCommand(mySql,myConn);
33 myConn.Open();//打开数据库
34 myCommand.ExecuteNonQuery();//执行语句
35 }
36 catch(Exception ee)//错误消息
37 {
38 }
39 finally//不管是否发生错误都要执行
40 {
41 myConn.Close();//关闭数据库
42 }
43 }
44 }
45 }
46
47 //把从下拉列表中选择的用户,添加到显示的文本框中
48 private void ListBox_name_SelectedIndexChanged(object sender, System.EventArgs e)
49 {
50 if(this.TextBox_name.Text=="")
51 {
52 this.TextBox_name.Text=this.ListBox_name.SelectedItem.Text;
53 }
54 else
55 {
56 this.TextBox_name.Text+=","+this.ListBox_name.SelectedItem.Text;
57 }
58 }
59
2 实现邮件群发功能
3
4 //邮件群发功能
5 界面主要有TextBox(TextBox_name),ListBox(ListBox_name),Button(Button_sent)等一些控件,
6 ListBox里面为所有的用户或是好友,TextBox里面为所选择的用户或好友(可以多选择,用逗号分隔开),Button为发送按钮
7 //添加引用
8 using System.Data.SqlClient;
9 using System.Web.Mail;
10 using System.IO;
11 //执行发送
12 private void Button_sent_Click(object sender, System.EventArgs e)
13 {
14 //取到用户名
15 string TextBox_name = this.Text_to.Text;
16 //显示所有接收人,去逗号
17 string [] split = TextBox_name.Split(new Char [] {','});
18 SqlConnection myConn = new SqlConnection(conn);
19 DataSet mySet = new DataSet();
20 ////变量个数
21 //for (int i=0;i<split.LongLength;i++)
22 //一个一个的取到里面的单个的值
23 foreach (string s in split)
24 {
25 //添加数据表(Info)
26 if (s.Trim() != "")
27 {
28 try//异常处理
29 {
30 //SQL语句
31 string mySql = "INSERT INTO Info(Info_To)VALUES('"+s+"')";
32 SqlCommand myCommand = new SqlCommand(mySql,myConn);
33 myConn.Open();//打开数据库
34 myCommand.ExecuteNonQuery();//执行语句
35 }
36 catch(Exception ee)//错误消息
37 {
38 }
39 finally//不管是否发生错误都要执行
40 {
41 myConn.Close();//关闭数据库
42 }
43 }
44 }
45 }
46
47 //把从下拉列表中选择的用户,添加到显示的文本框中
48 private void ListBox_name_SelectedIndexChanged(object sender, System.EventArgs e)
49 {
50 if(this.TextBox_name.Text=="")
51 {
52 this.TextBox_name.Text=this.ListBox_name.SelectedItem.Text;
53 }
54 else
55 {
56 this.TextBox_name.Text+=","+this.ListBox_name.SelectedItem.Text;
57 }
58 }
59