当前位置:
首页 > 编程开发 > Objective-C编程 >
-
c# Builder 实现POP3信箱的监视
制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
前言:
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+"封新邮件!";
}
}
}
具体的实现,可以参考一下网上的一篇文章:《用VC#编写仿MSN Messager的滚动提示窗口》,已经记不清出处了,可以通过这个网址http://yousoft.hi.com.cn/article_view.asp?id=6595浏览。
3.获取邮件数量的实现,代码见下:
string str_server;
if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!=""))
{
TcpClient tcpc = new TcpClient(tB_server.Text,110);
Byte[] outbytes;
string input;
NetworkStream ns = null;
try{
ns = tcpc.GetStream();
StreamReader sr = new StreamReader(ns);
tB_status.Text=sr.ReadLine();
input = "USER " + tB_user.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "PASS " + tB_pwd.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "STAT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
str_server=sr.ReadLine();
str_num="";
if (str_server.StartsWith("+OK")){
str_num=str_server.Split(' ')[1];
} else str_num="";
tB_status.Text=tB_status.Text+"\r\n"+"A:"+str_server;
input = "QUIT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+"B:"+sr.ReadLine();
}
catch(InvalidOperationException ioe){
tB_status.Text="Could not connect to mail server";
}
if (str_num!="") {
WinForm1 form = new WinForm1();
form.str_num=str_num;
form.ScrollShow();
}
程序与邮件服务器建立一个TCP连接,然后发送POP3的一些命令,取到服务器传回的信息,得到邮件的数量。
STAT命令的回应中有两个数字,分别表示邮件的数量和邮件的大小。一般格式是这样的“+OK 数量 大小”,所以只要根据服务器返回的信息,提取+OK,然后再按空格分隔得到数量。代码见上。
4.注册表的读写:
我们知道,要实现程序在开机后自己运行,可以通过写注册表HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run实现。
另外,为了方便用户,我们把邮件服务器,用户,密码等信息存入注册表,不用每次都再输一次。
保存信息代码如下:
RegistryKey rk;
Registry.CurrentUser.CreateSubKey("Software\\yousoft");
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",true);
rk.Setvalue("server",tB_server.Text);
rk.Setvalue("user",tB_user.Text);
rk.Setvalue("pwd",tB_pwd.Text);
rk.Setvalue("interval",tB_mins.Text);
if (cB_autorun.Checked) {
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Setvalue("chkmail",Application.ExecutablePath);
} else
{
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Deletevalue("chkmail");
}
rk.Close();
启动程序时读入信息:
private void WinForm_Load(object sender, System.EventArgs e)
{
RegistryKey rk;
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",false);
object srv=rk.Getvalue("server");
if (srv!=null) tB_server.Text=srv.ToString();
object usr=rk.Getvalue("user");
if (usr!=null) tB_user.Text=usr.ToString();
object pwd=rk.Getvalue("pwd");
if (pwd!=null) tB_pwd.Text=pwd.ToString();
object mins=rk.Getvalue("interval");
if (mins!=null) tB_mins.Text=mins.ToString();
rk.Close();
}
WinForm的完整代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Net;
using Microsoft.Win32;
namespace pop3
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tB_server;
private System.Windows.Forms.TextBox tB_user;
private System.Windows.Forms.TextBox tB_pwd;
private System.Windows.Forms.TextBox tB_status;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private bool f_open=true;
private string str_num="";
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox tB_mins;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.CheckBox cB_autorun;
public WinForm()
{
//
// 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(WinForm));
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.tB_server = new System.Windows.Forms.TextBox();
this.tB_user = new System.Windows.Forms.TextBox();
this.tB_pwd = new System.Windows.Forms.TextBox();
this.tB_status = new System.Windows.Forms.TextBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.button2 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.tB_mins = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.cB_autorun = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "取邮件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 1;
this.label1.Text = "邮件服务器:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(48, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 2;
this.label2.Text = "用户名:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(58, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 3;
this.label3.Text = "密码:";
//
// tB_server
//
this.tB_server.Location = new System.Drawing.Point(112, 24);
this.tB_server.Name = "tB_server";
this.tB_server.Size = new System.Drawing.Size(176, 21);
this.tB_server.TabIndex = 4;
this.tB_server.Text = "";
this.tB_server.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// tB_user
//
this.tB_user.Location = new System.Drawing.Point(112, 60);
this.tB_user.Name = "tB_user";
this.tB_user.Size = new System.Drawing.Size(176, 21);
this.tB_user.TabIndex = 5;
this.tB_user.Text = "";
this.tB_user.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// tB_pwd
//
this.tB_pwd.Location = new System.Drawing.Point(112, 91);
this.tB_pwd.Name = "tB_pwd";
this.tB_pwd.PasswordChar = '*';
this.tB_pwd.Size = new System.Drawing.Size(176, 21);
this.tB_pwd.TabIndex = 6;
this.tB_pwd.Text = "";
this.tB_pwd.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// tB_status
//
this.tB_status.Location = new System.Drawing.Point(16, 208);
this.tB_status.Multiline = true;
this.tB_status.Name = "tB_status";
this.tB_status.Size = new System.Drawing.Size(328, 80);
this.tB_status.TabIndex = 7;
this.tB_status.Text = "";
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "邮件检测";
this.notifyIcon1.Visible = true;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "设置&S";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "-";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "退出&Q";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(48, 176);
this.button2.Name = "button2";
this.button2.TabIndex = 8;
this.button2.Text = "最小化";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// timer1
//
this.timer1.Interval = 5000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// tB_mins
//
this.tB_mins.Location = new System.Drawing.Point(112, 124);
this.tB_mins.Name = "tB_mins";
this.tB_mins.Size = new System.Drawing.Size(144, 21);
this.tB_mins.TabIndex = 10;
this.tB_mins.Text = "20000";
this.tB_mins.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// label4
//
this.label4.Location = new System.Drawing.Point(32, 128);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 9;
this.label4.Text = "时间间隔:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(264, 129);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 16);
this.label5.TabIndex = 11;
this.label5.Text = "毫秒";
//
// button3
//
this.button3.Location = new System.Drawing.Point(264, 176);
this.button3.Name = "button3";
this.button3.TabIndex = 12;
this.button3.Text = "应用";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// cB_autorun
//
this.cB_autorun.Location = new System.Drawing.Point(112, 152);
this.cB_autorun.Name = "cB_autorun";
this.cB_autorun.Size = new System.Drawing.Size(160, 24);
this.cB_autorun.TabIndex = 13;
this.cB_autorun.Text = "启动时自动执行";
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 301);
this.Controls.Add(this.cB_autorun);
this.Controls.Add(this.button3);
this.Controls.Add(this.label5);
this.Controls.Add(this.tB_mins);
this.Controls.Add(this.label4);
this.Controls.Add(this.button2);
this.Controls.Add(this.tB_status);
this.Controls.Add(this.tB_pwd);
this.Controls.Add(this.tB_user);
this.Controls.Add(this.tB_server);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "WinForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "收取邮件";
this.Load += new System.EventHandler(this.WinForm_Load);
this.Activated += new System.EventHandler(this.WinForm_Activated);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
string str_server;
if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!=""))
{
TcpClient tcpc = new TcpClient(tB_server.Text,110);
Byte[] outbytes;
string input;
NetworkStream ns = null;
try{
ns = tcpc.GetStream();
StreamReader sr = new StreamReader(ns);
tB_status.Text=sr.ReadLine();
input = "USER " + tB_user.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "PASS " + tB_pwd.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "STAT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
str_server=sr.ReadLine();
str_num="";
if (str_server.StartsWith("+OK")){
str_num=str_server.Split(' ')[1];
} else str_num="";
tB_status.Text=tB_status.Text+"\r\n"+str_server;
input = "QUIT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
}
catch(InvalidOperationException ioe){
tB_status.Text="Could not connect to mail server";
}
if (str_num!="") {
WinForm1 form = new WinForm1();
form.str_num=str_num;
form.ScrollShow();
}
}
}
private void WinForm_Activated(object sender, System.EventArgs e)
{
if (f_open) {
this.Hide();
f_open=false;
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Show();
this.Focus();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Hide();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
button1_Click(sender,e);
}
private void tB_pwd_TextChanged(object sender, System.EventArgs e)
{
timer1.Enabled=false;
if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!=""))
{
timer1.Interval=System.Convert.ToInt32(tB_mins.Text);
timer1.Enabled=true;
} else
{
timer1.Enabled=false;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
RegistryKey rk;
Registry.CurrentUser.CreateSubKey("Software\\yousoft");
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",true);
rk.Setvalue("server",tB_server.Text);
rk.Setvalue("user",tB_user.Text);
rk.Setvalue("pwd",tB_pwd.Text);
rk.Setvalue("interval",tB_mins.Text);
if (cB_autorun.Checked) {
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Setvalue("chkmail",Application.ExecutablePath);
} else
{
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Deletevalue("chkmail");
}
rk.Close();
}
private void WinForm_Load(object sender, System.EventArgs e)
{
RegistryKey rk;
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",false);
object srv=rk.Getvalue("server");
if (srv!=null) tB_server.Text=srv.ToString();
object usr=rk.Getvalue("user");
if (usr!=null) tB_user.Text=usr.ToString();
object pwd=rk.Getvalue("pwd");
if (pwd!=null) tB_pwd.Text=pwd.ToString();
object mins=rk.Getvalue("interval");
if (mins!=null) tB_mins.Text=mins.ToString();
rk.Close();
}
}
}
按F9编译程序并运行。如图:
[ 相关贴图 ]
[ 相关贴图 ]
[ 相关贴图 ]
四.小结:
本文向读者介绍了如何运用Borland C# builder实现POP3信箱的监视,其中大量运用了Windows Forms编程和.Net框架下的网络编程的原理。通过本文,希望能使读者对.Net中的Borland C# builder有一个感性的认识,同时对Windows Forms编程和其中的网络编程也有个大致的了解。如果有兴趣,可以到悠游在线(网址:http://yousoft.hi.com.cn)下载全部的源代码或与本人交流。
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) 这样一个完整的托盘程序就设好了。
前言:
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+"封新邮件!";
}
}
}
具体的实现,可以参考一下网上的一篇文章:《用VC#编写仿MSN Messager的滚动提示窗口》,已经记不清出处了,可以通过这个网址http://yousoft.hi.com.cn/article_view.asp?id=6595浏览。
3.获取邮件数量的实现,代码见下:
string str_server;
if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!=""))
{
TcpClient tcpc = new TcpClient(tB_server.Text,110);
Byte[] outbytes;
string input;
NetworkStream ns = null;
try{
ns = tcpc.GetStream();
StreamReader sr = new StreamReader(ns);
tB_status.Text=sr.ReadLine();
input = "USER " + tB_user.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "PASS " + tB_pwd.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "STAT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
str_server=sr.ReadLine();
str_num="";
if (str_server.StartsWith("+OK")){
str_num=str_server.Split(' ')[1];
} else str_num="";
tB_status.Text=tB_status.Text+"\r\n"+"A:"+str_server;
input = "QUIT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+"B:"+sr.ReadLine();
}
catch(InvalidOperationException ioe){
tB_status.Text="Could not connect to mail server";
}
if (str_num!="") {
WinForm1 form = new WinForm1();
form.str_num=str_num;
form.ScrollShow();
}
程序与邮件服务器建立一个TCP连接,然后发送POP3的一些命令,取到服务器传回的信息,得到邮件的数量。
STAT命令的回应中有两个数字,分别表示邮件的数量和邮件的大小。一般格式是这样的“+OK 数量 大小”,所以只要根据服务器返回的信息,提取+OK,然后再按空格分隔得到数量。代码见上。
4.注册表的读写:
我们知道,要实现程序在开机后自己运行,可以通过写注册表HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run实现。
另外,为了方便用户,我们把邮件服务器,用户,密码等信息存入注册表,不用每次都再输一次。
保存信息代码如下:
RegistryKey rk;
Registry.CurrentUser.CreateSubKey("Software\\yousoft");
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",true);
rk.Setvalue("server",tB_server.Text);
rk.Setvalue("user",tB_user.Text);
rk.Setvalue("pwd",tB_pwd.Text);
rk.Setvalue("interval",tB_mins.Text);
if (cB_autorun.Checked) {
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Setvalue("chkmail",Application.ExecutablePath);
} else
{
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Deletevalue("chkmail");
}
rk.Close();
启动程序时读入信息:
private void WinForm_Load(object sender, System.EventArgs e)
{
RegistryKey rk;
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",false);
object srv=rk.Getvalue("server");
if (srv!=null) tB_server.Text=srv.ToString();
object usr=rk.Getvalue("user");
if (usr!=null) tB_user.Text=usr.ToString();
object pwd=rk.Getvalue("pwd");
if (pwd!=null) tB_pwd.Text=pwd.ToString();
object mins=rk.Getvalue("interval");
if (mins!=null) tB_mins.Text=mins.ToString();
rk.Close();
}
WinForm的完整代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Net;
using Microsoft.Win32;
namespace pop3
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tB_server;
private System.Windows.Forms.TextBox tB_user;
private System.Windows.Forms.TextBox tB_pwd;
private System.Windows.Forms.TextBox tB_status;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private bool f_open=true;
private string str_num="";
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox tB_mins;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.CheckBox cB_autorun;
public WinForm()
{
//
// 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(WinForm));
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.tB_server = new System.Windows.Forms.TextBox();
this.tB_user = new System.Windows.Forms.TextBox();
this.tB_pwd = new System.Windows.Forms.TextBox();
this.tB_status = new System.Windows.Forms.TextBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.button2 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.tB_mins = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.cB_autorun = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "取邮件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 1;
this.label1.Text = "邮件服务器:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(48, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 2;
this.label2.Text = "用户名:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(58, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 3;
this.label3.Text = "密码:";
//
// tB_server
//
this.tB_server.Location = new System.Drawing.Point(112, 24);
this.tB_server.Name = "tB_server";
this.tB_server.Size = new System.Drawing.Size(176, 21);
this.tB_server.TabIndex = 4;
this.tB_server.Text = "";
this.tB_server.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// tB_user
//
this.tB_user.Location = new System.Drawing.Point(112, 60);
this.tB_user.Name = "tB_user";
this.tB_user.Size = new System.Drawing.Size(176, 21);
this.tB_user.TabIndex = 5;
this.tB_user.Text = "";
this.tB_user.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// tB_pwd
//
this.tB_pwd.Location = new System.Drawing.Point(112, 91);
this.tB_pwd.Name = "tB_pwd";
this.tB_pwd.PasswordChar = '*';
this.tB_pwd.Size = new System.Drawing.Size(176, 21);
this.tB_pwd.TabIndex = 6;
this.tB_pwd.Text = "";
this.tB_pwd.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// tB_status
//
this.tB_status.Location = new System.Drawing.Point(16, 208);
this.tB_status.Multiline = true;
this.tB_status.Name = "tB_status";
this.tB_status.Size = new System.Drawing.Size(328, 80);
this.tB_status.TabIndex = 7;
this.tB_status.Text = "";
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "邮件检测";
this.notifyIcon1.Visible = true;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "设置&S";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "-";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "退出&Q";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(48, 176);
this.button2.Name = "button2";
this.button2.TabIndex = 8;
this.button2.Text = "最小化";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// timer1
//
this.timer1.Interval = 5000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// tB_mins
//
this.tB_mins.Location = new System.Drawing.Point(112, 124);
this.tB_mins.Name = "tB_mins";
this.tB_mins.Size = new System.Drawing.Size(144, 21);
this.tB_mins.TabIndex = 10;
this.tB_mins.Text = "20000";
this.tB_mins.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);
//
// label4
//
this.label4.Location = new System.Drawing.Point(32, 128);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 9;
this.label4.Text = "时间间隔:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(264, 129);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 16);
this.label5.TabIndex = 11;
this.label5.Text = "毫秒";
//
// button3
//
this.button3.Location = new System.Drawing.Point(264, 176);
this.button3.Name = "button3";
this.button3.TabIndex = 12;
this.button3.Text = "应用";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// cB_autorun
//
this.cB_autorun.Location = new System.Drawing.Point(112, 152);
this.cB_autorun.Name = "cB_autorun";
this.cB_autorun.Size = new System.Drawing.Size(160, 24);
this.cB_autorun.TabIndex = 13;
this.cB_autorun.Text = "启动时自动执行";
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 301);
this.Controls.Add(this.cB_autorun);
this.Controls.Add(this.button3);
this.Controls.Add(this.label5);
this.Controls.Add(this.tB_mins);
this.Controls.Add(this.label4);
this.Controls.Add(this.button2);
this.Controls.Add(this.tB_status);
this.Controls.Add(this.tB_pwd);
this.Controls.Add(this.tB_user);
this.Controls.Add(this.tB_server);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "WinForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "收取邮件";
this.Load += new System.EventHandler(this.WinForm_Load);
this.Activated += new System.EventHandler(this.WinForm_Activated);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
string str_server;
if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!=""))
{
TcpClient tcpc = new TcpClient(tB_server.Text,110);
Byte[] outbytes;
string input;
NetworkStream ns = null;
try{
ns = tcpc.GetStream();
StreamReader sr = new StreamReader(ns);
tB_status.Text=sr.ReadLine();
input = "USER " + tB_user.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "PASS " + tB_pwd.Text + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
input = "STAT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
str_server=sr.ReadLine();
str_num="";
if (str_server.StartsWith("+OK")){
str_num=str_server.Split(' ')[1];
} else str_num="";
tB_status.Text=tB_status.Text+"\r\n"+str_server;
input = "QUIT" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length)
tB_status.Text=tB_status.Text+"\r\n"+sr.ReadLine();
}
catch(InvalidOperationException ioe){
tB_status.Text="Could not connect to mail server";
}
if (str_num!="") {
WinForm1 form = new WinForm1();
form.str_num=str_num;
form.ScrollShow();
}
}
}
private void WinForm_Activated(object sender, System.EventArgs e)
{
if (f_open) {
this.Hide();
f_open=false;
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Show();
this.Focus();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Hide();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
button1_Click(sender,e);
}
private void tB_pwd_TextChanged(object sender, System.EventArgs e)
{
timer1.Enabled=false;
if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!=""))
{
timer1.Interval=System.Convert.ToInt32(tB_mins.Text);
timer1.Enabled=true;
} else
{
timer1.Enabled=false;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
RegistryKey rk;
Registry.CurrentUser.CreateSubKey("Software\\yousoft");
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",true);
rk.Setvalue("server",tB_server.Text);
rk.Setvalue("user",tB_user.Text);
rk.Setvalue("pwd",tB_pwd.Text);
rk.Setvalue("interval",tB_mins.Text);
if (cB_autorun.Checked) {
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Setvalue("chkmail",Application.ExecutablePath);
} else
{
rk=Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
rk.Deletevalue("chkmail");
}
rk.Close();
}
private void WinForm_Load(object sender, System.EventArgs e)
{
RegistryKey rk;
rk=Registry.CurrentUser.OpenSubKey("Software\\yousoft",false);
object srv=rk.Getvalue("server");
if (srv!=null) tB_server.Text=srv.ToString();
object usr=rk.Getvalue("user");
if (usr!=null) tB_user.Text=usr.ToString();
object pwd=rk.Getvalue("pwd");
if (pwd!=null) tB_pwd.Text=pwd.ToString();
object mins=rk.Getvalue("interval");
if (mins!=null) tB_mins.Text=mins.ToString();
rk.Close();
}
}
}
按F9编译程序并运行。如图:
[ 相关贴图 ]
[ 相关贴图 ]
[ 相关贴图 ]
四.小结:
本文向读者介绍了如何运用Borland C# builder实现POP3信箱的监视,其中大量运用了Windows Forms编程和.Net框架下的网络编程的原理。通过本文,希望能使读者对.Net中的Borland C# builder有一个感性的认识,同时对Windows Forms编程和其中的网络编程也有个大致的了解。如果有兴趣,可以到悠游在线(网址:http://yousoft.hi.com.cn)下载全部的源代码或与本人交流。
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) 这样一个完整的托盘程序就设好了。
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数