zoukankan      html  css  js  c++  java
  • 用多线程发送邮箱(一次给一个用户发送N封邮件)

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

     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 }
  • 相关阅读:
    View Controller 生命周期的各个方法的用法
    IOS开发之Post 方式获取服务器数据
    委托代理
    Function
    SKPhysicsContactDelegate协议
    UITouch附加
    Remove Duplicates from Sorted Array II
    4Sum
    [Text Justification
    Count and Say
  • 原文地址:https://www.cnblogs.com/ElvisZhongShao/p/4453883.html
Copyright © 2011-2022 走看看