接上面
// 
// PopServer
// 
this.PopServer.Location = new System.Drawing.Point(136, 16);
this.PopServer.Name = "PopServer";
this.PopServer.Size = new System.Drawing.Size(168, 21);
this.PopServer.TabIndex = 0;
this.PopServer.Text = "";
// 
// Username
// 
this.Username.Location = new System.Drawing.Point(136, 48);
this.Username.Name = "Username";
this.Username.Size = new System.Drawing.Size(168, 21);
this.Username.TabIndex = 1;
this.Username.Text = "";
// 
// Password
// 
this.Password.Location = new System.Drawing.Point(136, 80);
this.Password.Name = "Password";
this.Password.PasswordChar = '*';
this.Password.Size = new System.Drawing.Size(168, 21);
this.Password.TabIndex = 2;
this.Password.Text = "";
// 
// MailNum
// 
this.MailNum.Location = new System.Drawing.Point(88, 272);
this.MailNum.Name = "MailNum";
this.MailNum.Size = new System.Drawing.Size(40, 21);
this.MailNum.TabIndex = 4;
this.MailNum.Text = "";
// 
// Connect
// 
this.Connect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Connect.Location = new System.Drawing.Point(320, 24);
this.Connect.Name = "Connect";
this.Connect.Size = new System.Drawing.Size(75, 24);
this.Connect.TabIndex = 7;
this.Connect.Text = "连接";
this.Connect.Click += new System.EventHandler(this.Connect_Click);
// 
// Disconnect
// 
this.Disconnect.Enabled = false;
this.Disconnect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Disconnect.Location = new System.Drawing.Point(320, 64);
this.Disconnect.Name = "Disconnect";
this.Disconnect.Size = new System.Drawing.Size(75, 24);
this.Disconnect.TabIndex = 8;
this.Disconnect.Text = "断开连接";
this.Disconnect.Click += new System.EventHandler(this.Disconnect_Click);
// 
// Retrieve
// 
this.Retrieve.Enabled = false;
this.Retrieve.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Retrieve.Location = new System.Drawing.Point(320, 272);
this.Retrieve.Name = "Retrieve";
this.Retrieve.Size = new System.Drawing.Size(72, 24);
this.Retrieve.TabIndex = 9;
this.Retrieve.Text = "收取邮件";
this.Retrieve.Click += new System.EventHandler(this.Retrieve_Click);
// 
// Message
// 
this.Message.Location = new System.Drawing.Point(8, 136);
this.Message.Name = "Message";
this.Message.Size = new System.Drawing.Size(384, 128);
this.Message.TabIndex = 3;
this.Message.Text = "";
// 
// Status
// 
this.Status.ItemHeight = 12;
this.Status.Location = new System.Drawing.Point(8, 328);
this.Status.Name = "Status";
this.Status.Size = new System.Drawing.Size(384, 52);
this.Status.TabIndex = 6;
// 
// BackupChBox
// 
this.BackupChBox.Location = new System.Drawing.Point(144, 272);
this.BackupChBox.Name = "BackupChBox";
this.BackupChBox.Size = new System.Drawing.Size(160, 24);
this.BackupChBox.TabIndex = 5;
this.BackupChBox.Text = "在邮件服务器上保留备份";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 389);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.BackupChBox,
  this.Status,
  this.Message,
  this.Retrieve,
  this.Disconnect,
  this.Connect,
  this.MailNum,
  this.Password,
  this.Username,
  this.PopServer,
  this.label6,
  this.label5,
  this.label4,
  this.label3,
  this.label2,
  this.label1});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "POP3邮件接收程序";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Connect_Click(object sender, System.EventArgs e)
{
//将光标置为等待状态
Cursor cr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor; //用110端口新建POP3服务器连接
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 = "PASS "+ Password.Text+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine()); //向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小
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, System.EventArgs e)
{
//将光标置为等待状态
Cursor cr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor; //向服务器发送QUIT命令从而结束和POP3服务器的会话
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, System.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();
} //若BackupChBox未选中,则收取邮件后,删除保留在服务器上的邮件
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(InvalidOperationException err)
{
Status.Items.Add("Error: "+err.ToString());
}
}
}
}

解决方案 »

  1.   

    http://www.codeproject.com/useritems/POP3Library.asp这边有个例子,看看,能不能用的。
      

  2.   

    用JMAIL解决了这个问题,总结一下:
    收发邮件都可以使用jmail,在.net下通过以下步骤生成所需的dll,
    1.安装jmail4.3 
    2.找到jmail.dll(Program Files\Dimac\w3JMail4下)
    3.执行Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe,
    格式如下:tlbimp jmail.dll /out:myJmail.dll /namespace:myJmail
    生成myJmail.dll后,copy到web的根目录的bin目录,
    在vs.net中引用 using jmail;在其他机器上注册
    在运行窗口中运行 regsvr32 E:\王伟的共享\jmail.dll 然后在项目中引用jmail发送邮件
        public void sendEmail(string receiver,string title,string content)
        {
       Message email=new Message();   email.AddRecipient(receiver,receiver,"");   email.Charset="gb2312";
       email.From = "[email protected]";
       email.Subject = title;   email.HTMLBody = content;   email.Send("username:password@smtpServer",false); 
        } 
    接收邮件:
    jmail.POP3 pop=new jmail.POP3();
    pop.Connect("username","password","pop3Server",110);
    pop.Logging=true;
    Messages mm=pop.Messages;
    try
    {
    for(int i=1;i<mm.Count;i++)
    {
    Response.Write(mm[i].Subject.ToString()+"<br>");
    }
    }
    catch(Exception ex)
    {
    Response.Write(ex.Message);
    }
    pop.Disconnect();
    -----------------------------------------------
      

  3.   

    可不可以不用jmail解决。HELP ME!!!
      

  4.   

    C#中对POP3邮件解码
    http://www.9seek.com/news/show.aspx?id=516&cid=13
      

  5.   

    我有vb.net的
    http://expert.csdn.net/Expert/topic/2459/2459551.xml?temp=.802334
      

  6.   

    http://www.codeproject.com/csharp/pop3client.asp
    或者试这个,看好不好用
      

  7.   

    gjs_w (阿松), 哥们儿
    我可以解开 Base64 的编码
    源代码如下:
    using  System.text
    =====================
    private void button1_Click_1(object sender, System.EventArgs e)
    {//======[解码]按钮
    try
    {//把textBox4 解到 textBox3
       textBox3.Text = Base64Decode(textBox4.Text);
    }
    catch(Exception ee)
    {MessageBox.Show("异常提示信息:"+ee.Message);}
    }
    ======================
    protected string Base64Decode(string dstr)
    {
    byte[] barray;
    barray=Convert.FromBase64String(dstr);
    return Encoding.Default.GetString(barray);
    }
    但是你要自己判断信中 Base64 编码的开始 和结束
    结束是 "="
    你把 上方代码中的  szTemp  在 listbox 中一行一行的追加
    你就会看到 Base64 编码的开始 和结束
    但是我还不知道 QP 格式的编码怎么解放!
    而且我现在测试用的 WebEasyMail  TMD用的就是QP 格式的编码!
    我哭~~~~~~~
    要是解决麻烦给回个信
    [email protected] 谢谢啊!
      

  8.   

    到这里http://msdn.microsoft.com/搜“POP3”!
      

  9.   

    去下个MyOE.NET源码吧,我现在都用它来收信,挺好用的,介面又好看
    http://www.myoe.org/modules/news/
      

  10.   

    要解码,很烦。涉及到N个RFC的内容。