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

转载 2008年05月01日 03:39:00
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
//希望通过这篇文章,你可以用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();
}
} &
11AJ44AI0IR7.jpg
seover="window.status='正文--如何使用C#访问POP3服务器(转)';return true">
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

使用C#开发信息管理系统

本课程将C#面向对象编程方法、ADO.NET技术、数据库技术、WinForm开发技术综合在一起,讲解一个完整的项目,能够帮助C#学员顺利掌握企业项目开发的核心技能。 【课程咨询】请加QQ:995551858(报名咨询+购买后资料发放)
  • 2015年06月10日 16:33

如何使用C#访问POP3服务器系统C#代码

  • 2009年06月23日 08:09
  • 14.88MB
  • 下载

c#使用pop3服务器进行邮箱验证

环境开发环境:.net4.0+vs2013 操作系统:win8.1内容由于项目需要,拟开发一个模块进行邮箱验证操作,最终采取使用pop3服务器进行邮箱登陆并且获取内容编码 /// ...
  • s906199800
  • s906199800
  • 2017-10-30 11:48:40
  • 236

C#POP3协议实现SSL验证登陆GMAIL

最近在折腾POP3协议,登陆pop.qq.com和pop.163.com没有什么问题,于是就想着登陆pop.gmail.com,结果失败了。经查,发现gmail的pop3端口不是110,而是995。于...
  • xxdddail
  • xxdddail
  • 2013-07-30 17:46:35
  • 3195

c#之 简单stmp/pop邮件服务(一)-------发送邮件

大家好!很高兴和大家分享,邮件服务操作。我也是昨天刚刚学会的!呵呵,希望对大家有用。c#的邮件服务,主要有两种。一种是利用JMail组件,进行收发。另一种是利用vs自带的stmp/pop3邮件服务,来...
  • kkfdsa132
  • kkfdsa132
  • 2010-03-17 10:59:00
  • 5636

详图解-用C#从POP3服务器取得邮件(提供源代码下载)

Download source files - 6 Kb Download demo project - 4 Kb IntroductionIn this article, Ill s...
  • metababy
  • metababy
  • 2006-01-18 17:15:00
  • 2123

c#中邮件收发处理(POP3,IMAP,SMTP)

来源: 随心所欲 - 博客园  本文涉及的内容:  1:自己制作Pop3Helper  信件格式的处理有麻烦  2:使用现成的pop3类  LumiSoft.Net.POP3.Client;   有两...
  • lovewiyi
  • lovewiyi
  • 2007-10-19 21:39:00
  • 1333

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

//希望通过这篇文章,你可以用C#写出自己的Email客户端程序 This is a follow up to my SMTP example that shows how to access you...
  • airweilai
  • airweilai
  • 2008-06-14 17:20:00
  • 108

如何使用C#访问POP3服务器(英文)

  //希望通过这篇文章,你可以用C#写出自己的Email客户端程序This is a follow up to my SMTP ...
  • xqf222
  • xqf222
  • 2006-07-22 11:31:00
  • 840

C#实现POP3邮件接收程序

来自:http://outofmemory.cn/code-snippet/2409/C-achieve-POP3-email-receive-program 本文通过运用C#来实现一个基于...
  • ArvinStudy
  • ArvinStudy
  • 2013-03-08 11:41:30
  • 2953
收藏助手
不良信息举报
您举报文章:如何使用C#访问POP3服务器(转)
举报原因:
原因补充:

(最多只允许输入30个字)