科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件C# Builder实现POP3信箱的监视

C# Builder实现POP3信箱的监视

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文向大家介绍怎样用Borland C# Builder编写一个信箱监视程序。

作者:徐长友 来源:悠游在线 2007年11月14日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
 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)下载全部的源代码或与本人交流。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章