电脑开机自动发送邮件通知
前言
来自一位爸爸的需求,由于女儿在家会偷偷玩电脑,就想让hojun帮忙做个电脑开机就马上短信通知他的小程序。短信要钱,邮件免费,当然是选择邮件咯~
当时就在网络上收索了一波,其中一种解决方案是使用一个小软件加windows的bat批处理命令实现开机自动发送邮件的功能。尝试下载这个软件结果直接被360检测成了病毒,直接给清理掉了。
还有一种实现是通过windows的服务来实现的,略微看了一下。由于windows服务还真没接触过,就决定,花一个晚上的时间动手实现一下这个功能。
第一步 开发环境介绍
windows电脑
VisualStudio(博主用的2015)
.Net framework4.0
可以在 C:\Windows\Microsoft.NET\Framework\ 目录看下如下图:(可以发现1-4都有)

第二步 开发windows服务
打开vs2015点击新建项目

在Visual C#>Windows>经典桌面 下面,这里选择.NET Framework4(因为之前目录下有,我们这里选择有的),再选择Windows服务。名称使用默认的,位置自己选择。点击确定->

新建后如下图,我们在这个页面 右键>添加安装程序

会跳到这个页面,我们可以在控件上面 右键>属性,也可以点击右侧解决方案资源管理器的属性栏更改其属性。

这个给它的Account改为LocalSystem

这里添加描述、名称,以及启动方式改为Auto开机自动

安装配置好后,回到之前页面,点击蓝色字体跳转到代码视图

默认给我们三个方法,一个构造方法、开始以及停止

开始coding前,我们还要设置下我们的邮箱
第三步 准备邮箱授权码
这里以QQ邮箱为例,点击设置>账户>

点击开启POP3/SMTP服务,会叫你发送短信验证

验证完后得到授权码

第四步 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)
{
//实例化一个发送邮件类。
MailMessage mailMessage = new MailMessage();
//发件人邮箱地址,方法重载不同,可以根据需求自行选择。
mailMessage.From = new MailAddress("1234567890@qq.com");
//收件人邮箱地址。
mailMessage.To.Add(new MailAddress("0987654321@qq.com"));
//邮件标题。
mailMessage.Subject = "电脑状态";
//邮件内容。
mailMessage.Body = "开机";
//实例化一个SmtpClient类。
SmtpClient client = new SmtpClient();
//在这里我使用的是qq邮箱,所以是smtp.qq.com,如果你使用的是126邮箱,那么就是smtp.126.com。
client.Host = "smtp.qq.com";
//使用安全加密连接。
client.EnableSsl = true;
//不和请求一块发送。
client.UseDefaultCredentials = false;
//验证发件人身份(发件人的邮箱,邮箱里的生成授权码);
client.Credentials = new NetworkCredential("1234567890@qq.com", "xxxxxxxxxxxx");
//发送
client.Send(mailMessage);
//Context.Response.Write("发送成功");
StopWindowsService("AutoSendEmail");
}
}
/// <summary>
/// 开启服务
/// </summary>
/// <param name="windowsServiceName">服务名称</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("服务启动......");
control.Start();
Console.WriteLine("服务已经启动......");
}
else if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
Console.WriteLine("服务已经启动......");
}
}
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="windowsServiceName">服务名称</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("服务停止......");
control.Stop();
Console.WriteLine("服务已经停止......");
}
else if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
Console.WriteLine("服务已经停止......");
}
}
}
}
}
代码完成后 右键>生成

在输出窗口拷贝生成目录

在电脑中打开如下:

在这个目录下新建两个txt文档,内容如下:
安装.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
然后将其扩展名改为bat

右键>以管理员身份运行

出现如下提示表示安装成功

回到桌面,开始右键>打开控制面板

搜索服务,并打开

按名称排序,找到我们的AutoSendEmail服务

右键启动

手机上收到邮件如下

卸载的话也是右键>以管理员身份运行

致谢
杂记2:VS2013创建Windows服务实现自动发送邮件,作者:wuxiaochao
C#实现发送给QQ邮件,作者:谢尊旭
Windows服务实现自动发送邮件通知,云栖社区,来源互联网