码迷,mamicode.com
首页 > 编程语言 > 详细

用多线程发送邮箱(一次给一个用户发送N封邮件)

时间:2015-04-24 18:09:05      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

前台不用写,后台执行方法就可以了。

 1 namespace SendMail
 2 {
 3     public partial class SendMail_Page : System.Web.UI.Page
 4     {
 5         protected void Page_Load(object sender, EventArgs e)
 6         {
 7             if (!IsPostBack)
 8             {
 9                 ThStart();
10             }
11         }
12         public Thread[] threads = new Thread[5];
13         //开启线程
14         public void ThStart()
15         {
16             ThreadStart thsrt = new ThreadStart(SendMail);
17             //循环5个线程  
18             for (int i = 0; i < 5; i++)
19             {
20                 threads[i] = new Thread(thsrt);
21                 threads[i].IsBackground = true;
22             }
23             //循环遍历所有的5个线程  
24             foreach (Thread th in threads)
25             {
26                 th.Start();
27             }
28         }
29 
30         public void SendMail()
31         {
32             try
33             {
34                 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
35                 client.Host = "smtp.qq.com";//qq郵箱用這個,網易的就用smtp.126.com,看情況而定
36                 client.UseDefaultCredentials = false;
37                 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
38                 client.Credentials = new System.Net.NetworkCredential("發送郵箱", "郵箱密碼");//驗證發送郵箱和這個一樣不
39                 System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
40                 Message.From = new System.Net.Mail.MailAddress("發送郵箱");
41                 Message.To.Add("接收郵箱");
42                        
43                 Message.Subject = "用戶測試";
44                 Message.Body = "主題——測試——郵箱發送";
45                 Message.SubjectEncoding = System.Text.Encoding.UTF8;
46                 Message.BodyEncoding = System.Text.Encoding.UTF8;
47                 Message.Priority = System.Net.Mail.MailPriority.High;
48                 Message.IsBodyHtml = true;
49                 client.Send(Message);
50                 Thread.Sleep(1000);
51             }
52             catch (Exception)
53             {  
54                 //循环遍历所有的5个线程  
55                 foreach (Thread th in threads)
56                 {
57                     //中止线程  
58                     th.Abort();
59                 }
60             }
61         }
62     }
63 }

 

用多线程发送邮箱(一次给一个用户发送N封邮件)

标签:

原文地址:http://www.cnblogs.com/ElvisZhongShao/p/4453883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!