国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何使用C#訪問POP3服務器

2024-07-21 02:25:41
字體:
來源:轉載
供稿:網友
this is a follow up to my smtp example that shows how to access your pop3 server. this program connects and logs on to your pop3 server, and checks to see how many new messages you have.

the instantiation of the pop is in main() like this:
pop pop = new pop("pop-server", "loginname", "password"); you must replace "pop-server" with the name of your pop server, "loginname" with your own log in, and "password" with your password. the class has two methods. the connect method takes care of actually logging in to the server. the tcpclient class is used to establish the connection. the "user" and "pass" commands are used to login. connect returns a networkstream object created during the connection process. the second method is getnumberofnewmessages, which returns the number of unread messages on the server. the response to the "stat" command is parsed to extract the number of new messages.


requirement:

requires .net sdk


how to compile?


csc /r:system.net.dll /r:system.io.dll pop.cs  

source code

using system.net.sockets;
using system.io;
using system.net;
using system;

class pop
{
string popserver;
string user;
string pwd;
public pop(){}
public pop(string server, string _user, string _pwd)
{
popserver = server;
user = _user;
pwd = _pwd;
}
private networkstream connect()
{
tcpclient sender = new tcpclient(popserver,110);
byte[] outbytes;
string input;
networkstream ns = null;
try{
ns = sender.getstream();
streamreader sr = new streamreader(ns);
console.writeline(sr.readline() );

input = "user " + user + "/r/n";
outbytes = system.text.encoding.ascii.getbytes(input.tochararray());
ns.write(outbytes,0,outbytes.length) ;
console.writeline(sr.readline() );

input = "pass " + pwd + "/r/n";
outbytes = system.text.encoding.ascii.getbytes(input.tochararray());
ns.write(outbytes,0,outbytes.length) ;
console.writeline(sr.readline() );

return ns;
}
catch(invalidoperationexception ioe){
console.writeline("could not connect to mail server");
return ns;
}
}
public int getnumberofnewmessages()
{
byte[] outbytes;
string input;
try{
networkstream ns = connect();
streamreader sr = new streamreader(ns);

input = "stat" + "/r/n";
outbytes = system.text.encoding.ascii.getbytes(input.tochararray());
ns.write(outbytes,0,outbytes.length);
string resp = sr.readline();
console.writeline(resp);
string[] tokens = resp.split(new char[] {' '});

input = "quit" + "/r/n";
outbytes = system.text.encoding.ascii.getbytes(input.tochararray());
ns.write(outbytes,0,outbytes.length);
console.writeline(sr.readline());

sr.close();
ns.close();
return tokens[1].toint32();
}
catch(invalidoperationexception ioe){
console.writeline("could not connect to mail server");
return 0;
}
}
public static void main()
{
pop pop = new pop("pop-server", "loginname", "password");
console.writeline("new messages = {0}", pop.getnumberofnewmessages() );
console.readline();
}
}   

商業源碼熱門下載www.html.org.cn

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宾阳县| 邳州市| 马关县| 毕节市| 会宁县| 临桂县| 依兰县| 龙口市| 施甸县| 神农架林区| 南投县| 永济市| 扶沟县| 蚌埠市| 玉田县| 聂荣县| 铜山县| 灌阳县| 格尔木市| 中方县| 永清县| 蓬安县| 凌源市| 沧源| 深泽县| 宁晋县| 正安县| 原阳县| 宁明县| 洪江市| 兰考县| 二连浩特市| 茌平县| 左云县| 抚宁县| 塔河县| 光泽县| 公主岭市| 城口县| 凤山县| 上栗县|