C#Builder实现POP3信箱的监视

C#Builder实现POP3信箱的监视


Author:徐长友&;nbspFrom:Internet前言:



   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 "封新邮件!";

      }

     

      }

      }  (2005-5-07:07:54)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [C#] 的这些文章也感兴趣:

用C#实现一种超酷的任务栏滑出式提醒框
DataGrid - 导出Excel文件(WinForm)
VisualC#事件与接口编程实例
C# 对象深拷贝、浅铐贝、直接拷贝
C#基类应用--字符串处理类
windows应用程序版的google搜索引擎源码
让窗体飘动起来--C#中Timer组件用法
C#实现动态分配IP和释放IP
实例介绍C#GUI开发(1)
论C#变得越来越臃肿是不可避免的