• 码农

    2008-03-28

    前一阵子有个需求,要求写一个群发邮件的软件。因为小弟是Web工程师(呃....自己汗一下),所以采用了asp.net 开发,实际测试中发现还真存在不少问题。
    第一,IIS有响应时间问题。
    第二,控制起来好不方便。
    So,记得姜GG写了一个PERL群发E-MAIL的代码,找来看看,没想到还发现有后续版本(python)。。用perl发一大堆邮件的一种方法。
    前面用perl写的群发邮件的脚本的python版本。 
    在自己电脑尝试装了一下PERL 2.5.8,不成,根本不会用,姜GG说你用C#写呗,俺很惭愧...答曰,没写过winForm程序...只有写一个命令行形式的垃圾邮件群发器。

    呃。。。看网上的垃圾邮件群发器都采用SMTP轮询,俺没有那么多SMTP帐号,AND 也不会装SMTP服务器,SO,写一个单一SMTP服务器的版本。

    运行环境:.Net Framework 2.0
    需要准备滴:title.txt,content.txt,list.txt 位置:C:\Mail\
                     content.txt 格式为mail@mail.com,mail2@mail.com,mail3@mail.com,
    代码:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Threading;
    namespace appMail
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filepath = @"C:\Mail\list.txt";
                string titlepath = @"C:\Mail\title.txt";
                string contentpath = @"C:\Mail\Content.txt";
                StreamReader sf = new StreamReader(filepath, Encoding.Default);
                string mailList = sf.ReadToEnd();
                sf.Close();
                StreamReader sf2 = new StreamReader(titlepath, Encoding.Default);
                string title = sf2.ReadToEnd();
                sf2.Close();
                StreamReader sf3 = new StreamReader(contentpath, Encoding.Default);
                string content = sf3.ReadToEnd();
                sf3.Close();
                Console.WriteLine("输入smtp服务器,如QQ邮箱SMTP服务器为smtp.qq.com:");
                string smtpStr = Console.ReadLine();
                Console.WriteLine("邮箱名(全名):");
                string username = Console.ReadLine();
                Console.WriteLine("邮箱密码:");
                string passwd = Console.ReadLine();
                Console.WriteLine("输入间隔发送邮件数目:");
                int mSize = int.Parse(Console.ReadLine());
                Console.WriteLine("休眠时间,单位秒");
                int tempPer =int.Parse( Console.ReadLine());
                int mPer = tempPer*1000;
                string[] str = Regex.Split(mailList, ",", RegexOptions.IgnoreCase);
                int m = 1;
                foreach (string i in str)
                {
                    if (i.Trim() != "")
                    {
                        if (m % mSize  == 0)
                        {
                            if (SendMail(smtpStr, username, passwd, username, i.Trim(), title, content))
                            {
                               Console.WriteLine("第" + m + "封:" + i + "发送完成;休眠"+ mSize +"秒钟;");
                               Thread.Sleep(mPer);
                            }
                            else
                            {
                                Console.WriteLine("第" + m + "封:" + i + "发送失败;休眠" + mSize + "秒钟;");
                                Thread.Sleep(mPer);
                            }
                           
                        }
                        else
                        {
                            if (SendMail(smtpStr, username, passwd, username, i.Trim(), title, content))
                            {
                                Console.WriteLine("第" + m + "封:" + i + "发送完成;");
                            }
                            else
                            {
                                Console.WriteLine("第" + m + "封:" + i + "发送失败;");
                            }

                        }
                        m++;
                    }
               }
                Console.WriteLine("邮件发送完毕");
                Console.ReadKey();
            }
            public static Boolean  SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody)
            {
                if (Regex.IsMatch(strto, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                {
                    //生成一个   使用SMTP发送邮件的客户端对象
                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);

                    //表示以当前登录用户的默认凭据进行身份验证
                    client.UseDefaultCredentials = true;

                    //包含用户名和密码
                    client.Credentials = new System.Net.NetworkCredential(UserName, Password);

                    //指定如何发送电子邮件。
                    //Network        电子邮件通过网络发送到   SMTP   服务器。    
                    //PickupDirectoryFromIis               将电子邮件复制到挑选目录,然后通过本地   Internet   信息服务   (IIS)   传送。    
                    //SpecifiedPickupDirectory           将电子邮件复制到   SmtpClient.PickupDirectoryLocation   属性指定的目录,然后由外部应用程序传送。    
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                    //建立邮件对象  
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);

                    //定义邮件正文,主题的编码方式
                    message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
                    message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");

                    //获取或设置一个值,该值指示电子邮件正文是否为   HTML。  
                    message.IsBodyHtml = true;

                    //指定邮件优先级
                    message.Priority = System.Net.Mail.MailPriority.Normal;

                    //邮件回复地址
                    //message.ReplyTo = new System.Net.Mail.MailAddress(rep);

                    //添加附件
                    //System.Web.Mail.MailAttachment   mailAttachment=new   System.Web.Mail.MailAttachment(@ "C:/Mail/附件.txt ");  
                    //if   (strFileName   !=   " "   &&   strFileName   !=   null)
                    //{
                    //        Attachment   data   =   new   Attachment(strFileName);
                    //        message.Attachments.Add(data);
                    //}
                    //发件人身份验证,否则163   发不了
                    client.Credentials = new System.Net.NetworkCredential(strFrom, Password);
                    //发送
                    try
                    {
                        client.Send(message);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                    finally
                    {
                    }
                }
                else {
                    return false;
                }

            }
        }
    }