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

Atilia是一個基于命令行的C#程序,可以發(fā)送郵件給一個或多個人。Atilia通過QQ的SMTP服務(wù)發(fā)送郵件,可以發(fā)送附件,可以在配置文件中手動配置收信人。
二、運(yùn)行Atilia需要什么
在Atilia應(yīng)用程序的同一目錄下,有如下文件
1)一個Attachments文件夾,Atilia會將里面所有的子文件(不含子文件夾及其中文件)視作附件發(fā)送給收信人
2)AddressBook.xml文件,用于配置收信人
3)Atilia.html文件,是被發(fā)送的郵件文本
這三個文件都位于編譯環(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ī)則很簡單,保存在AddressBook.xml中
<?xml version="1.0" encoding="gb2312"?> <!--通訊錄--> <Root Subject="測試郵件"> <Person Name="江有汜" Email="1239063237@qq.com" /> <Person Name="淫俠" Email="****@qq.com" /> </Root>
每一個Person代表了一個人,Name是后面Email的一個標(biāo)識,Email是收信人的地址
Atilia運(yùn)行后會將郵件發(fā)給通信錄中存在的每一個Person
四、輸入?yún)?shù)
1)沒有輸入?yún)?shù):當(dāng)即準(zhǔn)備發(fā)送所有的郵件,發(fā)送前詢問是否發(fā)送:要求輸入(y/n)
2)兩個輸入?yún)?shù):8位的年月日 和 6位的時分秒,如2014年9月30日23時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ā)送級別:Normal");
mlmssg.Priority = MailPriority.Normal;
//mailMessage.ReplyTo = new MailAddress("1239063237@qq.com"); //已過時
//讀取附件列表
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);
}
}
}
//設(shè)定SMTP服務(wù)器
Console.WriteLine("準(zhǔn)備設(shè)置SMTP服務(wù)");
SmtpClient smtpclt = new SmtpClient();
smtpclt.DeliveryMethod = SmtpDeliveryMethod.Network;
Console.WriteLine("正在填寫SMTP服務(wù)器地址");
smtpclt.Host = "smtp.qq.com";
Console.WriteLine("正在填寫登錄賬戶和登錄密碼");
smtpclt.Credentials =
new System.Net.NetworkCredential("1254355584", "****");
//沒有指定時間
if (args.Length == 0)
{
//發(fā)送郵件前的最后提示
while (true)
{
Console.WriteLine("您確實要發(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("輸入錯誤");
}
}
}
else
{
int time_a = 0; //年月日
int time_b = 0; //時分秒
int time_now_a;
int time_now_b;
try
{
//時間分為兩部分
//前一部分是8位數(shù)字表示的時間 如:20140930
//后一部分是4位數(shù)字表示的時間 如:210755
if (args.Length != 2)
{
throw new Exception("參數(shù)不正確");
}
//年月日
if (!Regex.IsMatch(args[0], "^[0-9]{8}$"))
{
throw new Exception("錯誤的時間數(shù)據(jù)");
}
bool b1 = int.TryParse(args[0], out time_a);
//時分秒
if (!Regex.IsMatch(args[1], "^[0-9]{6}$"))
{
throw new Exception("錯誤的時間數(shù)據(jù)");
}
bool b2 = int.TryParse(args[1], out time_b);
if ((!b1) || (!b2))
{
throw new Exception("時間數(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ā)信時間 {0} {1}",
time_a, time_b);
counter = 0;
}
}
else
{
break;
}
}
}
//發(fā)送郵件
Console.WriteLine("正在發(fā)送郵件,請稍候 ...");
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");
}
}
}
附:慶祝國慶節(jié)的Atilia.html內(nèi)容
<html>
<head>
<title>
國慶快樂!
</title>
<style>
body{text-align:center}
</style>
</head>
<body>
<span style="color:red;font-size:250%;font-weight:800">
江有汜 攜 Atilia 恭祝大家 國慶快樂!?。?
</span>
<hr />
<img src="http://upload.wikimedia.org/wikipedia/commons/c/ce/Chinese_flag_%28Beijing%29_-_IMG_1104.jpg"
alt="中華人民共和國國旗" height="400" width="660"/>
<hr>
<b>十一小長假,可要注意好好休息啊~~~</b><br>
<p>
圖片來源:
<a >
維基共享資源:飄揚(yáng)在北京的五星紅旗
</a>
</p>
<p>
程序源碼:
<a >
源碼地址
</a>
</p>
刮開涂層贏千萬大獎:
<span style="background-color:black;color:black">
Atilia 很萌的,乃們不要黑她 :P
</span>
</body>
</html>
發(fā)送后的效果展示:

希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C#創(chuàng)建WCF服務(wù)控制臺應(yīng)用程序詳解
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建WCF服務(wù)控制臺應(yīng)用程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

