C#数据库邮件列表发送邮件

 1 using System;
 2     using System.Net;
 3     using System.Net.Mail;
 4     using System.Text;
 5     using System.Threading;
 6 
 7     public class test
 8     {
 9         delegate void sendDelegate(string fromstring to, string subject, string body, string host, int port, string userName, string password);
10         /// <summary>
11         /// 发送电子邮件
12         /// </summary>
13         /// <param name="from">发件人</param>
14         /// <param name="to">收件人</param>
15         /// <param name="subject">邮件主题</param>
16         /// <param name="body">邮件内容</param>
17         /// <param name="host">发送服务地址(smtp.qq.com)</param>
18         /// <param name="port">发送邮件服务器端口(25) int型</param>
19         /// <param name="userName">用户名</param>
20         /// <param name="password">密码</param>
21         public void sendmail(string fromstring to, string subject, string body, string host, int port, string userName, string password)
22         {
23             MailMessage message = new MailMessage(from, to, subject, body);
24             message.IsBodyHtml = true;
25 
26             message.BodyEncoding = MediaTypeNames.Text.Encoding.UTF8;
27             message.Attachments.Add(new Attachment("c:\\log.log"));
28             SmtpClient client = new SmtpClient(host, port);
29             client.Credentials = new NetworkCredential(userName, password);
30             client.DeliveryMethod = SmtpDeliveryMethod.Network;
31             client.Send(message);
32         }
33         public void sby()
34         {
35             SqlConnection conn = new SqlConnection();
36             conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=db_showHouse";   //打开连接
37             conn.Open();
38             SqlCommandcmd = new SqlCommand("select Email from Employee", conn);
39             SqlDataReader drNew = cmd.ExecuteReader();
40             if (drNew.HasRows)
41             {
42                 while (drNew.Read())
43                     new sendDelegate(sendmail).BeginInvoke("someone@somecompany.com", drNew[0].ToString(), "subject""body""smtp.somescompany.com"25"userName""password");
44             }
45             drNew.Close();
46         }
47     }
posted @ 2012-02-06 17:03  wenanry  阅读(649)  评论(0编辑  收藏  举报