欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#郵件定時(shí)群發(fā)工具Atilia用法實(shí)例

 更新時(shí)間:2015年08月19日 15:00:33   作者:北風(fēng)其涼  
這篇文章主要介紹了C#郵件定時(shí)群發(fā)工具Atilia用法,較為詳細(xì)的分析了Atilia實(shí)現(xiàn)郵件定時(shí)群發(fā)功能的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#郵件定時(shí)群發(fā)工具Atilia用法。分享給大家供大家參考。具體如下:

一、Atilia可以做什么

Atilia是一個(gè)基于命令行的C#程序,可以發(fā)送郵件給一個(gè)或多個(gè)人。Atilia通過(guò)QQ的SMTP服務(wù)發(fā)送郵件,可以發(fā)送附件,可以在配置文件中手動(dòng)配置收信人。

二、運(yùn)行Atilia需要什么

在Atilia應(yīng)用程序的同一目錄下,有如下文件

1)一個(gè)Attachments文件夾,Atilia會(huì)將里面所有的子文件(不含子文件夾及其中文件)視作附件發(fā)送給收信人

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

3)Atilia.html文件,是被發(fā)送的郵件文本

這三個(gè)文件都位于編譯環(huán)境中的根目錄下,在“程序集屬性→生成事件→后期生成事件命令行”中可以將編譯環(huán)境中的文件復(fù)制到Debug目錄中

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

三、收信人的配置

收信人配置的規(guī)則很簡(jiǎn)單,保存在AddressBook.xml中

<?xml version="1.0" encoding="gb2312"?>
<!--通訊錄-->
<Root Subject="測(cè)試郵件">
 <Person Name="江有汜" Email="1239063237@qq.com" />
 <Person Name="淫俠" Email="****@qq.com" />
</Root>

每一個(gè)Person代表了一個(gè)人,Name是后面Email的一個(gè)標(biāo)識(shí),Email是收信人的地址

Atilia運(yùn)行后會(huì)將郵件發(fā)給通信錄中存在的每一個(gè)Person

四、輸入?yún)?shù)

1)沒(méi)有輸入?yún)?shù):當(dāng)即準(zhǔn)備發(fā)送所有的郵件,發(fā)送前詢問(wèn)是否發(fā)送:要求輸入(y/n)

2)兩個(gè)輸入?yún)?shù):8位的年月日 和 6位的時(shí)分秒,如2014年9月30日23時(shí)40分00秒,就需要輸入如下命令運(yùn)行: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("1254355584@qq.com");
      //讀取收信人列表
      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("正在生成郵件主題,設(shè)定編碼格式");
      mlmssg.Subject = (xroot as XmlElement).GetAttribute("Subject");
      mlmssg.SubjectEncoding = System.Text.Encoding.UTF8;
      Console.WriteLine("正在讀取郵件內(nèi)容(Atilia.html),設(shè)定編碼格式");
      mlmssg.Body = File.ReadAllText(
        "Atilia.html", Encoding.GetEncoding("gb2312"));
      mlmssg.BodyEncoding = System.Text.Encoding.UTF8;
      mlmssg.IsBodyHtml = true;
      Console.WriteLine("設(shè)定郵件發(fā)送級(jí)別:Normal");
      mlmssg.Priority = MailPriority.Normal;
      //mailMessage.ReplyTo = new MailAddress("1239063237@qq.com"); //已過(guò)時(shí)
      //讀取附件列表
      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;
          //查詢文件夾中的各個(gè)文件
          foreach (FileInfo f in dif.GetFiles())
          {
            //分類(lèi)討論幾種文件類(lèi)型
            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);
          }
        }
      }
      //設(shè)定SMTP服務(wù)器
      Console.WriteLine("準(zhǔn)備設(shè)置SMTP服務(wù)");
      SmtpClient smtpclt = new SmtpClient();
      smtpclt.DeliveryMethod = SmtpDeliveryMethod.Network;
      Console.WriteLine("正在填寫(xiě)SMTP服務(wù)器地址");
      smtpclt.Host = "smtp.qq.com";
      Console.WriteLine("正在填寫(xiě)登錄賬戶和登錄密碼");
      smtpclt.Credentials = 
        new System.Net.NetworkCredential("1254355584", "****");
      //沒(méi)有指定時(shí)間
      if (args.Length == 0)
      {
        //發(fā)送郵件前的最后提示
        while (true)
        {
          Console.WriteLine("您確實(shí)要發(fā)送這些郵件嗎? (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("輸入錯(cuò)誤");
          }
        }
      }
      else 
      {
        int time_a = 0; //年月日
        int time_b = 0; //時(shí)分秒
        int time_now_a;
        int time_now_b;
        try
        {
          //時(shí)間分為兩部分
          //前一部分是8位數(shù)字表示的時(shí)間 如:20140930
          //后一部分是4位數(shù)字表示的時(shí)間 如:210755
          if (args.Length != 2)
          {
            throw new Exception("參數(shù)不正確");
          }
          //年月日
          if (!Regex.IsMatch(args[0], "^[0-9]{8}$"))
          {
            throw new Exception("錯(cuò)誤的時(shí)間數(shù)據(jù)");
          }
          bool b1 = int.TryParse(args[0], out time_a);
          //時(shí)分秒
          if (!Regex.IsMatch(args[1], "^[0-9]{6}$"))
          {
            throw new Exception("錯(cuò)誤的時(shí)間數(shù)據(jù)");
          }
          bool b2 = int.TryParse(args[1], out time_b);
          if ((!b1) || (!b2))
          {
            throw new Exception("時(shí)間數(shù)據(jù)轉(zhuǎn)換失敗");
          }
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
          Console.WriteLine("命令示例: Atilia 20140930 210755");
          //按任意鍵繼續(xù)
          Console.WriteLine("按任意鍵繼續(xù)...");
          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("正在等待發(fā)信時(shí)間 {0} {1}",
                time_a, time_b);
              counter = 0;
            }
          }
          else
          {
            break;
          }
        }
      }
      //發(fā)送郵件
      Console.WriteLine("正在發(fā)送郵件,請(qǐng)稍候 ...");
      smtpclt.Send(mlmssg);
      //mail from address must be same as authorization user
      //QQ郵箱→設(shè)置→賬戶→POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù)
      //勾選POP3/SMTP服務(wù)
      Console.WriteLine("郵件發(fā)送完畢,正在釋放資源");
      smtpclt.Dispose();
      mlmssg.Dispose();
      Console.WriteLine("按任意鍵繼續(xù)...");
      Console.ReadKey();
      Console.WriteLine("\b");
    }
  }
}

附:慶祝國(guó)慶節(jié)的Atilia.html內(nèi)容

<html>
  <head>
    <title>
      國(guó)慶快樂(lè)!
    </title> 
    <style> 
      body{text-align:center} 
    </style>
  </head> 
  <body>
    <span style="color:red;font-size:250%;font-weight:800">
      江有汜 攜 Atilia 恭祝大家 國(guó)慶快樂(lè)!??!
    </span>
    <hr />
      <img src="http://upload.wikimedia.org/wikipedia/commons/c/ce/Chinese_flag_%28Beijing%29_-_IMG_1104.jpg" 
        alt="中華人民共和國(guó)國(guó)旗" height="400" width="660"/>
    <hr>
    <b>十一小長(zhǎng)假,可要注意好好休息啊~~~</b><br>
    <p>
      圖片來(lái)源:
      <a >
        維基共享資源:飄揚(yáng)在北京的五星紅旗
      </a>
    </p>
    <p>
      程序源碼:
      <a >
        源碼地址
      </a>
    </p>
    刮開(kāi)涂層贏千萬(wàn)大獎(jiǎng):
    <span style="background-color:black;color:black">
      Atilia 很萌的,乃們不要黑她 :P
    </span> 
  </body>
</html>

發(fā)送后的效果展示:

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論