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

2016-01-29 13:51 38 1 收藏

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

【 tulaoshi.com - ASP.NET 】

//希望通过这篇文章,你可以用C#写出自己的Email客户端程序

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com)

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 + "rn";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );

input = "pass " + pwd + "rn";
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" + "rn";
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[] {' '});

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com)

input = "quit" + "rn";
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();
}
}

来源:http://www.tulaoshi.com/n/20160129/1491752.html

延伸阅读
代码如下: using System; using System.Collections; using System.Text; using System.IO; using System.Collections.Specialized; using System.Text.RegularExpressions; using System.Diagnostics; namespace CSS { public class App { public static void Main(string[] args) { //初始化CSS解析器 CssDocument doc = ne...
标签: 服务器
如何设置FTP服务器共享访问权限 利用ftp服务器软件架设好FTP服务器后,不要以为就万事OK了。我们还需要对信息上传目录的访问权限进行合适设置,以确保每一个部门的员工只能使用特定帐号登录、访问各自的信息传输目录,下面就是具体的设置步骤: 首先按照前面步骤打开Internet信息服务窗口,在该窗口的左侧显示区域,用鼠标右键单击目...
标签: 电脑入门
一直以来,QQ邮箱的所有用户都可免费享受POP3和SMTP服务。有使用邮件客户端习惯的网友,或者当您在受到网络限制不能登录WEB的情况下,同样可以通过POP3/SMTP功能来使用QQ邮箱。 有些网友可能还对QQ邮箱的POP3/SMTP相关设置不熟悉,今天我们就一起来了解一下。 用POP3/SMTP服务收发QQ邮件 请进入邮箱“设置”,在“帐...
标签: PHP
  test_pop3.php <HTML <HEAD <TITLETest for Manuel Lemos's PHP POP3 class</TITLE </HEAD <BODY <? require("pop3.php"); $user="user"; $password="passwd"; $apop=0; $pop3_connection=new pop3_class; $pop3_connection-hostname="mail.xiaocui.com"; if(($error=$...
标签: Delphi
  ---- 本 文 将 向 大 家 介 绍 怎 样 编 写自 己 的 信 箱 监 视 程 序, 程 序 将 直 接 调 用WinSock 函 数 来 进 行网 络 通 信。 除 了 具 备WinSock 编 程 知 识 之 外, 还 必 须 了 解POP3 协 议。 下 面 是 对POP3 的 一 个 粗 略 的 介 绍, 读 者 可 以 参 看RFC 1225 更 为 详 细 地 了 解 该 协 议。 一、 关 于POP3 协...

经验教程

688

收藏

23
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部