首页  ·  知识 ·  编程语言
C#QQEMAIL邮件批量发送程序
网友  其它 | http://blog.csdn.net/luyikk/archive/2007/03/06/1521904.aspx  .NET  编辑:德仔   图片来源:网络
ausing System; using System.Collections.Generic; using System.ComponentModel; using System.Data;<
ausing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Xml.Serialization;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Web.Mail;
namespace EMAILQQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.listView1.View = View.Details;
            this.listView1.Columns.Add("用户名", this.listView1.Width / 3, HorizontalAlignment.Left);
            this.listView1.Columns.Add("密码", this.listView1.Width / 3, HorizontalAlignment.Right);
            this.listView1.Columns.Add("smtp服务器", this.listView1.Width / 3, HorizontalAlignment.Left);

            try
            {
                XmlSerializer srl = new XmlSerializer(typeof(iock));
                StreamReader red = new StreamReader("lock.xml");
                iock af = (iock)srl.Deserialize(red);
                red.Close();
                this.textBox5.Text = af.i;
                this.textBox6.Text = af.v;
            }
            catch
            {
            }
        }
        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = this.textBox1.Text;
            lvi.Tag = i + 1;
            lvi.SubItems.Add(textBox2.Text);
            lvi.SubItems.Add(textBox3.Text);
            this.listView1.Items.Add(lvi);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.SelectedItems[0].Index >= 0)
                {
                    this.listView1.Items.RemoveAt(this.listView1.SelectedItems[0].Index);
                }
            }
            catch
            {
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private void button3_Click_1(object sender, EventArgs e)
        {
            item ac=new item(listView1.Items.Count);
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                ac.aa[i] = listView1.Items[i].Text;
                ac.bb[i] = listView1.Items[i].SubItems[1].Text;
                ac.cc[i] = listView1.Items[i].SubItems[2].Text;
            }

            XmlSerializer srl = new XmlSerializer(typeof(item));
            StreamWriter wrt = new StreamWriter("items.xml");
            srl.Serialize(wrt, ac);
            wrt.Close();
        }
        private void button4_Click(object sender, EventArgs e)
        {

            this.listView1.Items.Clear();
            XmlSerializer srl = new XmlSerializer(typeof(item));
            StreamReader red = new StreamReader("items.xml");
            item a = (item)srl.Deserialize(red);
            red.Close();
          
            for (int i =0; i<a.i; i++)
            {
               
                ListViewItem lvi = new ListViewItem();
                lvi.Text = a.aa[i];
                lvi.Tag = i;
                lvi.SubItems.Add(a.bb[i]);
                lvi.SubItems.Add(a.cc[i]);
                this.listView1.Items.Add(lvi);
            }

        }

        private void label5_Click(object sender, EventArgs e)
        {
        }

        private string[] lis1;
        private string[] lis2;
        private string[] lis3;
        private int x;
        private int QQIDX;
        private int QQIDZ;
        private string from;
        private string text;
        private bool acx;
        private void button5_Click(object sender, EventArgs e)
        {

            free = -1;
            eac = true;
            lis1 = new string[listView1.Items.Count];
            lis2 = new string[listView1.Items.Count];
            lis3 = new string[listView1.Items.Count];
            acx = this.checkBox1.Checked;
           
            x = listView1.Items.Count;
            QQIDX = int.Parse(this.textBox5.Text)-1;
            QQIDZ = int.Parse(this.textBox6.Text);

            from = this.textBox4.Text;
            text=this.richTextBox1.Text;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                lis1[i]=listView1.Items[i].Text;
                lis2[i]=listView1.Items[i].SubItems[1].Text;
                lis3[i]=listView1.Items[i].SubItems[2].Text;

            }
            Thread[] thread = new Thread[listView1.Items.Count];
           
            for(int i=0;i<x;i++)
            {
               
                thread[i] = new Thread(new ThreadStart(setemail));
                thread[i].Start();
                try
                {
                    Thread.Sleep(int.Parse(this.textBox7.Text));
                }
                catch
                {
                    MessageBox.Show("每线程延迟时间错误");
                }
            }
                             
       
            }
       
        private int free=-1;

        private int getlist()
        {
            lock (this)
            {
                free++;
                return free;
            }

        }
        private void UpdateUI(object o, System.EventArgs e)
        {
            this.textBox5.Text = o.ToString();
        }
        private void UpdateUI2(object o, System.EventArgs e)
        {
            this.richTextBox2.AppendText (o.ToString());
        }
        private string getfull()
        {
            lock (this)
            {
                QQIDX++;
                if (QQIDX == QQIDZ)
                    eac = false;
                this.textBox5.BeginInvoke(new System.EventHandler(UpdateUI), QQIDX.ToString());
                return QQIDX.ToString();
            }
        }
        private bool eac = true;

        public  string getwan(string s)
        {
            lock (this)
            {
                try
                {
                    Int16 d = 0;
                    while (true)
                    {

                        if (s[d] == '.' || d == s.Length)
                        {
                            break;
                        }
                        d++;
                    }
                    return s.Remove(0, d + 1);
                }
                catch
                {
                  //  MessageBox.Show("用户数据库中发现有错误的地址:" + s);
                    return s;
                }
          
            }
        }
        private string emailname;
        private void getemailname(string a)
        {
            lock (this)
            {
                emailname = a;
            }
        }
        private string emailfale;
        private void getemailfale(string a)
        {
            lock (this)
            {
                emailfale = a;
            }
        }

        private void setemail()
        {
         
                int i = getlist();

                while (eac)
                {
                    try
                    {
                        string full;
                      
                            full = getfull();
                       
                        MailMessage mail = new MailMessage();
                        mail.To = full + "@qq.com";
                     
                        getemailname(mail.To);
                        if (acx == false)
                        {
                            mail.From = lis1[i] + "@" + getwan(lis3[i]);
                        }
                        else
                        {
                            mail.From = this.textBox9.Text;
                        }

                        getemailfale(mail.From);
                        mail.Subject = this.from;
                        mail.Body = this.text;
                        mail.BodyFormat = MailFormat.Html;
                        mail.Priority = MailPriority.High;
                        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
                        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", lis1[i]); //set your username here
                        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", lis2[i]); //set your password here
                        SmtpMail.SmtpServer = lis3[i];
                        SmtpMail.Send(mail);
                        this.richTextBox2.BeginInvoke(new System.EventHandler(UpdateUI2), emailfale + ":" + emailname + "---OK\r\n");
                    }
                    catch (Exception ex)
                    {
                        this.richTextBox2.BeginInvoke(new System.EventHandler(UpdateUI2), emailfale + ":" + emailname + "EOR:" + ex.Message + "\r\n");
                       
                    }
                    try
                    {
                        Thread.Sleep(int.Parse(this.textBox7.Text));
                    }
                    catch
                    {
                        MessageBox.Show("每线程延迟时间错误");
                    }
                }
           
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.eac = false;
            iock a = new iock();
            a.i=this.textBox5.Text;
            a.v = this.textBox6.Text;

            XmlSerializer srl = new XmlSerializer(typeof(iock));
            StreamWriter wrt = new StreamWriter("lock.xml");
            srl.Serialize(wrt, a);
            wrt.Close();

        }
    }

    public class iock
    {
        public string  i;
        public string v;
    }
    public class item
    {
        public int i = 0;
        public string[] aa;
        public string[] bb;
        public string[] cc;
        public item()
        {
        }
        public  item(int a)
        {
            i = a;
            aa = new string[a];
            bb = new string[a];
            cc = new string[a];
          
        }
    }
}
 
本文作者:网友 来源:其它 | http://blog.csdn.net/luyikk/archive/2007/03/06/1521904.aspx
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读