通行证: 用户 密码 域名空间  下载中心 社区论坛 信息公告 MY小屋
联系我们
设为首页
加入收藏

 

QQ,ASP,PHP,JSP,XML,SQL,.Net,编程 程序 网页图象 建站经验 私服
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿
论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图
专题 | 应用软件 | 聊天通讯 | Q Q 专栏 | 建站经验 | 在线工具 | 站长Club | 注 册 表 | 旧版
社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务
当前位置:首页>>编程开发>>NET专区>>正文 新版上线![旧版]
注:打开慢时请稍等

如何使用C#访问POP3服务器

http://www.iyit.net  日期:2006-6-12 1:59:33  来源:  点击:
参加讨论】//希望通过这篇文章,你可以用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.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();
}
}



编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部]
上一篇:用ado+来删除数据
下一篇:用DataList 控制元件开发的一个简单的留言本程序
转载请注明来源:www.iyit.net
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

 相关文章
如何在服务器上保存一定时间的信息 利用asp+的独特的底层操作的功能实现对 查看服务器磁盘、文件的aspx
多中WEB服务器的通用JSp源代码暴露漏洞 WebSphere应用服务器 安装 WebSphere应用服务器
Resin服务器平台介绍简介 在Linux上架设支持JSP+PHP的Web服务器 Apache服务器之JSP概述篇
WIN98/2000下的jsp服务器 让你的IIS服务器支持JSP JSP由浅入深(1)—— 熟悉JSP服务器
用session代替apache服务器验证 在服务器上安装、使用MySQL的注意事项 php3:跨平台的服务器端嵌入式脚本语言
配置最新的PHP加MYSQL服务器 apache+mysql+php+ssl服务器之完全安装 PHP在XP下IIS和Apache2服务器上的安装
PHP4在WinXP下IIS和Apache2服务器上的安 PHP程序与服务器端通讯方法小结 PHP程序与服务器端通讯的方法
PHP程序加速探索之服务器负载测试 PHP巧获服务器端信息 WINDOWS服务器安装多套PHP的另类解决方
最新更新 热点排行 推荐新闻
使用 ASP.NET 加密口令
[ASP.NET] Session 详解
从ASP迁移至ASP+
从ASP迁移至ASP+ ----------进入DataS
从ASP迁移至ASP+ --将HTML表格转换为A
CGI教程(2)
CGI教程(3)
CGI教程(5)
CGI教程(6)
CGI教程(7)
CGI教程(8)
CGI教程(5)
CGI教学:第一章 cgilib例
CGI教学:第二章 动态创建图像
CGI教学:第三章 计数器的编写方法
为ASP.NET 2.0菜单控件增加target属性
将Asp.net页面输出为HTML
在ASP.NET中防止注入攻击
ASP.NET 2.0 中的异步页功能应用
asp.net面试试题收集
QQ密码本地破解
msn8.0下载
PPLive最新内部版本揭密
珊蝴虫QQ探测隐身的招式用法
Visual Basic 概述
exeplorer.exe错误的问题的总结、解决
Authorware7.0基础与实例教程连载 第5
解决Windows中的explorer.exe出错
Windows常见文件修复技巧
Photoshop打造美女性感纹身(2)
在ASP.NET中防止注入攻击
用ASP.NET开发Web服务的五则技巧
ASP.NET 2.0 中的异步页功能应用
Windows下的虚拟主机设置全功略
危险无处不在 Html标签带来的安全隐患
网络游戏是06年互联网最具发展潜力业务
巧用ACDSee 8.0截取QQ表情每一帧
解除瑞星 诺顿遗留下的杀毒兼容问题
第一款开源AJAX安全扫描工具Sprajax
WinRAR人性化功能揭密
在ASP.NET中防止注入攻击
用ASP.NET开发Web服务的五则技巧
Linux操作系统12则经典应用技巧
 友情链接
设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接
Copyrights © 2004-2006 iYiT.Net All Rights Reserved.
网站合作、广告联系QQ:147007642、466949678
易特网络技术 点击这里给我发消息