`

C#带数据多线程发邮件

阅读更多

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mail;

namespace ConsoleApplication1
{
 
    class Program
    {
        static void Main(string[] args)
        {
            int tempCount = 12;
            int tempWhileCount = (tempCount / 10) * 10;
            int tempOtherCount = tempCount - tempWhileCount;

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

            //指定线程数
            int size = 5;
            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();
                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();
            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 = "mailto:wangdetian116688@163.com%22;//System.Configuration.ConfigurationManager.AppSettings[%22frommail"];
        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 void Run()
        {
            foreach (MailInfo s in LisMails)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine(string.Format(MailMessage,s.MailName+s.MailAddress));
                SendMail20(s.MailAddress, s.MailName, string.Format(MailMessage, s.MailName));
            }
        }
        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;
        }
    }
      
    }


 

分享到:
评论

相关推荐

    c#定时器+多线程

    c#定时器+多线程,定时更新数据 定时器方法,可发送定时邮件

    c#邮件自动发送程序源码

    c#+vs2008 写的邮件自动发送程序。 将要发送的邮件写入到sql Server中的表(固定的表结构),由此程序周期性地从表中读取要待发邮件并进行发送。 发送邮件的方法在单独的线程中运行。 可配置多个smtp进行发送。 其中...

    c#(c sharp)实用100小实例

    c#(c sharp)实用100小实例 内容有 锯齿型数组 重载 邮件提示器 邮件发送程序 透明窗口 进度条的使用 运动的球 菜单的使用 翻牌游戏 网页下载器 线程例子 线程休眠 类型转换 简易计算器 生成网页 添加控件 浮动的...

    C#网络编程培训课件

    C#网络编程---第1章_进程、线程与网络协议.ppt C#网络编程---第2章_TCP应用编程.ppt C#网络编程---第3章_UDP应用编程.ppt C#网络编程---第4章_P2P应用编程.ppt C#网络编程---第5章_SMTP与POP3应用编程.ppt C#网络...

    超级多的C#辅助类大全

    超级多的C#辅助类大全 网上有各式各样的帮助类,公共类,但是比较零碎,经常有人再群里或者各种社交账号上问有没有这个helper,那个helper,于是萌生了收集全部helper的念头,以便日后使用。各式各样的几乎都能找到,...

    C#源码大集合 02(共3卷)

    └ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程互斥 ├─chap09-多线程 ├─D00-多线程 ├─Windows多线程编程技术与实例 ... ... ├─多线程,多接收...

    C#开发经验技巧宝典

    1081 利用C#操作XML数据 653 1082 显示数据库记录 653 1083 如何扫描计算机端口号 653 1084 如何获得指定网页的源代码 654 1085 如何将指定URL数据下载到本地文件 654 1086 如何设置Internet浏览器外观 ...

    C#源码大集合 03(共3卷)

    ├─第53讲 文件系统监视器 │ └─第55讲 聊天室扩展之三——使用MS Agent &lt;br&gt;└ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程...

    C#源码大集合 01(共3卷)

    ├─第53讲 文件系统监视器 │ └─第55讲 聊天室扩展之三——使用MS Agent &lt;br&gt;└ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程...

    明日科技C#开发入门及项目实战

    实例180 使用多线程制作端口扫描工具 实例181 使用线程扫描局域网ip地址 实例182 使用线程休眠控制图片以百叶窗效果显示 实例183 使用线程控制向窗体中拖放图片并显示 实例184 使用线程制作动画效果的状态栏 实例185...

    《C#经典编程220例》.(明日科技).【带书签】-共3部分

    实例180 使用多线程制作端口扫描工具 343 实例181 使用线程扫描局域网ip地址 345 实例182 使用线程休眠控制图片以百叶窗效果显示 347 实例183 使用线程控制向窗体中拖放图片并显示 349 实例184 使用线程制作动画效果...

    C#学习文档实例讲解

    第1章 进程、线程与网络协议 7 1.1 进程和线程 7 1.1.1 Process类 7 1.1.2 Thread类 9 1.1.3 在一个线程中操作另一个线程的控件 13 1.2 IP地址与端口 15 1.2.1 TCP/IP网络协议 16 1.2.2 IPAddress类与Dns类 17 1.2.3...

    C#网络应用高级编程

    第1章 进程、线程与网络协议  1.1 进程和线程  1.1.1 Process类  1.1.2 Thread类  1.1.3 在一个线程中操作另一个线程的控件 1.2 IP地址与端口  1.2.1 TCP/IP  1.2.2 IPAddress类与Dns类  ...

    C#开发典型模块大全

    8.3.4 为邮件上传多个附件 193 8.3.5 邮件接收实现 194 8.3.6 查看邮件详细信息 196 8.3.7 下载附件的实现 197 8.3.8 删除邮件实现 198 8.3.9 用户管理 198 第9章 短信群发模块 9.1 设计思路 202 ...

    C#全能速查宝典

    《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...

    C#开发典型模块大全(光盘)第二部分

    8.3.4 为邮件上传多个附件 8.3.5 邮件接收实现 8.3.6 查看邮件详细信息 8.3.7 下载附件的实现 8.3.8 删除邮件实现 8.3.9 用户管理 第9章 短信群发模块 9.1 设计思路 9.2 关键技术 9.2.1 短信猫中API函数的使用 ...

    C#开发典型模块大全(光盘)

    8.3.4 为邮件上传多个附件 8.3.5 邮件接收实现 8.3.6 查看邮件详细信息 8.3.7 下载附件的实现 8.3.8 删除邮件实现 8.3.9 用户管理 第9章 短信群发模块 9.1 设计思路 9.2 关键技术 9.2.1 短信猫中API函数的使用 ...

    C# winform典型系统开发模板

     8.3.4 为邮件上传多个附件 193  8.3.5 邮件接收实现 194  8.3.6 查看邮件详细信息 196  8.3.7 下载附件的实现 197  8.3.8 删除邮件实现 198  8.3.9 用户管理 198 第9章 短信群发模块   ...

    C#开发典型模块大全(光盘)第一部分

    8.3.4 为邮件上传多个附件 8.3.5 邮件接收实现 8.3.6 查看邮件详细信息 8.3.7 下载附件的实现 8.3.8 删除邮件实现 8.3.9 用户管理 第9章 短信群发模块 9.1 设计思路 9.2 关键技术 9.2.1 短信猫中API函数的使用 ...

    Visual C#网络编程技术与实践源代码

    全书共分13章,主要内容包括网络编程原理、Visual C# 2005的套接字以及多线程的开发、基于各种不同协议的网络编程应用模块,并通过几个典型的实例介绍了Visual C# 2005网络编程的实际应用。 本书注重代码的通用性和...

Global site tag (gtag.js) - Google Analytics