//希望通过这篇文章,你可以用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();
}
}
分享到:
相关推荐
如何使用C#访问POP3服务器系统C#代码
C#实现pop3 接收邮件
51CTO下载-运用C#实现POP3邮件接收程序源代码
老外用c#写的一个邮件服务器.实现了smtp pop3等协议,另外,里面的网络库还实现了一些其他的协议,比如FTP,HTTP,RTP SIP等.
使用C#接收 POP3邮件的详细步骤,附带部分代码
c#中通过POP3获取邮件,这个是下载就可以用的。。
运用C#实现POP3邮件接收程序运用C#实现POP3邮件接收程序运用C#实现POP3邮件接收程序
基于C# POP3邮件收件服务器 引用了OpenPOP 简单的使用,易于理解!
连个文件 一个运用C#实现POP3邮件接收程序源代码下载 一个运用C#实现邮件发送 两个都可以用vs2005直接打开运行! 实现了163.126邮箱的!
C# POP3接收邮件程序(可用),有需要的就下载吧
主要为大家详细介绍了C#如何连接服务器共享文件夹,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
开源的pop3 客户端, 用C#从底层写的POP3邮件接收程序,让您从底层了解邮件接收的原理,对从事邮件开发开发人员很有帮助。
C# POP3协议邮件附件自动获取 引用OpenPOP 获取当日邮件信息 自动创建子文件夹 添加本地文件去重功能
在V isual C#编程环境下应用POP3协议,通过套接字发送POP3命令,接收并处理邮件服务器的反馈信息,从而在客户端实现对电子邮件的接收。
亲测可用! 还存在的BUG: 1.有的邮件会出现乱码,有待修正; 2.qq邮箱只能接收今天收到的邮件;
使用 C# 创建一个 FTP 服务器,供大家一起共同分享学习。
自己用TCP编写的pop3 邮件接收小程序 还有待改进!
课程设计论文,有关SMTP服务器端的创建,有完整的截图和详细的内容,可以供大家借鉴
这个程序我在网上搜了,有很多方法,但是都很凌乱,很散,我集合了这些方法编写出了一个完整的pop3 加SSL加密的接收邮件方法。
提供账号密码能够获取邮箱内的联系人 保证能通过