方法一
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net.Configuration;
using System.Configuration;
#region 邮件接收类
///
/// 邮件接收类
///
public class POP3
{
#region Fields
string POPServer;
string mPOPUserName;
string mPOPPass;
int mPOPPort;
NetworkStream ns;
StreamReader sr;
#endregion
#region Constructors
///
/// POP3
///
/// POP3服务器名称
/// 用户名
/// 用户密码
public POP3(string server, string userName, string password)
: this(server, 110, userName, password)
{
}
///
/// POP3
///
/// POP3服务器名称
/// 端口号
/// 用户名
/// 用户密码
public POP3(string server, int port, string userName, string password)
{
POPServer = server;
mPOPUserName = userName;
mPOPPass = password;
mPOPPort = port;
}
#endregion
#region Methods
#region Public
///
/// 获得新邮件数量
///
///
public int GetNumberOfNewMessages()
{
byte[] outbytes;
string input;
try
{
Connect();
input = "stat" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
string resp = sr.ReadLine();
string[] tokens = resp.Split(new Char[] { ' ' });
Disconnect();
return Convert.ToInt32(tokens[1]);
}
catch
{
return -1;
}
}
///
/// 获取新邮件内容
///
/// 邮件主题
///
public List
{
int newcount;
List
try
{
newcount = GetNumberOfNewMessages();
Connect();
for (int n = 1; n
List
string msgsubj = GetMessageSubject(msglines);
if (msgsubj.CompareTo(subj) == 0)
{
MailMessage msg = new MailMessage();
msg.Subject = msgsubj;
msg.From = new MailAddress(GetMessageFrom(msglines));
msg.Body = GetMessageBody(msglines);
newmsgs.Add(msg);
DeleteMessage(n);
}
}
Disconnect();
return newmsgs;
}
catch (Exception e)
{
return newmsgs;
}
}
///
/// 获取新邮件内容
///
/// 新邮件索引
///
public MailMessage GetNewMessages(int nIndex)
{
int newcount;
MailMessage msg = new MailMessage();
try
{
newcount = GetNumberOfNewMessages();
Connect();
int n = nIndex + 1;
if (n
List
string msgsubj = GetMessageSubject(msglines);
msg.Subject = msgsubj;
msg.From = new MailAddress(GetMessageFrom(msglines));
msg.Body = GetMessageBody(msglines);
}
Disconnect();
return msg;
}
catch
{
return null;
}
}
#endregion
#region Private
private bool Connect()
{
TcpClient sender = new TcpClient(POPServer, mPOPPort);
byte[] outbytes;
string input;
try
{
ns = sender.GetStream();
sr = new StreamReader(ns);
sr.ReadLine();
input = "user " + mPOPUserName + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
input = "pass " + mPOPPass + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true;
}
catch
{
return false;
}
}
private void Disconnect()
{
string input = "quit" + "\r\n";
Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
ns.Close();
}
private List
{
Byte[] outbytes;
string input;
string line = "";
input = "retr " + messagenumber.ToString() + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
List
do
{
line = sr.ReadLine();
msglines.Add(line);
} while (line != ".");
msglines.RemoveAt(msglines.Count - 1);
return msglines;
}
private string GetMessageSubject(List
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("Subject:"))
{
tokens = line.Split(new Char[] { ' ' });
return tokens[1].Trim();
}
}
return "None";
}
private string GetMessageFrom(List
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("From:"))
{
tokens &#61; line.Split(new Char[] { &#39; <&#39; });
return tokens[1].Trim(new Char[] { &#39; <&#39;, &#39;>&#39; });
}
}
return "None";
}
private string GetMessageBody(List
{
string body &#61; "";
string line &#61; " ";
IEnumerator msgenum &#61; msglines.GetEnumerator();
while (line.CompareTo("") !&#61; 0)
{
msgenum.MoveNext();
line &#61; (string)msgenum.Current;
}
while (msgenum.MoveNext())
{
body &#61; body &#43; (string)msgenum.Current &#43; "\r\n";
}
return body;
}
private void DeleteMessage(int messagenumber)
{
Byte[] outbytes;
string input;
try
{
input &#61; "dele " &#43; messagenumber.ToString() &#43; "\r\n";
outbytes &#61; System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
}
catch (Exception e)
{
return;
}
}
#endregion
#endregion
}
#endregion
方法二
一个asp.net 版本的邮件接收程序&#xff0c;我们知道&#xff0c;在asp&#43;中发送Email是见很方便的事情,可是怎么进行收取pop信件的程序呢&#xff1f;看来只有拿出豆腐的杀手剑了&#xff1a;&#xff09;
首先我们来看看这个程序的代码&#xff1a;
pop.aspx
<%&#64; Assembly Name&#61;"System.Net" %>
<%&#64; Import Namespace&#61;"System.Net" %>
<% &#64;Import Namespace&#61;"System.Net.Sockets" %>
<%&#64; Import Namespace&#61;"System.IO" %>
好了大家通过这个程序&#xff0c;首先可以了解到pop(Post Ofice Protocal)协议&#xff0c;其次可以加深对asp.net的socket 程序的
理解&#xff0c;还可以通过这个程序接收ISP提供的没有Web方式收取邮件的程序对ISP的信箱进行管理。