CodeForFun–編寫自動登入Email的程式

來源:互聯網
上載者:User

每天早上一開機就登入部落格園已經成了習慣, 總要先開啟瀏覽器, 然後輸入www.cnblogs.com,然後等待... ...
要是某天有火箭隊的比賽, 還要去關注一下賽事情況: 先開啟瀏覽器, 然後輸入www.sohu.com,點NBA,然後... ...
哈哈, 還有一件少不了的事情,就是要登入電子油箱查看一下是否有新郵件...

這麼多操作真是麻煩! 能不能通過點擊一個按紐來簡化這些操作呢?

前兩天從同事(ZnS04)那裡得到啟示, 根據自動化測試的原理, 完全可以...

好了,下面就開始編寫吧 ... ...

1).  建立一個 window application 工程.
2). 在表單上添加3個Button,設定好屬性, :


3). 給工程添加2個引用 Microsoft.mshtml 和SHDocVw.dll
4). 編寫代碼

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8using System.Threading;
 9//添加引用
10using SHDocVw;
11using mshtml;
12
13namespace WebExplorer
14{
15    public partial class Form1 : Form
16    {
17        public Form1()
18        {
19            InitializeComponent();
20        }
21
22        /**//// <summary>
23        /// Redirect to the URL page.
24        /// </summary>
25        /// <param name="URL">Your wanted URL.</param>
26        public void GotoURL(string URL)
27        {
28            //執行個體化一個IE模型
29            SHDocVw.InternetExplorer IE = new InternetExplorer();            
30            IE.Visible = true;
31            object nullArg = null;
32            //引導到URL
33            IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, ref nullArg);
34        }
35
36        private void gotocnBlogs_Click(object sender, EventArgs e)
37        {
38            this.GotoURL("www.cnblogs.com");
39        }
40
41        private void gotoNBA_Click(object sender, EventArgs e)
42        {
43            this.GotoURL("sports.sohu.com/nba"); 
44        }
45
46        private void gotoGmail_Click(object sender, EventArgs e)
47        {
48            try
49            {
50                
                        this.GotoURL("http://gmail.google.com");
55
56                Thread.Sleep(3000);
57                //得到IE的文件物件模型
58                mshtml.IHTMLDocument2 DOM = (mshtml.IHTMLDocument2)IE.Document;
59                //聲明使用者名稱
60                mshtml.IHTMLInputTextElement txtUserName = (mshtml.IHTMLInputTextElement)DOM.all.item("Email", null);
61                txtUserName.value = "YOUE USERNAME";
62                //聲明密碼
63                mshtml.IHTMLInputTextElement txtPwd = (mshtml.IHTMLInputTextElement)DOM.all.item("Passwd", null);
64                txtPwd.value = "PASSWORD";
65                //聲明登入
66                mshtml.HTMLInputElement btnLogin = (mshtml.HTMLInputElement)DOM.all.item("null", 0);
67                Thread.Sleep(1000);
68                btnLogin.click();
69            }
70            catch (Exception ex)
71            {
72
73            }
74        }
75    }
76}

5. 運行工程,點擊上面的Button,OK!

需要說明兩點:
1. 關於兩個引用
   Microsoft.mshtml:這個引用可以從add reference->.NET中得到。
   SHDocVw.dll:這個引用在windows/system32目錄下。
2.對上述dll進行引用後,即可執行個體化IE模型,通過構建頁面元素進行操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.