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

首頁 > 開發(fā) > 綜合 > 正文

如何使用C#訪問POP3服務(wù)器(轉(zhuǎn))

2024-07-21 02:20:17
字體:
供稿:網(wǎng)友
//希望通過這篇文章,你可以用c#寫出自己的email客戶端程序

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();
}
}  


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜黄县| 庄河市| 右玉县| 恭城| 盱眙县| 工布江达县| 凤庆县| 天镇县| 思南县| 盐亭县| 高唐县| 酒泉市| 图木舒克市| 瓦房店市| 衡阳县| 白山市| 泾源县| 武隆县| 融水| 凯里市| 仁怀市| 铜梁县| 沂水县| 上高县| 通州区| 精河县| 衢州市| 临安市| 饶阳县| 桂阳县| 古交市| 灵丘县| 石泉县| 府谷县| 丘北县| 新安县| 阳山县| 昌平区| 沈丘县| 昌黎县| 常山县|