C#使用 LumiSoft.Net.dll 通过IMAP 登陆QQ邮箱读取未读邮件

一、首先,需要需要登陆的QQ邮箱账号开启IMAP功能,进入邮箱设置即可,开启IMAP功能后会获取一串密码,这是接下来登陆需要的密码,请务必开启;

二、需要LumiSoft.Net.dll;

三、LumiSoft.Net.dll的开发环境是.Net 2.0 使用Microsoft Visual Studio 系列工具时请注意更改项目环境;Ps:.Net 2.0 不支持 linq


1、声明一个

IMAP_Client client = new IMAP_Client(); 

实例化client对象;

2、登陆邮箱

                client.Connect("imap.qq.com", 993, true);
                string path = System.Environment.CurrentDirectory + "//user.txt";
                if (!File.Exists(path)) {
                    Console.WriteLine("请在配置文件中修改账号密码。");
                    return;
                }
                string emailAccount;
                string password;
                using (FileStream fs = new FileStream(path, FileMode.Open)) {
                    StreamReader sr = new StreamReader(fs);
                    emailAccount = sr.ReadLine();
                    password = sr.ReadLine();
                }

                Console.WriteLine(emailAccount + password);
                client.Login(emailAccount, password);
                client.SelectFolder("INBOX");
博主使用的文件读取账号和密码方便修改,省的改代码;

3、获取邮件参数设置

                var seqSet = IMAP_t_SeqSet.Parse("1:*");
                seqSet.GetType();
                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(),//邮件标记-----已读:/Seen(还是\Seen,记不太清了,试一下就成) 未读:不知道...
                    new IMAP_t_Fetch_i_InternalDate(),
                    new IMAP_t_Fetch_i_Rfc822()//获取邮件内容需要的参数----未知含义
                };
4、Fetch读取邮件

                //Fetch 第一个参数false时seqSet有效
                client.Fetch(false, seqSet, items, (s, ex) =>
                {
                    try
                    {
                        var email = ex.Value as IMAP_r_u_Fetch;

                        if (!email.Flags.Flags.ToString().Equals("\\Seen"))
                        {

                            if (email.Envelope.Subject.Contains("交易成功"))
                            {
                                Console.WriteLine(email.Flags.Flags);
                                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();
                                    if (toolBox.findData(mine.BodyHtmlText))//mine.BodyHtmlText是邮件正文
                                    {
                                        delMail(email.UID.UID);
                                    }
                                }
                            }
                            else
                            {
                                delMail(email.UID.UID);
                            }
                        }

                    }
                    catch (Exception exx)
                    {
                        Console.WriteLine("Handle-Err:" + exx.Message);
                    }

                });

5、删除邮箱 
  

            try
            {
                var sequenceSet = new IMAP_SequenceSet();
                sequenceSet.Parse(uid.ToString());
                client.StoreMessageFlags(true, sequenceSet, IMAP_Flags_SetType.Add, IMAP_MessageFlags.Deleted);
                Console.WriteLine("----------------");
                client.Expunge();//执行删除
            }
            catch (Exception x)
            {
                Console.WriteLine(x.ToString());
                Console.WriteLine(x.Message);
            }
over.

你可能感兴趣的:(C#)