Ejemplo n.º 1
0
        // IMAP: GGet all messages in server (HostPOP) and after that, delete all od messages in that server
        public int GetMailIMAPDelete(string FolderName, string HostIMAP, int PortIMAP, bool SSLSupport, string MailName, string PWstring)
        {
            if (!imapConnect())
                return -1;

            //choose current folfer
            if (!imapClient.SelectMailbox(FolderName))
            {
                // if folder is not exist, create new folder
                imapClient.CreateMailbox(FolderName);
                imapClient.SelectMailbox(FolderName);
            }

            Imap subimapClient = new Imap();
            if (!subimapClient.UnlockComponent("IMAP-TEAMBEAN_17BDEB05021V"))
                return -1;
            //config info of imap server
            if (SSLSupport)      //if SSL support
                subimapClient.Ssl = true;
            else
                subimapClient.Ssl = false;
            subimapClient.Port = PortIMAP;
            //connect
            subimapClient.ConnectTimeout = 15;  // defaulf timeout = 30, so when it can't connect, it's too slow
            if (!subimapClient.Connect(HostIMAP))
                return -1;   // if can't connect to imap server

            if (!subimapClient.Login(MailName, PWstring))
                return -1;   // if can't login to imap server

            // choose folder Inbox on imap server
            // it's a little complex because the name of Inbox folder is diffirent on each imap server, such as INBOX, InBox, or InBox
            Mailboxes mailbox = subimapClient.ListMailboxes("", "*");
            for (int i = 0; i < mailbox.Count; i++)
            {
                if (mailbox.GetName(i).ToUpper() == "INBOX")
                {
                    if (!subimapClient.SelectMailbox(mailbox.GetName(i)))
                        return -1;
                    break;
                }
            }

            MessageSet OLdMSSet = imapClient.Search("ALL", true);   //info of all old messages in our server
            MessageSet MSSet = subimapClient.Search("ALL", true);   //info of all  messages in imap server
            if (MSSet.Count != 0)
            {
                // add new message
                for (int i = 0; i < MSSet.Count; i++)
                {
                    Email mail = subimapClient.FetchSingle(MSSet.GetId(i), true);
                    imapClient.AppendMail(FolderName, mail);
                }

                //delete message on server
                subimapClient.SetFlags(MSSet, "Deleted", 1);
                subimapClient.Expunge();

                // mark new message is unread
                imapClient.SelectMailbox(FolderName);
                MessageSet NewMSSet = imapClient.Search("ALL", true);
                for (int i = 0; i < OLdMSSet.Count; i++)
                    NewMSSet.RemoveId(OLdMSSet.GetId(i));
                imapClient.SetFlags(NewMSSet, "Seen", 0);
                //Email agwag = imapClient.FetchSingle(NewMSSet.GetId(0), true);
            }
            subimapClient.Disconnect();
            imapDisconnect();
            return MSSet.Count;
        }