C#编写电子油箱

第一步:新建一个windows应用程序,命名为:Mail,并为生成的窗体添加相应的控件,效果图如下:

第二步,为控件添加相应的代码事件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Mail
{
    public partial class Form1 : Form
    {
        public TcpClient Server;
        public NetworkStream NetStrm;
        public StreamReader RdStrm;
        public string Data;
        public byte[] szData;
        public string CRLF = "\r\n";
        public Form1()
        {
            InitializeComponent();
        }

        private void Connect_Click(object sender, EventArgs e)
        {
            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            Server = new TcpClient(Popserver.Text,110);
            Status.Items.Clear();
            try
            {
                NetStrm = Server.GetStream();
                RdStrm = new StreamReader(Server.GetStream());
                Status.Items.Add(RdStrm.ReadLine());
                Data = "USER" + Username.Text + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                Status.Items.Add(RdStrm.ReadLine());
                Data = "STAT" + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                Status.Items.Add(RdStrm.ReadLine());
                Connect.Enabled = false;
                Disconnect.Enabled = true;
                Retrieve.Enabled = true;
                Cursor.Current = cr;
            }
            catch (InvalidOperationException err)
            {
                Status.Items.Add("ERROR:" + err.ToString());
            }
        }

        private void Disconnect_Click(object sender, EventArgs e)
        {
            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            Data = "QUIT" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData, 0, szData.Length);
            Status.Items.Add(RdStrm.ReadLine());
            NetStrm.Close();
            RdStrm.Close();
            Connect.Enabled = true;
            Disconnect.Enabled = false;
            Retrieve.Enabled = false;
            Cursor.Current = cr;
        }

        private void Retrieve_Click(object sender, EventArgs e)
        {

            Cursor cr = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            String szTemp;
            Message.Clear();
            try
            {
                Data = "RETR" + Mailnum.Text + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                szTemp = RdStrm.ReadLine();
                if (szTemp[0] != '-')
                {
                    while (szTemp != ".")
                    {
                        Message.Text += szTemp;
                        szTemp = RdStrm.ReadLine();
                    }
                    if (BackupChBox.Checked == false)
                    {

                        Data = "DELE" + Mailnum.Text + CRLF;
                        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                        NetStrm.Write(szData, 0, szData.Length);
                        Status.Items.Add(RdStrm.ReadLine());
                    }
                }
                else
                {
                    Status.Items.Add(szTemp);
                }
                Cursor.Current = cr;
            }
            catch (InvalidExpressionException err)
            {
                Status.Items.Add("ERROR" + err.ToString());
            }
        }
    }
}
说明:这只是一个简单的通讯编程,实现的功能也只是收取邮件,发送邮件功能的实现,需要等待......

posted @ 2008-05-29 10:03  小麻雀  阅读(1008)  评论(4编辑  收藏  举报