Atilia - C#邮件定时群发工具

一、Atilia可以做什么

Atilia - C#邮件定时群发工具_第1张图片

Atilia是一个基于命令行的C#程序,可以发送邮件给一个或多个人。Atilia通过QQ的SMTP服务发送邮件,可以发送附件,可以在配置文件中手动配置收信人。

二、运行Atilia需要什么

在Atilia应用程序的同一目录下,有如下文件

1)一个Attachments文件夹,Atilia会将里面所有的子文件(不含子文件夹及其中文件)视作附件发送给收信人

2)AddressBook.xml文件,用于配置收信人

3)Atilia.html文件,是被发送的邮件文本

这三个文件都位于编译环境中的根目录下,在“程序集属性→生成事件→后期生成事件命令行”中可以将编译环境中的文件复制到Debug目录中

xcopy "$(ProjectDir)Atilia.html" "$(TargetDir)" /Y
xcopy "$(ProjectDir)AddressBook.xml" "$(TargetDir)" /Y
xcopy "$(ProjectDir)Attachments\*" "$(TargetDir)\Attachments\" /Y

三、收信人的配置

收信人配置的规则很简单,保存在AddressBook.xml中




  
  

每一个Person代表了一个人,Name是后面Email的一个标识,Email是收信人的地址

Atilia运行后会将邮件发给通信录中存在的每一个Person

四、输入参数

1)没有输入参数:当即准备发送所有的邮件,发送前询问是否发送:要求输入(y/n)

2)两个输入参数:8位的年月日 和 6位的时分秒,如2014年9月30日23时40分00秒,就需要输入如下命令运行:Atilia 20140930 234000

五、程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Mime;
using System.Xml;
using System.Text.RegularExpressions;

namespace Atilia
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mlmssg = new MailMessage();
            
            mlmssg.From = new MailAddress("[email protected]");

            //读取收信人列表
            Console.WriteLine("正在读取收信人列表");
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load("AddressBook.xml");
            XmlNode xroot = xdoc.SelectSingleNode("Root");
            foreach (var xe in xroot.ChildNodes)
            {
                //判断读取到的是XmlElement而不是注释
                if (xe is XmlElement)
                {
                    mlmssg.To.Add((xe as XmlElement).GetAttribute("Email"));
                    Console.WriteLine("增加收信人 {0} 邮箱地址为 {1}",
                        (xe as XmlElement).GetAttribute("Name"),
                        (xe as XmlElement).GetAttribute("Email"));
                }
            }

            Console.WriteLine("正在生成邮件主题,设定编码格式");
            mlmssg.Subject = (xroot as XmlElement).GetAttribute("Subject");
            mlmssg.SubjectEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("正在读取邮件内容(Atilia.html),设定编码格式");
            mlmssg.Body = File.ReadAllText(
                "Atilia.html", Encoding.GetEncoding("gb2312"));
            mlmssg.BodyEncoding = System.Text.Encoding.UTF8;
            mlmssg.IsBodyHtml = true;
            Console.WriteLine("设定邮件发送级别:Normal");
            mlmssg.Priority = MailPriority.Normal;
            //mailMessage.ReplyTo = new MailAddress("[email protected]"); //已过时

            //读取附件列表
            Console.WriteLine("正在读取附件列表");
            if (System.IO.Directory.Exists("Attachments"))
            {
                System.IO.DirectoryInfo dif = new DirectoryInfo("Attachments");
                if (dif.GetFiles().Count() != 0) //只读取文件,不查看子文件夹
                {
                    System.Net.Mail.Attachment att = null;
                    //查询文件夹中的各个文件
                    foreach (FileInfo f in dif.GetFiles())
                    {
                        //分类讨论几种文件类型
                        switch (f.Extension.ToLower())
                        {
                            case ".rar":
                            case ".zip":
                                {
                                    att = new Attachment(f.FullName, 
                                        MediaTypeNames.Application.Zip);
                                } 
                                break;
                            case ".pdf":
                                {
                                    att = new Attachment(f.FullName,
                                        MediaTypeNames.Application.Pdf);
                                }
                                break;
                            case ".rtf":
                                {
                                    att = new Attachment(f.FullName,
                                        MediaTypeNames.Application.Rtf);
                                }
                                break;
                            default: //其他格式不指定格式
                                {
                                    att = new Attachment(f.FullName,
                                        MediaTypeNames.Application.Octet);
                                }
                                break;
                        }

                        ContentDisposition cd = att.ContentDisposition;
                        cd.CreationDate = File.GetCreationTime(f.FullName);
                        cd.ModificationDate = File.GetLastWriteTime(f.FullName);
                        cd.ReadDate = File.GetLastAccessTime(f.FullName);

                        Console.WriteLine("成功添加附件 {0}", f.Name);
                        mlmssg.Attachments.Add(att);
                    }
                }
            }

            //设定SMTP服务器
            Console.WriteLine("准备设置SMTP服务");
            SmtpClient smtpclt = new SmtpClient();
            smtpclt.DeliveryMethod = SmtpDeliveryMethod.Network;
            Console.WriteLine("正在填写SMTP服务器地址");
            smtpclt.Host = "smtp.qq.com";
            Console.WriteLine("正在填写登录账户和登录密码");
            smtpclt.Credentials = 
                new System.Net.NetworkCredential("1254355584", "****");

            //没有指定时间
            if (args.Length == 0)
            {
                //发送邮件前的最后提示
                while (true)
                {
                    Console.WriteLine("您确实要发送这些邮件吗? (y/n)");
                    string result;
                    result = Console.ReadLine();
                    result = result.ToLower().Trim();
                    if (result == "y")
                    {
                        break;
                    }
                    else if (result == "n")
                    {
                        Environment.Exit(0);
                    }
                    else
                    {
                        Console.WriteLine("输入错误");
                    }
                }
            }
            else 
            {
                int time_a = 0; //年月日
                int time_b = 0; //时分秒
                int time_now_a;
                int time_now_b;

                try
                {
                    //时间分为两部分
                    //前一部分是8位数字表示的时间 如:20140930
                    //后一部分是4位数字表示的时间 如:210755
                    if (args.Length != 2)
                    {
                        throw new Exception("参数不正确");
                    }

                    //年月日
                    if (!Regex.IsMatch(args[0], "^[0-9]{8}$"))
                    {
                        throw new Exception("错误的时间数据");
                    }

                    bool b1 = int.TryParse(args[0], out time_a);

                    //时分秒
                    if (!Regex.IsMatch(args[1], "^[0-9]{6}$"))
                    {
                        throw new Exception("错误的时间数据");
                    }

                    bool b2 = int.TryParse(args[1], out time_b);

                    if ((!b1) || (!b2))
                    {
                        throw new Exception("时间数据转换失败");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("命令示例: Atilia 20140930 210755");
                    //按任意键继续
                    Console.WriteLine("按任意键继续...");
                    Console.ReadKey();
                    Console.WriteLine("\b");
                    Environment.Exit(0);
                }


                int counter = 0;
                while (true)
                {
                    time_now_a = DateTime.Now.Year * 10000 + 
                        DateTime.Now.Month * 100 + DateTime.Now.Day;
                    time_now_b = DateTime.Now.Hour * 10000 +
                        DateTime.Now.Minute * 100 + DateTime.Now.Second;
                    if (time_now_a < time_a || 
                        (time_now_a >= time_a && time_now_b < time_b))
                    {
                        System.Threading.Thread.Sleep(500);
                        counter++;
                        if (counter % 10 == 0)
                        {
                            Console.WriteLine("正在等待发信时间 {0} {1}",
                                time_a, time_b);
                            counter = 0;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //发送邮件
            Console.WriteLine("正在发送邮件,请稍候 ...");
            smtpclt.Send(mlmssg);

            //mail from address must be same as authorization user
            //QQ邮箱→设置→账户→POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
            //勾选POP3/SMTP服务

            Console.WriteLine("邮件发送完毕,正在释放资源");

            smtpclt.Dispose();
            mlmssg.Dispose();
            
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
            Console.WriteLine("\b");
        }
    }
}

附:庆祝国庆节的Atilia.html内容


    
        
            国庆快乐!
         
        
     
    
        
            江有汜 携 Atilia 恭祝大家 国庆快乐!!!
        
        
            
        
        十一小长假,可要注意好好休息啊~~~
        

            图片来源:                              维基共享资源:飘扬在北京的五星红旗                      

        

            程序源码:                              源码地址                      

        刮开涂层赢千万大奖:                      Atilia 很萌的,乃们不要黑她 :P               

发送后的效果展示:

Atilia - C#邮件定时群发工具_第2张图片

END

转载于:https://my.oschina.net/Tsybius2014/blog/323703

你可能感兴趣的:(人工智能,c#)