读书人

急接收邮件登陆的有关问题。全代码。

发布时间: 2012-12-28 10:29:04 作者: rapoo

急。。。。。。在线等。。。。接收邮件登陆的问题。。。。全代码。。
本帖最后由 kingboy_321 于 2012-12-13 19:59:17 编辑 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class frmjieshou : Form
{
public frmjieshou()
{
InitializeComponent();
}
public static string strserver;
public static string pwd;
public static int k;
public static TcpClient tcpclient;
public static StreamReader sreader;
public static string strret;
private string sendpopcmd(TcpClient tcpclient, string strcmd)
{
Byte[] arrcmd;
string strret;
Stream stream;
stream = tcpclient.GetStream();
strcmd = strcmd + "\r\n";
arrcmd = Encoding.Default.GetBytes(strcmd.ToCharArray());
stream.Write(arrcmd, 0, strcmd.Length);
strret = sreader.ReadLine();
return strret;
}
private string logon(TcpClient tcpclient, string user, string pass)
{
string strret;
strret = sendpopcmd(tcpclient, "user" + user);
strret = sendpopcmd(tcpclient, "pass" + pass);
return strret;
}
private string[] staticmailbox(TcpClient tcpclient)
{
string strret;
strret = sendpopcmd(tcpclient, "stat");
if (judgestring(strret) != "+OK")


{
return "-ERR-ERR".Split("".ToCharArray());
}
else
{
string[] arrret = strret.Split("".ToCharArray());
return arrret;
}
}
private string judgestring(string strcheck)
{
if (strcheck.Substring(0, 3) != "+OK")
{
return "-ERR";
}
else
{
return "+OK";
}
}
private string[] popmail(TcpClient tcpclient, int i)
{
string strret;
string[] arrret = new string[20];
bool strbody = false;
string[] arrtemp;
strret = sendpopcmd(tcpclient, "retr" + i.ToString());
if (judgestring(strret) != "+OK")
{
return "-ERR-ERR".Split("".ToCharArray());
}
arrret[0] = "+OK";
while (sreader.Peek() != 46)
{
strret = sreader.ReadLine();
arrtemp = strret.Split(":".ToCharArray());
if (strret == "")
{
strbody = true;


}
if (arrtemp[0].ToLower() == "date")
{
arrret[1] = arrtemp[1];
}
if (arrtemp[0].ToLower() == "from")
{
arrret[2] = (arrtemp[1].Substring(arrtemp[1].LastIndexOf("<") + 1)).Replace(">", "");
}
if (arrtemp[0].ToLower() == "to")
{
arrret[3] = (arrtemp[1].Substring(arrtemp[1].LastIndexOf("<") + 1)).Replace(">", "");
}
if (arrtemp[0].ToLower() == "subject")
{
arrret[4] = arrtemp[1].ToString();
}
if (strbody)
{
arrret[5] = arrret[5] + strret + "\n";
}

} return arrret;
}
public static string base64decode(string str)
{
return Encoding.UTF8 .GetString (Convert .FromBase64String (str));
}

private void btnlogin_Click(object sender, EventArgs e)
{
string user = txtmail.Text;
string pass = txtpwd.Text;
string[] arrret;
pwd = pass;


strserver = txtpop.Text;
tcpclient = new TcpClient();
try
{
tcpclient.Connect(strserver, 110);
sreader = new StreamReader(tcpclient.GetStream(), Encoding.Default);
sreader.ReadLine();
strret = logon(tcpclient, user, pass);
if (judgestring(strret) != "+OK")
{
MessageBox.Show("用户名或密码错误");
return;
}
arrret = staticmailbox(tcpclient);
if (arrret[0] != "+OK")
{
MessageBox.Show("出错了");
return;
}
textBox1.AppendText("当前用户:" + user + "\n");
textBox1.AppendText("邮箱中共有:" + arrret[1] + "封邮件" + "\n");
textBox1.AppendText("共占:" + arrret[2] + "Byte");
k = Convert.ToInt32(arrret[1]);
txtpop.ReadOnly = txtmail.ReadOnly = txtpwd.ReadOnly = true;
btnlogin.Enabled = false;
btnreveive.Enabled = true;
MessageBox.Show("登陆成功");
}
catch
{
MessageBox.Show("登陆失败,连接服务器错误");
}


}

private void btnout_FormClosed(object sender, EventArgs e)
{
try
{
tcpclient.Close();
}
catch { }
}

private void btnreveive_Click(object sender, EventArgs e)
{
try
{
if (txtnum.Text != "")
{
if (Convert.ToInt32(txtnum.Text) > k || Convert.ToInt32(txtnum.Text) <= 0)
{
MessageBox.Show("输入的索引错误");
}
else
{
textBox1.Clear();
string[] arrrets;
arrrets = popmail(tcpclient, Convert.ToInt32(txtnum.Text));
textBox1.AppendText("当前是第" + txtnum.Text + "封信" + "\n");
textBox1.AppendText("邮件日期" + arrrets[1] + "\n");
textBox1.AppendText("发信人" + arrrets[2] + "\n");
textBox1.AppendText("收信人" + arrrets[3] + "\n");
textBox1.AppendText("邮件主题" + base64decode(arrrets[4]) + "\n");
textBox1.AppendText("邮件内容" + base64decode(arrrets[5]) + "\n");


}
}
else
{
MessageBox.Show("邮件索引失败");
}
}
catch { }
}
}
}

自检通过,可以建立连接但是用户名密码错误。我是新申请的用户名和密码怎么能错呢。还是程序有问题。请各位大哥指导。。。。。在线跪等。。。。。。
[解决办法]

引用:
大哥们这个问题还没有解决呢,谁能帮个忙啊


http://www.lumisoft.ee/lswww/download/downloads/Examples/pop3_client_app.zip
这个就是你想要的了,

读书人网 >C#

热点推荐