?? class1.cs
字號:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace 登陸POP3服務器_獲得帳戶中電子郵件數據
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class Class1
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
private static TcpClient tpServer ;
private static byte[] byData ;
//設定一個字節數組,用以存放向POP3服務器傳送的命令
private static string CRLF = "\r\n" ;
//定義回車換行符
private static string sRec ;
//定義一個字符串,用以存放從POP3服務器反饋數據
private static string sData ;
//定義一個字符串,用以存放向POP3服務器傳送的命令字符串
private static NetworkStream nsStream;
//客戶機和POP3服務器進行數據交換的基礎數據流
private static StreamReader rdStream;
//從基礎數據流中讀取數據
[STAThread]
static void Main(string[] args)
{
if ( LoadPOP ( "pop.21cn.com" , "majinhu" , "123456" ) )
{
sData = "STAT" + CRLF ;
//定義獲取帳戶中郵件數目和占用空間的命令
byData = System.Text.Encoding.Default.GetBytes ( sData.ToCharArray ( ) ) ;
nsStream.Write ( byData , 0 , byData.Length ) ;
//向POP3服務器傳送獲取帳戶中郵件數目和占用空間的命令
sRec = rdStream.ReadLine ( ) ;
//接收服務器反饋的數據
Console.WriteLine ( "向POP3服務器傳送獲取帳戶數據命令后,服務器返回的信息:" + sRec ) ;
String[] Num = sRec.Split ( " ".ToCharArray ( ) ) ;
Console.WriteLine ( "帳戶中郵件數目:" + Num[ 1 ] + ",所占空間:"+ Num[ 2 ] + "字節" ) ;
}
//
// TODO: 在此處添加代碼以啟動應用程序
//
}
public static bool LoadPOP ( string POPServer , string POPUser , string POPPsw )
{
string sPOPServer = POPServer ;
string sPOPUser = POPUser ;
string sPOPPass = POPPsw ;
try
{
tpServer = new TcpClient ( sPOPServer , 110 ) ;
//和POP3服務器的110端口號建立TCP連接
}
catch
{
Console.WriteLine ("無法和指定的POP3服務器建立連接!" ) ;
return false;
}
nsStream = tpServer.GetStream ( ) ;
//獲取客戶機和服務器會話的數據流
rdStream = new StreamReader ( tpServer.GetStream ( ) ) ;
sRec = rdStream.ReadLine ( ) ;
sData = "USER "+ sPOPUser + CRLF ;
//定義向POP3服務器傳送帳戶對應的命令行
byData = System.Text.Encoding.Default.GetBytes ( sData.ToCharArray ( ) ) ;
nsStream.Write ( byData , 0 , byData.Length ) ;
//向POP3服務器傳送帳戶
sRec = rdStream.ReadLine ( ) ;
//接收POP3服務器反饋數據
Console.WriteLine ( "向POP3服務器傳送帳戶后,服務器返回的信息:" + sRec ) ;
string [ ] sUserString = sRec.Split ( " ".ToCharArray ( ) ) ;
if ( sUserString[ 0 ] == "-ERR" )
{
Console.WriteLine ( "該POP3服務器中沒有此帳戶!" ) ;
return false;
}
sData = "PASS " + sPOPPass + CRLF ;
//定義向POP3服務器傳送帳戶口令對應的命令行
byData = System.Text.Encoding.Default.GetBytes ( sData.ToCharArray ( ) ) ;
nsStream.Write ( byData , 0 , byData.Length ) ;
//向POP3服務器傳送帳戶對應口令
sRec = rdStream.ReadLine ( ) ;
//接收POP3服務器反饋數據
Console.WriteLine ( "向POP3服務器傳送帳戶口令后,服務器返回的信息:" + sRec ) ;
string[ ] sPassString = sRec.Split ( " ".ToCharArray ( ) ) ;
if ( sPassString[ 0 ] == "-ERR" )
{
Console.WriteLine ( "帳戶對應的口令有誤!" ) ;
return false;
}
return true;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -