前言
來自一位爸爸的需求,由于女兒在家會(huì)偷偷玩電腦卧檐,就想讓hojun幫忙做個(gè)電腦開機(jī)就馬上短信通知他的小程序。短信要錢焰宣,郵件免費(fèi)霉囚,當(dāng)然是選擇郵件咯~
當(dāng)時(shí)就在網(wǎng)絡(luò)上收索了一波,其中一種解決方案是使用一個(gè)小軟件加windows的bat批處理命令實(shí)現(xiàn)開機(jī)自動(dòng)發(fā)送郵件的功能匕积。嘗試下載這個(gè)軟件結(jié)果直接被360檢測成了病毒盈罐,直接給清理掉了榜跌。
還有一種實(shí)現(xiàn)是通過windows的服務(wù)來實(shí)現(xiàn)的,略微看了一下盅粪。由于windows服務(wù)還真沒接觸過钓葫,就決定,花一個(gè)晚上的時(shí)間動(dòng)手實(shí)現(xiàn)一下這個(gè)功能票顾。
第一步 開發(fā)環(huán)境介紹
windows電腦
VisualStudio(博主用的2015)
.Net framework4.0
可以在 C:\Windows\Microsoft.NET\Framework\ 目錄看下如下圖:(可以發(fā)現(xiàn)1-4都有)
第二步 開發(fā)windows服務(wù)
打開vs2015點(diǎn)擊新建項(xiàng)目
在Visual C#>Windows>經(jīng)典桌面 下面础浮,這里選擇.NET Framework4(因?yàn)橹澳夸浵掠校覀冞@里選擇有的)奠骄,再選擇Windows服務(wù)豆同。名稱使用默認(rèn)的,位置自己選擇含鳞。點(diǎn)擊確定->
新建后如下圖影锈,我們在這個(gè)頁面 右鍵>添加安裝程序
會(huì)跳到這個(gè)頁面,我們可以在控件上面 右鍵>屬性蝉绷,也可以點(diǎn)擊右側(cè)解決方案資源管理器的屬性欄更改其屬性鸭廷。
這個(gè)給它的Account改為LocalSystem
這里添加描述、名稱熔吗,以及啟動(dòng)方式改為Auto開機(jī)自動(dòng)
安裝配置好后辆床,回到之前頁面,點(diǎn)擊藍(lán)色字體跳轉(zhuǎn)到代碼視圖
默認(rèn)給我們?nèi)齻€(gè)方法磁滚,一個(gè)構(gòu)造方法佛吓、開始以及停止
開始coding前,我們還要設(shè)置下我們的郵箱
第三步 準(zhǔn)備郵箱授權(quán)碼
這里以QQ郵箱為例垂攘,點(diǎn)擊設(shè)置>賬戶>
點(diǎn)擊開啟POP3/SMTP服務(wù)维雇,會(huì)叫你發(fā)送短信驗(yàn)證
驗(yàn)證完后得到授權(quán)碼
第四步 coding生成
代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace AutoSendEmail
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer createOrderTimer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
createOrderTimer = new System.Timers.Timer();
createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(Page_Load);
createOrderTimer.Interval = 20000;
createOrderTimer.Enabled = true;
createOrderTimer.AutoReset = true;
createOrderTimer.Start();
}
protected override void OnStop()
{
}
protected void Page_Load(object sender, System.Timers.ElapsedEventArgs args)
{
Ping ping = new Ping();
PingReply pr = ping.Send("baidu.com");
if (pr.Status == IPStatus.Success)
{
//實(shí)例化一個(gè)發(fā)送郵件類。
MailMessage mailMessage = new MailMessage();
//發(fā)件人郵箱地址晒他,方法重載不同吱型,可以根據(jù)需求自行選擇。
mailMessage.From = new MailAddress("1234567890@qq.com");
//收件人郵箱地址陨仅。
mailMessage.To.Add(new MailAddress("0987654321@qq.com"));
//郵件標(biāo)題津滞。
mailMessage.Subject = "電腦狀態(tài)";
//郵件內(nèi)容。
mailMessage.Body = "開機(jī)";
//實(shí)例化一個(gè)SmtpClient類灼伤。
SmtpClient client = new SmtpClient();
//在這里我使用的是qq郵箱触徐,所以是smtp.qq.com,如果你使用的是126郵箱狐赡,那么就是smtp.126.com撞鹉。
client.Host = "smtp.qq.com";
//使用安全加密連接。
client.EnableSsl = true;
//不和請求一塊發(fā)送。
client.UseDefaultCredentials = false;
//驗(yàn)證發(fā)件人身份(發(fā)件人的郵箱鸟雏,郵箱里的生成授權(quán)碼);
client.Credentials = new NetworkCredential("1234567890@qq.com", "xxxxxxxxxxxx");
//發(fā)送
client.Send(mailMessage);
//Context.Response.Write("發(fā)送成功");
StopWindowsService("AutoSendEmail");
}
}
/// <summary>
/// 開啟服務(wù)
/// </summary>
/// <param name="windowsServiceName">服務(wù)名稱</param>
static void StartWindowsService(string windowsServiceName)
{
using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController(windowsServiceName))
{
if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
Console.WriteLine("服務(wù)啟動(dòng)......");
control.Start();
Console.WriteLine("服務(wù)已經(jīng)啟動(dòng)......");
}
else if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
Console.WriteLine("服務(wù)已經(jīng)啟動(dòng)......");
}
}
}
/// <summary>
/// 停止服務(wù)
/// </summary>
/// <param name="windowsServiceName">服務(wù)名稱</param>
static void StopWindowsService(string windowsServiceName)
{
using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController(windowsServiceName))
{
if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
Console.WriteLine("服務(wù)停止......");
control.Stop();
Console.WriteLine("服務(wù)已經(jīng)停止......");
}
else if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
Console.WriteLine("服務(wù)已經(jīng)停止......");
}
}
}
}
}
代碼完成后 右鍵>生成
在輸出窗口拷貝生成目錄
在電腦中打開如下:
在這個(gè)目錄下新建兩個(gè)txt文檔享郊,內(nèi)容如下:
安裝.txt
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
InstallUtil.exe
InstallUtil E:\C#Workspace\lab\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe
pause
卸載.txt
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
InstallUtil.exe
InstallUtil.exe /u E:\C#Workspace\lab\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe
pause
然后將其擴(kuò)展名改為bat
右鍵>以管理員身份運(yùn)行
出現(xiàn)如下提示表示安裝成功
回到桌面,開始右鍵>打開控制面板
搜索服務(wù)孝鹊,并打開
按名稱排序炊琉,找到我們的AutoSendEmail服務(wù)
右鍵啟動(dòng)
手機(jī)上收到郵件如下
卸載的話也是右鍵>以管理員身份運(yùn)行
致謝
雜記2:VS2013創(chuàng)建Windows服務(wù)實(shí)現(xiàn)自動(dòng)發(fā)送郵件,作者:wuxiaochao
C#實(shí)現(xiàn)發(fā)送給QQ郵件,作者:謝尊旭
Windows服務(wù)實(shí)現(xiàn)自動(dòng)發(fā)送郵件通知,云棲社區(qū),來源互聯(lián)網(wǎng)