国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇坪县| 涪陵区| 嘉峪关市| 灵寿县| 巫溪县| 张家界市| 平塘县| 都江堰市| 九江市| 靖江市| 偏关县| 祁阳县| 灵武市| 大石桥市| 德州市| 禹城市| 孙吴县| 五大连池市| 固原市| 东阳市| 湖北省| 延川县| 沾化县| 延津县| 格尔木市| 西乌| 闽侯县| 泗水县| 赤水市| 江津市| 石渠县| 秦皇岛市| 黄骅市| 五台县| 葵青区| 新宁县| 荣成市| 建湖县| 泗阳县| 门源| 澜沧|