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

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

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

【 tulaoshi.com - ASP.NET 】

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

(本文来源于图老师网站,更多请访问https://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[] {' '});

input = "quit" + "rn";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
Console.WriteLine(sr.ReadLine());

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

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

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

延伸阅读
Foxmail如何设置POP3邮箱   1请下载并安装最新Foxmail 最新版本,打开 Foxmail:点击邮箱- 新建邮箱帐户,弹出以下窗口:(填入您的电子邮件地址,密码,以及账户名称) 2点击下一步弹出以下窗口,在POP3服务器和SMTP服务器处填入"mail.你的域名"(例如您的域名是 comingchina.com ,就填写mail.comingchina.com),在P...
标签: 服务器
如何设置FTP服务器共享访问权限 利用ftp服务器软件架设好FTP服务器后,不要以为就万事OK了。我们还需要对信息上传目录的访问权限进行合适设置,以确保每一个部门的员工只能使用特定帐号登录、访问各自的信息传输目录,下面就是具体的设置步骤: 首先按照前面步骤打开Internet信息服务窗口,在该窗口的左侧显示区域,用鼠标右键单击目...
标签: ASP
  大家知道直接使用ASP是不能够重启服务器的,这时我们需要制作一个组件来实现功能,ASP通过这个组件调用系统API,然 后按照不同的重启和关机方式进行操作!            下面先说COM的制作,在VB中新建一工程,当然是AceiveX dll的!      1)先修改工程属性...
Every so often people ask me the question how should they estimate memory consumption by MySQL Server in given configuration. What is the formula they could use. 经常有人问我配置MySQL时该如何估算内存的消耗。那么该使用什么公式来计算呢? The reasons to worry about memory usage are quite understandable. If ...
标签: 电脑入门
一直以来,QQ邮箱的所有用户都可免费享受POP3和SMTP服务。有使用邮件客户端习惯的网友,或者当您在受到网络限制不能登录WEB的情况下,同样可以通过POP3/SMTP功能来使用QQ邮箱。 有些网友可能还对QQ邮箱的POP3/SMTP相关设置不熟悉,今天我们就一起来了解一下。 用POP3/SMTP服务收发QQ邮件 请进入邮箱“设置”,在“帐...

经验教程

991

收藏

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