首页 > 代码库 > IMAP收邮件

IMAP收邮件

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 LumiSoft.Net.IMAP;using LumiSoft.Net.IMAP.Client;using System.IO;using LumiSoft.Net.Mail;using System.Net.Mime;using System.Net.Mail;using LumiSoft.Net.MIME;namespace EMailTest{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Handle(object sender, LumiSoft.Net.EventArgs<IMAP_r_u> e)        {            Console.WriteLine(e.Value.ToString());        }        private void button1_Click(object sender, EventArgs oe)        {            IMAP_Client client = new IMAP_Client();            try            {                //连接邮件服务器通过传入邮件服务器地址和用于IMAP协议的端口号                //SSL 993 Other 143                //client.Connect("imap.qq.com", 993, true);                //client.Login("4587405@qq.com", "*******");                client.Connect("imap.163.com", 143, false);                client.Login("ylx-1982@163.com", "*****");                client.GetFolders(null).ToList().ForEach(f => {                    Console.WriteLine(f.FolderName);                    var s = client.FolderStatus(f.FolderName);                    s.ToList().ForEach(sIt => {                        Console.WriteLine("总数:{0},未读:{1},最近{2}", sIt.MessagesCount, sIt.MessagesCount, sIt.UnseenCount);                    });                });                                            client.SelectFolder("INBOX");                               var seqSet = IMAP_t_SeqSet.Parse("1000:*");                var items = new IMAP_t_Fetch_i[]                {                    new IMAP_t_Fetch_i_Envelope(),                    new IMAP_t_Fetch_i_Uid(),                    new IMAP_t_Fetch_i_Flags(),                    new IMAP_t_Fetch_i_InternalDate(),                    new IMAP_t_Fetch_i_Rfc822()                };                //Fetch 第一个参数false时seqSet有效                client.Fetch(false, seqSet, items, (s, e) =>                {                    try                    {                                                var email = e.Value as IMAP_r_u_Fetch;                        //using (var ctx = new DBTEntities())                        //{                        //    var ent = new T_EMail();                        //    ent.Flags = email.Flags.Flags.ToString();                        //    ent.ReceiveDate = email.InternalDate.Date;                        //    ent.Subject = email.Envelope.Subject;                        //    ent.UId = email.UID.UID;                        //    ctx.T_EMail.AddObject(ent);                        //    ctx.SaveChanges();                        //}                        Console.WriteLine(" ");                        Console.WriteLine("标题:" + email.UID.UID +"," + email.InternalDate.Date   +"," + email.Envelope.Subject );                        Console.WriteLine("------------内容------------------------");                        if (email.Rfc822 != null)                        {                            email.Rfc822.Stream.Position = 0;                            var mine = Mail_Message.ParseFromStream(email.Rfc822.Stream);                            email.Rfc822.Stream.Close();                            //Console.WriteLine(mine.BodyHtmlText);                            //Console.WriteLine(mine.Body.MediaType);                            if (mine.Attachments.Count() > 0)                            {                                                                var list= mine.Attachments.ToList();                                foreach (var att in list)                                {                                    var part = att.Body as MIME_b_SinglepartBase;                                    string filename=@"C:\xx\" + att.ContentType.Param_Name;                                    File.WriteAllBytes(filename, part.Data);                                                                                                }                                                           }                        }                    }                    catch (Exception ex)                    {                        Console.WriteLine("Handle-Err:" + ex.Message);                    }                                    });            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {            }        }    }}
View Code

 

IMAP收邮件