如题!谢谢
解决方案 »
- PerformanceCounter 输入字符串的格式不正确。
- 本地c++ 调用c#写的COM组件 问题
- C#中,赋值与运算的区别--->请看别人写的代码,我不知道为什么要这样写?
- 寻找 《c#/Visual Basic.net与数据库程序设计》 光盘例子
- WebBrowser控件造成死机?
- 关于C#加密解密
- 小问题,脑子乱了 特来求助!
- 在。net环境利用数据窗体向导生成数据窗口,选择表之后出现下列的错误提示。
- SQL Server Configuration Manager无法启动
- 关于TREEVIEW的设置选中指定节点的问题,急!!!
- tabControl1控制问题
- 今天开始开发Winform程序,请问买什么书看容易上手?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;namespace POP3邮件接收系统
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
internal System.Windows.Forms.NumericUpDown NumericUpDown1;
internal System.Windows.Forms.Label Label8;
internal System.Windows.Forms.Label Label7;
internal System.Windows.Forms.TextBox TextBox1;
internal System.Windows.Forms.GroupBox groupBox2;
internal System.Windows.Forms.Label Label6;
internal System.Windows.Forms.TextBox txtContent;
internal System.Windows.Forms.TextBox txtSubject;
internal System.Windows.Forms.Label label5;
internal System.Windows.Forms.Label label4;
internal System.Windows.Forms.GroupBox groupBox1;
internal System.Windows.Forms.Button button1;
internal System.Windows.Forms.Label label1;
internal System.Windows.Forms.TextBox txtServer;
internal System.Windows.Forms.Label label2;
internal System.Windows.Forms.TextBox txtMailBox;
internal System.Windows.Forms.TextBox txtPwd;
internal System.Windows.Forms.Label label3;
internal System.Windows.Forms.Button button3;
internal System.Windows.Forms.TextBox txtFrom;
internal System.Windows.Forms.Button button4;
internal System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
private NetworkStream ns;
private TcpClient client=new TcpClient();
public Form1()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
client.Connect(txtServer.Text,110);
ns=client.GetStream();
string msg=Receivemsg();
bool a=b(msg);
if(!a)
{
MessageBox.Show("连接失败~~~~~!!");
return;
}
SendCommand("USER "+txtMailBox.Text); msg=Receivemsg();
a=b(msg);
if(!a)
{
MessageBox.Show("邮件地址有误!!!");
return;
}
SendCommand("PASS "+txtPwd.Text);
msg=Receivemsg();
a=b(msg);
if(!a)
{
MessageBox.Show("密码错误!!!!");
return;
}
SendCommand("STAT"); msg=Receivemsg();
a=b(msg);
if(!a)
{
MessageBox.Show("失败!!!!");
return;
}
string[] s=msg.Split(' ');
TextBox1.Text=s[1];
NumericUpDown1.Maximum=Convert.ToInt32(TextBox1.Text);
MessageBox.Show("连接成功!!!~");
button2.Enabled=true;
}
catch(Exception qq)
{
MessageBox.Show(qq.Message);
}
}
private void SendCommand(string cmd)
{
byte[] data=System.Text.Encoding.Default.GetBytes(cmd+"\r\n");
ns.Write(data,0,data.Length);
ns.Flush();
} private string Receivemsg()
{
byte[] data=new byte[1024];
int l=ns.Read(data,0,data.Length);
string msg=System.Text.Encoding.Default.GetString(data,0,l);
return msg;
} private bool b(string str)
{
int i=str.IndexOf("+OK");
if(i<0)
return false;
else
return true;
} private void button3_Click_1(object sender, System.EventArgs e)
{
try
{
string str="";
SendCommand("RETR "+NumericUpDown1.Value.ToString());
string msg=Receivemsg();
str=msg;
while(msg.IndexOf("\r\n.\r\n")<0)
{
msg=Receivemsg();
str=msg;
}
int start=str.IndexOf("From:")+5;
int end=str.IndexOf("\r\n",start);
string mailFrom=str.Substring(start,end-start);
txtFrom.Text=mailFrom;
start=str.IndexOf("Subject:")+8;
end=str.IndexOf("\r\n",start);
string subject=str.Substring(start,end-start);
txtSubject.Text=subject;
start=str.IndexOf("\r\n\r\n");
string content=str.Substring(start);
txtContent.Text=content;
}
catch(Exception qq)
{
MessageBox.Show(qq.Message);
}
} private void button2_Click(object sender, System.EventArgs e)
{
try
{
SendCommand("QUIT"); ns.Close();
client.Close();
}
catch(Exception qq)
{
MessageBox.Show(qq.Message);
}
} private void button4_Click(object sender, System.EventArgs e)
{
try
{
SendCommand("DELE "+NumericUpDown1.Value.ToString());
Receivemsg();
NumericUpDown1.Value--;
}
catch(Exception qq)
{
MessageBox.Show(qq.Message);
}
}
}
}
楼主自己看吧,这是个POP3邮件接受系统的代码,红色标记的的是与服务器连接的重点