www.pudn.com > IntMail.rar > Form1.cs


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using LumiSoft.Net.Mime; 
using LumiSoft.Net.POP3; 
using LumiSoft.Net.POP3.Client; 
namespace IntMail 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void 新建邮件ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmSend _fmSend = new FrmSend("","","",2); 
            _fmSend.ShowDialog(); 
        } 
 
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            Application.Exit(); 
        } 
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            Application.Exit(); 
        } 
 
        private void 邮件账户ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            MailOption _mail = new MailOption(); 
            _mail.ShowDialog(); 
        } 
 
        private void 通讯录ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmAddressList _fmlist = new FrmAddressList(); 
            _fmlist.Show(); 
        } 
 
        private void toolStripButton1_Click(object sender, EventArgs e) 
        { 
            //FrmSend _fmsend = new FrmSend(); 
            //_fmsend.Show(); 
        } 
 
        private void 当前帐户设置ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmDefaultSet _fmset = new FrmDefaultSet(this); 
            _fmset.ShowDialog(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
             
            try 
            { 
                CommonMail.init(); 
                DataSet ds = new DataSet(); 
                ds = CommonMail.Search("select * from MailAddress"); 
                if (ds.Tables.Count > 0) 
                { 
                    if (ds.Tables[0].Rows.Count > 0) 
                    { 
                        MailAddress.MailAddressD = ds.Tables[0].Rows[0]["MailAddress"].ToString(); 
                        MailAddress.MailAddressPOP3 = ds.Tables[0].Rows[0]["MailAddressPOP3"].ToString(); 
                        MailAddress.MailAddressSMTP = ds.Tables[0].Rows[0]["MailAddressSMTP"].ToString(); 
                        MailAddress.MailAddressUser = ds.Tables[0].Rows[0]["MailAddressUser"].ToString(); 
                        MailAddress.MailAddressPass = ds.Tables[0].Rows[0]["MailAddressPass"].ToString(); 
                        this.Text = "IntMail邮件系统    当前邮箱:"+MailAddress.MailAddressD; 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("数据文件出现异常"); 
            } 
        } 
 
        private void toolStripButton6_Click(object sender, EventArgs e) 
        { 
            listView1.Items.Clear(); 
            List x = GetEmails(MailAddress.MailAddressPOP3, MailAddress.MailAddressUser, MailAddress.MailAddressPass); 
            foreach (Mime y in x) 
            {               
                try 
                { 
                  //  MessageBox.Show(); 
                    ListViewItem L1 = new ListViewItem(new string[] { y.Attachments.Length>0?"附件":"", FormatString(y.MainEntity.From.ToAddressListString()), y.MainEntity.Subject.ToString(), y.MainEntity.Date.Date.ToString() }, -1); 
                    if (y.Attachments.Length > 0) 
                    { 
                        y.Attachments[0].DataToFile("d.jpg"); 
                    } 
                    listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] { L1 }); 
                    listView1.Items[listView1.Items.Count - 1].Tag = (object)y.BodyText; 
                    //MessageBox.Show(y.MainEntity.From.ToAddressListString()); 
                } 
                catch (Exception ex) 
                { 
                    MessageBox.Show(ex.Message); 
                } 
                } 
        } 
        
        //得到邮件 
        public List GetEmails(string pop1,string username1,string pass) 
        { 
            //需要首先设置这些信息 
            string pop3Server = "";    //邮箱服务器 如:"pop.sina.com.cn";或 "pop.tom.com" 好像sina的比较快 
            int pop3Port = 110;          //端口号码   用"110"好使。最好看一下你的邮箱服务器用的是什么端口号 
            bool pop3UseSsl = false; 
            string username = "";        //你的邮箱用户名 
            string password = "";      //你的邮箱密码 
            List gotEmailIds = new List(); 
 
            List result = new List(); 
            using (POP3_Client pop3 = new POP3_Client()) 
            { 
                try 
                { 
                    //与Pop3服务器建立连接 
                    pop3.Connect(pop1 , pop3Port, pop3UseSsl); 
                    //验证身份 
                    pop3.Authenticate(username1, pass, false); 
 
                    //获取邮件信息列表 
                    POP3_ClientMessageCollection infos = pop3.Messages; 
 
                    foreach (POP3_ClientMessage info in infos) 
                    { 
                        //每封Email会有一个在Pop3服务器范围内唯一的Id,检查这个Id是否存在就可以知道以前有没有接收过这封邮件 
                        if (gotEmailIds.Contains(info.UID)) 
                            continue; 
 
                        //获取这封邮件的内容 
                        byte[] bytes = info.MessageToByte(); 
                        //记录这封邮件的Id 
                        gotEmailIds.Add(info.UID); 
 
                        //解析从Pop3服务器发送过来的邮件信息 
                        Mime mime = Mime.Parse(bytes); 
 
                        result.Add(mime); 
                    } 
                } 
                catch (Exception ex) 
                { 
                    throw new Exception(ex.Message); 
                } 
            } 
            return result; 
        } 
 
        private void listView1_DoubleClick(object sender, EventArgs e) 
        { 
            textBox1.Text=""; 
            textBox1.Text = (string)listView1.FocusedItem.Tag; 
        } 
 
        private void 查看ToolStripMenuItem1_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                textBox1.Text = ""; 
                textBox1.Text = (string)listView1.FocusedItem.Tag; 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("没有选中要查看的信件"); 
            } 
        } 
        private string FormatString(string Code) 
        { 
            string Fname=""; 
            try 
            { 
              //将code以B?分开 
             //  Fname=Code.Split('?')[4].ToString().Split('<')[1].ToString().Split('>')[0].ToString(); 
                Fname = Code.Split('<')[1].ToString().Split('>')[0].ToString(); 
                //将code以?=分开 
               //将code以<> 
                return Fname; 
            } 
            catch (Exception ex) 
            { 
                return Fname; 
            } 
        } 
        //Base64解码 
        private String deCodeB64(String strSrc) 
        { 
            try 
            { 
                if (strSrc != "") 
                { 
                    byte[] by = Convert.FromBase64String(strSrc); 
                    strSrc = Encoding.Default.GetString(by); 
                } 
            } 
            catch (Exception ex) 
            { return ex.ToString(); } 
            return strSrc; 
        } 
 
        private void toolStripButton2_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                listView1.FocusedItem.SubItems[1].ToString(); 
                FrmSend _fmSend = new FrmSend(listView1.FocusedItem.SubItems[1].Text, listView1.FocusedItem.SubItems[2].Text, (string)listView1.FocusedItem.Tag, 0); 
                _fmSend.ShowDialog(); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("请选项择你要回复的信件!","提示"); 
            } 
        } 
 
        private void toolStripButton3_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                listView1.FocusedItem.SubItems[1].ToString(); 
                FrmSend _fmSend = new FrmSend(listView1.FocusedItem.SubItems[1].Text, listView1.FocusedItem.SubItems[2].Text, (string)listView1.FocusedItem.Tag, 1); 
                _fmSend.ShowDialog(); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("请选项择你要回复的信件!", "提示"); 
            } 
        } 
 
        private void toolStripButton5_Click(object sender, EventArgs e) 
        { 
            MessageBox.Show("请关帮助你访问:www.int-soft.cn"); 
        }    
 
 
 
 
    } 
}