Visual C#編寫實現POP3的程式

來源:互聯網
上載者:User

Visual C#編寫實現POP3的程式
1.開啟VS.NET 2003.

  2.建立一個WinForm Application.

  3.添加命名空間

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

  4.主要代碼

  為了防止介面在串連伺服器時死結,我另開了一個線程

private void button1_Click(object sender, System.EventArgs e)
{
 Thread myThread=new Thread(new ThreadStart(TreadGet));
 myThread.Start();
}

private void TreadGet()
{
 ConnectToServer();
 LogonServer();
 GetEmailList();
}

//串連伺服器
private void ConnectToServer()
{
 this.myClient=new TcpClient();
 try
 {
  this.myClient.Connect(this.serverNameTextBox.Text,110);
  this.messageListBox.Items.Add("串連伺服器成功");
  //初始化網路流,資料就是通過這個對象讀出來的
  ns=this.myClient.GetStream();
  sr=new StreamReader(ns);
  sw=new StreamWriter(ns);
 }
 catch(Exception ex)
 {
  this.messageListBox.Items.Add(ex.Message);
 }
}
//驗證使用者名稱,密碼
private void LogonServer()
{
 result=sr.ReadLine();
 this.messageListBox.Items.Add(result);
 sw.WriteLine("USER "+this.nameTextBox.Text);
 sw.Flush();

 result=sr.ReadLine();
 if(result.Substring(0,3)=="-ER")
 {
  this.messageListBox.Items.Add("沒有這個使用者名稱");
  return;
 }

 sw.WriteLine("PASS "+this.passTextBox.Text);
 sw.Flush();

 try
 {
  result=sr.ReadLine();
 }
 catch(IOException ioex)
 {
  this.messageListBox.Items.Add(ioex.Message);
  return;
 }

 if(result.Substring(0,4)=="-ERR")
 {
  this.messageListBox.Items.Add("無法登入,可能使使用者名稱密碼錯誤!");
  return;
 }

 this.messageListBox.Items.Add("登入成功");
 
}

//擷取郵件清單
private void GetEmailList()
{
 string from=null;
 string subject=null;
 sw.WriteLine("stat");
 sw.Flush();

 result=sr.ReadLine();
 // MessageBox.Show(result);
 string[] nummessage=result.Split(' ');
 int totalnum=Convert.ToInt32(nummessage[1]);
 if(totalnum>0)
  this.messageListBox.Items.Add("你有"+totalnum.ToString()+"郵件");
 else
  this.messageListBox.Items.Add("郵箱裡沒有郵件");

 for(int i=1;i<=totalnum;i++)
 {
  sw.WriteLine("top "+i.ToString()+" 0");
  sw.Flush();
  result=sr.ReadLine();

  while(true)
  {
   result=sr.ReadLine();
   if(result==".")
    break;
   if(result.Length>4)
   {
    if(result.Substring(0,5)=="From:")
     from=result ;
    if(result.Substring(0,8)=="Subject:")
     subject=result ;

   }
  }
  this.listBox1 .Items.Add(i.ToString()+" "+from+ " "+ subject);
 }
}

private void button2_Click(object sender, System.EventArgs e)
{
 this.listBox1.Items.Clear();
}

  參考資料

  網路郵件收取使用的是Pop3協議,瞭解Pop3協議有助於我們加深對郵件系統的理解。而且使用Pop3協議你還可以直接telnet 到郵件伺服器上去收信。

  一般telnet Pop3 Server 110後就可以用這些命令了,大小寫不敏感, 不包括口令本身,注意不要讓口令回顯,等驗證通過後再允許回顯好了。

  user username 使用者認可

  pass password 認可 執行成功則狀態轉換

  apop name,digest 認可一種安全傳輸口令的辦法,執行成功導致狀態轉換,請參見RFC 1321 。

  stat 處理請求server回送郵箱統計資料,如郵件數、 郵件總位元組數

  uidl n 處理 server返回用於該指定郵件的唯一標識, 如果沒有指定,返回所有的。

  list n 處理 server返回指定郵件的大小等

  retr n 處理 server返回郵件的全部文本

  dele n 處理 server標記刪除,quit命令執行時才真正刪除

  rset 處理撤消所有的dele命令

  top n,m 處理 返回n號郵件的前m行內容,m必須是自然數

  noop 處理 server返回一個肯定的響應

  quit client 希望結束會話。如果server處於"處理" 狀態,則現在進入"更新"狀態,刪除那些標記成刪除的郵件。如果server處於"認可"狀態, 則結束會話時server不進入"更新"狀態 。

  使用telnet 的一個完整的收信例子如下:

  telnet pop3Server 110
  user username
  pass ****
  stat
  list
  retr 1
  retr 2
  ...
  dele 1
  dele 2
  ...
  quit

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.