CodeForFun--编写自动登录Email的程序

本文介绍了一种使用C#编写的简单应用程序,通过点击按钮即可快速访问预设的网站,包括博客园、NBA赛事和Gmail邮箱。该应用利用了自动化测试原理,实现了IE浏览器的自动化操作,如导航至指定网址、自动填写表单等。

每天早上一开机就登录博客园已经成了习惯, 总要先打开浏览器, 然后输入www.cnblogs.com,然后等待... ...
要是某天有火箭队的比赛, 还要去关注一下赛事情况: 先打开浏览器, 然后输入www.sohu.com,点NBA,然后... ...
哈哈, 还有一件少不了的事情,就是要登录电子油箱查看一下是否有新邮件...

这么多操作真是麻烦! 能不能通过点击一个按纽来简化这些操作呢?

前两天从同事(ZnS04)那里得到启示, 根据自动化测试的原理, 完全可以...

好了,下面就开始编写吧 ... ...

1).  新建一个 window application 工程.
2). 在窗体上添加3个Button,设置好属性, 如图:

play.JPG
3). 给工程添加2个引用 Microsoft.mshtml 和SHDocVw.dll
4). 编写代码

 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Data;
 5None.gifusing System.Drawing;
 6None.gifusing System.Text;
 7None.gifusing System.Windows.Forms;
 8None.gifusing System.Threading;
 9None.gif//添加引用
10None.gifusing SHDocVw;
11None.gifusing mshtml;
12None.gif
13None.gifnamespace WebExplorer
14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
15InBlock.gif    public partial class Form1 : Form
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif        public Form1()
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            InitializeComponent();
20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
23InBlock.gif        /// Redirect to the URL page.
24InBlock.gif        /// </summary>
25ExpandedSubBlockEnd.gif        /// <param name="URL">Your wanted URL.</param>

26InBlock.gif        public void GotoURL(string URL)
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            //实例化一个IE模型
29InBlock.gif            SHDocVw.InternetExplorer IE = new InternetExplorer();            
30InBlock.gif            IE.Visible = true;
31InBlock.gif            object nullArg = null;
32InBlock.gif            //引导到URL
33InBlock.gif            IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, ref nullArg);
34ExpandedSubBlockEnd.gif        }

35InBlock.gif
36InBlock.gif        private void gotocnBlogs_Click(object sender, EventArgs e)
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            this.GotoURL("www.cnblogs.com");
39ExpandedSubBlockEnd.gif        }

40InBlock.gif
41InBlock.gif        private void gotoNBA_Click(object sender, EventArgs e)
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43InBlock.gif            this.GotoURL("sports.sohu.com/nba"); 
44ExpandedSubBlockEnd.gif        }

45InBlock.gif
46InBlock.gif        private void gotoGmail_Click(object sender, EventArgs e)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            try
49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
50InBlock.gif                
                        this.GotoURL("http://gmail.google.com");
55InBlock.gif
56InBlock.gif                Thread.Sleep(3000);
57InBlock.gif                //得到IE的文档对象模型
58InBlock.gif                mshtml.IHTMLDocument2 DOM = (mshtml.IHTMLDocument2)IE.Document;
59InBlock.gif                //声明用户名
60InBlock.gif                mshtml.IHTMLInputTextElement txtUserName = (mshtml.IHTMLInputTextElement)DOM.all.item("Email"null);
61InBlock.gif                txtUserName.value = "YOUE USERNAME";
62InBlock.gif                //声明密码
63InBlock.gif                mshtml.IHTMLInputTextElement txtPwd = (mshtml.IHTMLInputTextElement)DOM.all.item("Passwd"null);
64InBlock.gif                txtPwd.value = "PASSWORD";
65InBlock.gif                //声明登录
66InBlock.gif                mshtml.HTMLInputElement btnLogin = (mshtml.HTMLInputElement)DOM.all.item("null"0);
67InBlock.gif                Thread.Sleep(1000);
68InBlock.gif                btnLogin.click();
69ExpandedSubBlockEnd.gif            }

70InBlock.gif            catch (Exception ex)
71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
72InBlock.gif
73ExpandedSubBlockEnd.gif            }

74ExpandedSubBlockEnd.gif        }

75ExpandedSubBlockEnd.gif    }

76ExpandedBlockEnd.gif}


5. 运行工程,点击上面的Button,OK!

需要说明两点:
1. 关于两个引用
   Microsoft.mshtml:这个引用可以从add reference->.NET中得到。
   SHDocVw.dll:这个引用在windows/system32目录下。
2.对上述dll进行引用后,即可实例化IE模型,通过构建页面元素进行操作。

转载于:https://www.cnblogs.com/Ring1981/archive/2006/11/09/450744.html

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值