要做一个小工具,定时在订餐时间截止之后,把第二天的各餐订餐人数发信息到食堂工作人员手机上,由于条件有限,没买短信猫,最好还是免费的。
思前想后,做了三个方法:
一、直接用按键精灵,模拟打开统计界面复制统计信息,然后登录139邮箱发送信息。测试是通过的,但是按键精灵过于傻瓜化,有时候会被别的窗口挡住,造成发送不成功,排除掉;
二、用飞信机器人,定时自动在命令行下输代码发信息,也可以实现,不过飞信的短信不稳定,时好时坏,又排除掉;
三、突然想到139邮箱的邮件到达通知。无限量,正好合用。于是做了个窗体,定时自动打开,在webBrowser里自动登陆139邮箱具体如下:贴出部分代码//本段可实现自动登陆139邮箱
HtmlElement btnSubmit = webBrowser1.Document.All["loginBtn"];
HtmlElement tbUserid = webBrowser1.Document.All["txtUserName"];
HtmlElement tbPasswd = webBrowser1.Document.All["txtPassword"];
if (tbUserid == null || tbPasswd == null || btnSubmit == null)
{
return;
}
tbUserid.SetAttribute("value", "134xxxxxxxx"); //手机号
tbPasswd.SetAttribute("value", "xxxxxxxxxxx"); //邮箱密码
btnSubmit.InvokeMember("click");进入之后试了很久,自动点击发信按钮失败,于是曲线救国,换成用坐标单击,同时在load事件里从数据库里取出统计数据,放入剪切板里//以下代码实现自动点击“写信” int x = 57; //横坐标
int y = 108; //纵坐标 //此处要注意,这里的坐标不是你在IE打开网页的按钮坐标,而是在webBrowser界面里的按钮坐标
IntPtr handle = webBrowser1.Handle;
StringBuilder className = new StringBuilder(57);
while (className.ToString() != "Internet Explorer_Server")
{
handle = GetWindow(handle, 5); // Get a handle to the child window
GetClassName(handle, className, className.Capacity);
}
IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
const uint downCode = 0x201; // Left click down code
const uint upCode = 0x202; // Left click up code
SendMessage(handle, downCode, wParam, lParam); // Mouse button down
SendMessage(handle, upCode, wParam, lParam); // Mouse button up
System.Diagnostics.Process.Start(@"c:\自动发信息.exe"); //用于点击写信后,调用按钮精灵,填写收信人邮箱、主题,及把剪切板上的统计信息复制到邮件正文 ,再用类似本段代码点击发送。至此,定时自动发信息完成,最后把打开的程序都关闭,我是做了个批处理,在最后调用,把程序关闭。
例:taskkill /f /im 自动发信息.exe
思前想后,做了三个方法:
一、直接用按键精灵,模拟打开统计界面复制统计信息,然后登录139邮箱发送信息。测试是通过的,但是按键精灵过于傻瓜化,有时候会被别的窗口挡住,造成发送不成功,排除掉;
二、用飞信机器人,定时自动在命令行下输代码发信息,也可以实现,不过飞信的短信不稳定,时好时坏,又排除掉;
三、突然想到139邮箱的邮件到达通知。无限量,正好合用。于是做了个窗体,定时自动打开,在webBrowser里自动登陆139邮箱具体如下:贴出部分代码//本段可实现自动登陆139邮箱
HtmlElement btnSubmit = webBrowser1.Document.All["loginBtn"];
HtmlElement tbUserid = webBrowser1.Document.All["txtUserName"];
HtmlElement tbPasswd = webBrowser1.Document.All["txtPassword"];
if (tbUserid == null || tbPasswd == null || btnSubmit == null)
{
return;
}
tbUserid.SetAttribute("value", "134xxxxxxxx"); //手机号
tbPasswd.SetAttribute("value", "xxxxxxxxxxx"); //邮箱密码
btnSubmit.InvokeMember("click");进入之后试了很久,自动点击发信按钮失败,于是曲线救国,换成用坐标单击,同时在load事件里从数据库里取出统计数据,放入剪切板里//以下代码实现自动点击“写信” int x = 57; //横坐标
int y = 108; //纵坐标 //此处要注意,这里的坐标不是你在IE打开网页的按钮坐标,而是在webBrowser界面里的按钮坐标
IntPtr handle = webBrowser1.Handle;
StringBuilder className = new StringBuilder(57);
while (className.ToString() != "Internet Explorer_Server")
{
handle = GetWindow(handle, 5); // Get a handle to the child window
GetClassName(handle, className, className.Capacity);
}
IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
const uint downCode = 0x201; // Left click down code
const uint upCode = 0x202; // Left click up code
SendMessage(handle, downCode, wParam, lParam); // Mouse button down
SendMessage(handle, upCode, wParam, lParam); // Mouse button up
System.Diagnostics.Process.Start(@"c:\自动发信息.exe"); //用于点击写信后,调用按钮精灵,填写收信人邮箱、主题,及把剪切板上的统计信息复制到邮件正文 ,再用类似本段代码点击发送。至此,定时自动发信息完成,最后把打开的程序都关闭,我是做了个批处理,在最后调用,把程序关闭。
例:taskkill /f /im 自动发信息.exe
/// 发送电子邮件
/// </summary>
/// <param name="toAddress"></param>
/// <param name="subject"></param>
/// <param name="bodyString"></param>
public static void SendMail( string toAddress, string subject, string bodyString )
{
try
{
var client = new SmtpClient( "smtp.139.com" );
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential( "邮箱账号", "密码" );
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress addressFrom = new MailAddress( "你的邮箱地址", "你的名称" );
MailAddress addressTo = new MailAddress( toAddress, toAddress );
System.Net.Mail.MailMessage message = new MailMessage( addressFrom, addressTo );
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = bodyString;
message.Subject = subject;
message.IsBodyHtml = true;
client.Send( message );
}
catch ( Exception e ) { }
}