//希望通过这篇文章,你可以用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服务器系统,为开发者提供详细的步骤和代码示例。 POP3协议主要用于从邮件服务器下载邮件。它允许用户在本地客户端上存储邮件,通常在读取后会从服务器删除,以节省服务器空间...
C#中的.NET Framework提供了丰富的类库,使得与POP3服务器交互变得简单。 首先,我们需要了解`System.Net.Mail`命名空间,这是C#中处理邮件相关任务的核心。在这个命名空间下,`Pop3Client`类用于与POP3服务器通信...
2. **连接到邮件服务器**:使用`Connect()`方法建立与Pop3服务器的连接。如果是SSL连接,还需要使用`EnableSsl`属性设置为`true`。 3. **验证身份**:调用`Authenticate()`方法进行身份验证。这通常需要用户名和...
提供的压缩包文件“MailReceiver”可能包含了一个完整的C#示例项目,用于演示如何从POP3服务器接收邮件。这个项目可以作为学习和参考的起点,通过阅读代码,你可以更深入地了解如何在实际应用中实现这些功能。 总的...
标题中的“51CTO下载-运用C#实现POP3邮件接收程序源代码”表明这是一个关于使用C#编程语言开发的邮件接收程序,基于POP3(Post Office Protocol version 3)协议。POP3是一种标准的互联网协议,允许用户从邮件服务器...
2. **Pop3Client类**: `Pop3Client`是C#中用于连接到POP3服务器并执行操作的核心类。创建`Pop3Client`对象后,你需要设置服务器地址、端口号、用户名和密码。然后,通过调用`Connect()`方法建立连接,`Authenticate...
当用户连接到POP3服务器(通常使用端口110,或使用SSL加密的端口995),服务器会列出邮件箱中的邮件,用户可以选择下载或删除它们。 - POP3协议通常只提供离线访问,意味着一旦邮件被下载,服务器上可能会删除。...
C#中的.NET Framework提供了丰富的类库,如System.Net.Mail,使得开发者能够轻松地与POP3服务器交互。 首先,我们需要理解C#中收邮件的基本步骤: 1. **连接服务器**:使用`System.Net.NetworkInformation.Ping`类...
大部分情况下,POP3服务器的默认端口是110,如果启用了SSL/TLS加密,端口可能是995。 2. **身份验证**:连接成功后,使用`Authenticate`方法进行身份验证,通常使用用户名和密码。对于安全性考虑,可以使用`...
// 连接到POP3服务器 client.Connect("pop.example.com", 110, false); // 使用用户名和密码认证 client.Authenticate("username", "password"); // 获取邮箱信息 var inbox = client.Inbox; inbox.Open...
在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows桌面应用、Web应用以及游戏等领域。本主题聚焦于如何运用C#实现一个POP3邮件接收程序,这将帮助开发者理解网络通信和邮件处理的基本原理。 POP3(Post ...
标题中的"C#写的邮件服务器实现了smtp pop3 imap ftp icmp 等协议"表明这是一个使用C#编程语言开发的多协议邮件服务器程序。这个程序不仅支持SMTP(Simple Mail Transfer Protocol)、POP3(Post Office Protocol ...
1. **建立连接**:使用C#的网络编程类,如`System.Net.Sockets.TcpClient`或MailKit的`Pop3Client`,连接到POP3服务器。 2. **身份验证**:进行身份验证,通常使用用户名和密码,或者通过OAuth2等安全机制。 3. **...
当用户想要获取邮件时,客户端应用会连接到POP3服务器,通过身份验证后,服务器会列出邮件箱中的邮件数量和每个邮件的大小。客户端可以选择下载所有邮件或者只下载特定邮件。下载完成后,用户可以选择删除这些邮件...
1. **建立连接**:使用适当的网络库(如Python的socket库)与163邮箱的POP3服务器建立TCP连接。 2. **身份验证**:通过SMTP(Simple Mail Transfer Protocol)或POP3命令进行身份验证,提供用户名和密码。 3. **邮件...
在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows桌面应用、Web应用以及游戏等领域。本主题聚焦于利用C#从QQ邮箱和网易邮箱获取邮件及其附件,这一过程通常涉及邮件协议POP3(Post Office Protocol ...
在C#中实现POP3服务器,需要处理客户端的连接请求,提供邮件列表,允许用户选择并下载邮件,以及处理断开连接的逻辑。POP3的一个主要特点是它通常将邮件从服务器下载到本地设备,然后从服务器上删除。 IMAP4则比...
总的来说,这个C#项目是一个基础的POP3邮件接收器,它帮助学习者理解如何使用C#与邮件服务器通信,以及如何构建一个简单的邮件客户端界面。通过这个项目,你可以深入学习网络编程、邮件协议和C# UI设计,这些都是IT...
这本书旨在帮助开发者掌握使用C#进行网络应用开发的必备技能,包括基础知识、服务器实现、客户端编程,以及特定协议如FTP、SMTP、POP3的实现,同时还涉及到了Win32 API、Web技术和XML及Web服务的使用。 首先,基础...
Open POP3组件是一个开源的库,可能用不同的编程语言实现,如Python、Java或C#,它提供了接口来连接到POP3服务器,执行身份验证,列出邮件,下载邮件内容(包括附件),并可能提供了一些额外的功能,如邮件解析和...