c#多线程传值回调发邮件显示进行状态

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Threading;
using  System.Web.Mail;

namespace  ConsoleApplication1
{
  
public   delegate   void  CallBack( bool  state,  int  count);
    
public   class  Program
    {
       
        
public   void  Show( bool  state, int  count)
        {
            Console.WriteLine(
" {0}-{1} " ,state,count);  
        }
        
static   void  Main( string [] args)
        {
            
int  tempCount  =   120 ;
            
int  tempWhileCount  =  (tempCount  /   100 *   100 ;
            
int  tempOtherCount  =  tempCount  -  tempWhileCount;

            Console.WriteLine(tempWhileCount);
            Console.WriteLine(tempOtherCount);

            
// 指定线程数
             int  size  =   10 ;
            size 
=  tempWhileCount == 0 ? ( 0 ):(tempWhileCount  <=   100   ?  ( 10 ) : (tempWhileCount  <=   1000   ?  ( 20 ) : ( 25 )));
         
            List
< MailInfo >  lall  =   new  List < MailInfo > ();
            
// 添加邮件信息
             for  ( int  i  =   0 ; i  < tempCount; i ++ )
            {
                lall.Add(
new  MailInfo( " 三毛 " +  i.ToString(), " wangdetian16 "   +  i.ToString()  +   " @qq.com " ));
            }
            
// 创建多线程
             for  ( int  i  =   0 ; i  <  size; i ++ )
            {
                ThreadData temptd 
=   new  ThreadData( new  CallBack( new  Program().Show));
                List
< MailInfo >  tempLi  =   new  List < MailInfo > ();
                
for  ( int  j  =  i  *  (tempWhileCount  /  size); j  <  (i  *  (tempWhileCount  /  size)  +  tempWhileCount  /  size); j ++ )
                {
                    tempLi.Add(lall[j]);
                }
                temptd.LisMails 
=  tempLi;
                Thread tempThread 
=   new  Thread( new  ThreadStart(temptd.Run));
                tempThread.Start();


            }
            ThreadData temptdOther 
=   new  ThreadData( new  CallBack( new  Program().Show));
            List
< MailInfo >  tempLiOther  =   new  List < MailInfo > ();
            
for  ( int  j  =  tempWhileCount; j  <  tempCount; j ++ )
            {
                tempLiOther.Add(lall[j]);
            }
            temptdOther.LisMails 
=  tempLiOther;
            Thread tempThreadOther 
=   new  Thread( new  ThreadStart(temptdOther.Run));
            tempThreadOther.Start();
            



            
// ThreadData thdata1 = new ThreadData();
            
// List<string> l1 = new List<string>();
            
// for (int i = 0; i < 1000; i++)
            
// {
            
//     l1.Add("wangdetian16"+i.ToString()+"@163.com");
            
// }
            
            
// thdata1.LisMails = l1;
            
// Thread thread = new Thread(new ThreadStart(thdata1.Run));
            
// thread.Start();

            
// ThreadData thdata2 = new ThreadData();
            
// List<string> l2 = new List<string>();
            
// for (int i = 0; i < 10; i++)
            
// {
            
//     l2.Add("wangdetian18" + i.ToString() + "@163.com");
            
// }
            
// thdata2.LisMails = l2;
            
// Thread thread1 = new Thread(new ThreadStart(thdata2.Run));
            
// thread1.Start();
            
   
        }
    }

    
public   class  ThreadData
    {
     
        
public   static   string  MailMessage  =   " {0}你好你人物要做近三分萨拉夫接撒立法精神多了几分当上了副驾驶的发生大风蓝色队服那里公司发展规划的 " ;
        
private  List < MailInfo >  _LisMails;
        
public   static   string  frommail  =   " wangdetian116688@163.com " ; // System.Configuration.ConfigurationManager.AppSettings["frommail"];
         public   static   string  username  =   " wangdetian116688 " ; // System.Configuration.ConfigurationManager.AppSettings["username"];
         public   static   string  password  =   " 987654321 " ; // System.Configuration.ConfigurationManager.AppSettings["password"];
         public  List < MailInfo >  LisMails
        {
            
set  {  this ._LisMails  =  value; }
            
get  {  return   this ._LisMails; }
        }
        
public  CallBack CB;
        
public  ThreadData(CallBack cb)
        {
            
this .CB  =  cb;
        }
        
public   void  Run()
        {
            
int  i  =   0 ;

            
foreach  (MailInfo s  in  LisMails)
            {
                System.Threading.Thread.Sleep(
1000 );
                Console.WriteLine(
string .Format(MailMessage,s.MailName + s.MailAddress));
                CB(
false , ++ i);
              
//   SendMail20(s.MailAddress, s.MailName, string.Format(MailMessage, s.MailName));
            }
            CB(
true , i);
        }
        
private   bool  SendMail( string  toMail,  string  subject,  string  body)
        {
            
try
            {


                
string  fromMail  =  ThreadData.frommail;
                MailMessage myMail 
=   new  MailMessage();
                myMail.From 
=  fromMail;
                myMail.To 
=  toMail;
                
// myMail.Cc = ccMail;
                
// myMail.Bcc = bccMail;
                myMail.Subject  =  subject;
                myMail.Body 
=  body;
                myMail.BodyFormat 
=  MailFormat.Html;

                
// 附件
                 string  ServerFileName  =   "" ;
                
// if (this.upfile.PostedFile.ContentLength != 0)
                
// {
                
//     string upFileName = this.upfile.PostedFile.FileName;
                
//     string[] strTemp = upFileName.Split('.');
                
//     string upFileExp = strTemp[strTemp.Length - 1].ToString();
                
//     ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
                
//     this.upfile.PostedFile.SaveAs(ServerFileName);
                
//     myMail.Attachments.Add(new MailAttachment(ServerFileName));
                
// }


                myMail.Fields.Add(
" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " 1 );
                myMail.Fields.Add(
" http://schemas.microsoft.com/cdo/configuration/sendusername " , ThreadData.username);  // 发送方邮件帐户
                myMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendpassword " , ThreadData.password);  // 发送方邮件密码

                SmtpMail.SmtpServer 
=   " smtp. "   +  fromMail.Substring(fromMail.IndexOf( " @ " +   1 );
                SmtpMail.Send(myMail);

                
return   true ;
            }
            
catch
            {
                
return   false ;
            }
        }
        
private   bool  SendMail20( string  toMail,  string  subject,  string  body)
        {
            
try
            {

                System.Net.Mail.MailAddress from 
=   new  System.Net.Mail.MailAddress(ThreadData.frommail);  // 邮件的发件人
                
// string fromMail = ThreadData.frommail;
                System.Net.Mail.MailMessage myMail  =   new  System.Net.Mail.MailMessage();
                
// 设置邮件的标题
                myMail.Subject  =  subject;
                
// 设置邮件的发件人
                
// Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用
                myMail.From  =  from;
                
// MailAddress(address, displayName)
                myMail.To.Add( new  System.Net.Mail.MailAddress(toMail));
               
                
/// /设置邮件的抄送收件人
                
/// /这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好
                 // mail.CC.Add(new MailAddress("Manage@hotmail.com", "尊敬的领导");

                
// 设置邮件的内容
                myMail.Body  =  body;
                
// 设置邮件的格式
                myMail.BodyEncoding  =  System.Text.Encoding.GetEncoding( " gb2312 " );
                myMail.IsBodyHtml 
=   true ;
                
// 设置邮件的发送级别
                myMail.Priority  =  System.Net.Mail.MailPriority.Normal;

                
// 设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
                
// string fileName = txtUpFile.PostedFile.FileName.Trim();
                
// fileName = "D:\UpFile\" + fileName.Substring(fileName.LastIndexOf("\") + 1);
                
// txtUpFile.PostedFile.SaveAs(fileName);  //  将文件保存至服务器
                
// mail.Attachments.Add(new Attachment(fileName));

                myMail.DeliveryNotificationOptions 
=  System.Net.Mail.DeliveryNotificationOptions.OnSuccess;

                System.Net.Mail.SmtpClient client 
=   new  System.Net.Mail.SmtpClient();
                
// 设置用于 SMTP 事务的主机的名称,填IP地址也可以了
                client.Host  =   " smtp. "   +  ThreadData.frommail.Substring(ThreadData.frommail.IndexOf( " @ " +   1 ); 
                
// "smtp.hotmail.com"; //  "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
                
// 设置用于 SMTP 事务的端口,默认的是 25
                
// client.Port = 25;
                client.UseDefaultCredentials  =   false ;
                
// 这里才是真正的邮箱登陆名和密码,比如我的邮箱地址是 wangdetian@126.com, 我的用户名为 hbgx ,我的密码是 xgbh
                client.Credentials  =   new  System.Net.NetworkCredential(ThreadData.username, ThreadData.password);
                client.DeliveryMethod 
=  System.Net.Mail.SmtpDeliveryMethod.Network;
                
// 都定义完了,正式发送了,很是简单吧!
                client.Send(myMail);    

                
return   true ;
            }
            
catch
            {
                
return   false ;
            }
        }
       
    }
    
public   class  MailInfo
    {
        
public   string  MailName;
        
public   string  MailAddress;
        
public   string  MailMessage;
        
public  MailInfo( string  mailName,  string  mailAddress)
        {
            
this .MailName  =  mailName;
            
this .MailAddress  =  mailAddress;
        }
        
public  MailInfo( string  mailName,  string  mailAddress,  string  mailMessage)
        {
            
this .MailName  =  mailName;
            
this .MailAddress  =  mailAddress;
            
this .MailMessage  =  mailMessage;
        }
    } 
       
    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值