程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C# Builder實現POP3信箱的監視

C# Builder實現POP3信箱的監視

編輯:C#基礎知識

  前言:

  Borland的C# Builder是一個基於C#語言的編程工具,C# Builder允許開發者用Java或CORBA工具開發,C# Builder同時也具有從多種數據庫中讀取數據的能力,可以混合和適應不同開發標准的能力。本文向大家介紹怎樣用Borland C# Builder編寫一個信箱監視程序, 程序主要是通過WinSock來進行網絡通信。要實現這個程序,應對POP3協議有一定的了解,下面是對POP3協議的一個粗略的介紹,讀者可以參看RFC 1225更為詳細地了解該協議。

  一、POP3協議簡單介紹

  POP3服務器程序通常在TCP端口110提供服務。當客戶想要使用服務時,它便與服務器建立一個TCP連接。一旦連接建立,POP3服務器就向客戶發送一條歡迎消息。然後客戶開始給服務器發送命令,服務器則給出相應的回答。POP3的命令由一個關鍵詞或者關鍵詞加參數組成。每個命令以回車換行(0xD0xA)作為結束標志。對於所有的命令,POP3服務器都會提供一個回答。服務器的回答由一個狀態標志加一些附加信息組成。目前使用的兩個標志是“+OK”和“-ERR”,分別表示客戶的命令是否合法。所有的回答也是以回車換行結束。與本文討論的話題相關的四個POP3命令是USER、PASS、STAT和QUIT。

  USER命令

  格式USER name

  其中name是用戶在該POP3服務器上的用戶標識。客戶應該在接到服務器的歡迎消息後或者在上一個USER或者PASS失敗之後可以發送此命令。

  PASS命令

  格式PASS string

  其中string為該用戶的密碼。客戶在發送了USER命令並且收到了+OK的回答之後方可發送此命令。如果用戶名和密碼都正確,服務器回答+OK,否則-ERR。

  STAT命令

  格式STAT

  STAT命令來查看郵箱的情況。STAT命令的回應中有兩個數字,分別表示郵件的數量和郵件的大小。

  QUIT命令

  從POP3服務器上退出登錄。

  二、POP3信箱的監視程序分析

  我們准備的做的程序要實現以下功能:

  1.托盤圖標,程序一運行,只顯示一托盤圖標,右鍵點擊托盤圖標可彈出菜單。

  2.獲取郵件數量,根據POP3協議,得到郵件的數量。

  3.讀取和寫注冊表,注冊表中保存服務器、用戶名、密碼等設置。

  4.用戶提示信息,這裡我們做一個與MSN一樣的提示窗口。

  三、程序實現

  下面我們就不妨著手我們的程序。首先,打開Borland C# builder,新建一個項目,菜單 File->C# Applicaion 項目的名稱不妨設為"chkpop3",圖示如下:

  [ 相關貼圖 ]

  設計主窗口,如下圖:

  [ 相關貼圖 ]

  主要包括五個文本框,五個標簽,三個按鈕,一個選擇框和一個timer。

  WinForm設置如下:

  text:收取郵件

  startPosition:CenterScreen

  MaximizeBox:false

  三個按鈕:

  最小化按鈕:最小化窗口,這裡就是隱藏主窗口。

  取郵件按鈕:實現取得POP3信箱中的郵件數量,並用信息窗口提示。

  應用按鈕:保存設置到注冊表中

  timer1的功能:完成在一定時間間隔內取POP3信箱中郵件數量。

  1.托盤的實現:

  從tool palette選擇組件contextMenu(加兩個菜單項,設置和退出)、notifyIcon(text設為:郵箱檢測,圖標ICON選擇一個16x16的小圖標即可,contextMenu設為前面加入的contextMenu1,Visible設為true) 這樣一個完整的托盤程序就設好了。

  2.用戶提示信息的實現:

  新建一窗口WinForm1,把窗體的FormBorderStyle屬性設置為None(無邊框模式),然後把TopMost屬性(總在最上方)屬性設置為True,把ShowInTaskbar屬性(是否在 Windows 任務欄中顯示窗體)設置為False,並在窗體上加上一文字標簽,將窗體的背景設置為你想要的圖片和合適的大小。最後再放上三個Timer控件,其中,timer1控制窗體滾出的動畫,timer2控制窗體停留時間,timer3控制窗體的滾入動畫,將它們的Interval屬性設置為10,如圖:

  [ 相關貼圖 ]

  WinForm1代碼如下:

  

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace pop3
{
/// <summary>
/// Summary description for WinForm1.
/// </summary>
public class WinForm1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Timer timer2;
private System.Windows.Forms.Timer timer3;
private System.Windows.Forms.LinkLabel linkLabel1;
public string str_num;
public WinForm1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WinForm1));
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.timer3 = new System.Windows.Forms.Timer(this.components);
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// timer1
//
this.timer1.Interval = 10;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// timer2
//
this.timer2.Interval = 10;
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// timer3
//
this.timer3.Interval = 10;
this.timer3.Tick += new System.EventHandler(this.timer3_Tick);
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.BackColor = System.Drawing.Color.WhiteSmoke;
this.linkLabel1.Location = new System.Drawing.Point(56, 56);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(79, 17);
this.linkLabel1.TabIndex = 0;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "您有新郵件!";
//
// WinForm1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Info;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(194, 122);
this.Controls.Add(this.linkLabel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "WinForm1";
this.ShowInTaskbar = false;
this.Text = "信息提示";
this.TopMost = true;
this.Load += new System.EventHandler(this.WinForm1_Load);
this.Activated += new System.EventHandler(this.WinForm1_Activated);
this.ResumeLayout(false);
}
#endregion
private void WinForm1_Load(object sender, System.EventArgs e)
{
Screen[] screens = Screen.AllScreens;
Screen screen = screens[0];
this.Location = new Point(screen.WorkingArea.Width - 200, screen.WorkingArea.Height - 30);
this.timer2.Interval = 5000;
}
private void ScrollUp()
{
if(Height < 122)
{
this.Height += 3;
this.Location = new Point(this.Location.X, this.Location.Y - 3);
}
else
{
this.timer1.Enabled = false;
this.timer2.Enabled = true;
}
}
private void ScrollDown()
{
if(Height > 3)
{
this.Height -= 3;
this.Location = new Point(this.Location.X, this.Location.Y + 3);
}
else
{
this.timer3.Enabled = false;
this.Close();
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
ScrollUp();
}
private void timer2_Tick(object sender, System.EventArgs e)
{
timer2.Enabled = false;
timer3.Enabled = true;
}
private void timer3_Tick(object sender, System.EventArgs e)
{
ScrollDown();
}
public void ScrollShow()
{
this.Width = 194;
this.Height = 0;
this.Show();
this.timer1.Enabled = true;
}
private void WinForm1_Activated(object sender, System.EventArgs e)
{
linkLabel1.Text="您有"+str_num+"封新郵件!";
}
}
}

  • 首頁
  • 上一頁
  • 1
  • 2
  • 下一頁
  • 尾頁
  • 共2頁
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved