|
本文涉及的内容: 1:自己制作Pop3Helper 信件格式的处理有麻烦 2:使用现成的pop3类 LumiSoft.Net.POP3.Client; 有两种处理方法 3:使用IMAP收邮件 功能似乎更多,比起pop3来。 4:SMTP发送邮件 关于 Mailbox unavailable. The server response was: 5.7.1 Unable to relay for xx 的错误处理
自己写一个POP3的接收程序并不是很简单。主要问题就是如何处理信件的格式。 其处理方法不是太复杂,用一个tcp联接就可以了。 这是代码 public class Pop3Helper { string _pop3server; string _user; int _port; string _pwd;
public TcpClient _server; public NetworkStream _netStream; public StreamReader _reader; public string _data; public byte[] _charData; public string _CRLF = "\r\n";
private string _log; public string LogMSG { get { return _log; } } /// <summary> /// /// </summary> /// <param name="server"></param> /// <param name="port"></param> /// <param name="user"></param> /// <param name="pwd"></param> public Pop3Helper(string server, int port, string user, string pwd) { _pop3server = server; _port = port; _user = user; _pwd = pwd; } /// <summary> /// connect /// </summary> public void Connect() { //create a tcp connection _server = new TcpClient(_pop3server, _port);
//prepare _netStream = _server.GetStream(); _reader = new StreamReader(_server.GetStream()); if (!CheckResult(_reader.ReadLine())) throw new Exception("Connect Error");
//login _data = "USER " + this._user + _CRLF; _charData = System.Text.Encoding.ASCII.GetBytes(_data.ToCharArray()); _netStream.Write(_charData, 0, _charData.Length); if (!CheckResult(_reader.ReadLine())) throw new Exception("User Error");
_data = "PASS " + this._pwd + _CRLF; _charData = System.Text.Encoding.ASCII.GetBytes(_data.ToCharArray()); _netStream.Write(_charData, 0, _charData.Length); if (!CheckResult(_reader.ReadLine())) throw new Exception("Pass Error");
} /// <summary> /// get message Numbers /// </summary> /// <returns></returns> public int GetMailCount() { try { _data = "STAT" + _CRLF; _charData = System.Text.Encoding.ASCII.GetBytes(_data.ToCharArray()); _netStream.Write(_charData, 0, _charData.Length); string resp = _reader.ReadLine(); string[] tokens = resp.Split(new char[] { ' ' }); return Convert.ToInt32(tokens[1]); } catch (Exception ex) { return 0; } }
public string GetMail(int id) { string line; string content = ""; try { //get by id _data = "RETR " + id + _CRLF; _charData = System.Text.Encoding.ASCII.GetBytes(_data.ToCharArray()); _netStream.Write(_charData, 0, _charData.Length); line = _reader.ReadLine();
if (line[0] != '-') { //end with '.' while (line != ".") { line = _reader.ReadLine(); content += line + "\r\n"; } }
return content;
}
catch (Exception err) { Log(err.Message); return "Error"; } } public void DeleteMail(int id) { _data = "DELE" + id + _CRLF; _charData = System.Text.Encoding.ASCII.GetBytes(_data.ToCharArray()); _netStream.Write(_charData, 0, _charData.Length); if (!CheckResult(_reader.ReadLine())) throw new Exception("Delete Error");
} /// <summary> /// close connection /// </summary> public void Close() {
_data = "QUIT" + _CRLF; _charData = System.Text.Encoding.ASCII.GetBytes(_data.ToCharArray()); _netStream.Write(_charData, 0, _charData.Length);
//close _netStream.Close(); _reader.Close(); }
private bool CheckResult(string reply) { Log(reply); if (reply.IndexOf("+OK") > -1) return true; else return false; } private void Log(string msg) { _log += msg + "\r\n"; } } 。。。。。
共2页: 上一页 1 [2] 下一页
|