示例#1
0
 public void init()
 {//Get unread message list from GMail
     using (ImapClient cl = new ImapClient("imap.yandex.ru"))
     {
         cl.Port     = 993;
         cl.Ssl      = true;
         cl.UserName = "******";
         cl.Password = "******";
         var bl = cl.Authenticate();
         if (bl == true)
         {
             //Select folder
             ImapFolder folder = cl.SelectFolder("INBOX");
             DateTime   time;
             //Search Unread
             SearchResult list = cl.ExecuteSearch("UNSEEN UNDELETED");
             List <HigLabo.Mime.MailMessage> mg = new List <HigLabo.Mime.MailMessage>();
             //Get all unread mail
             for (int i = 0; i < list.MailIndexList.Count; i++)
             {
                 mg.Add(cl.GetMessage(list.MailIndexList[i]));
             }
             for (int i = 0; i < list.MailIndexList.Count; i++)
             {
                 time = mg[i].Date.LocalDateTime;
             }
         }
         //Change mail read state as read
         //  cl.ExecuteStore(1, StoreItem.FlagsReplace, "UNSEEN");
     }
 }
        /// <summary>
        /// Creates <see cref="ActivityFolder"/> instance for <paramref name="folder"/>.
        /// </summary>
        /// <param name="folder"><see cref="ImapFolder"/> instance.</param>
        /// <returns><see cref="ActivityFolder"/> instance unique identifier.</returns>
        private Guid CreateActivityFolder(ImapFolder folder)
        {
            EntitySchema schema         = _userConnection.EntitySchemaManager.GetInstanceByName("ActivityFolder");
            Entity       activityFolder = schema.CreateEntity(_userConnection);

            activityFolder.SetDefColumnValues();
            activityFolder.SetColumnValue("Name", folder.ShortName);
            activityFolder.SetColumnValue("FolderTypeId", FolderConsts.EmailFolderTypeId);
            activityFolder.SetColumnValue("ParentId", RootActivityFolderId);
            activityFolder.Save();
            return(activityFolder.PrimaryColumnValue);
        }
        /// <summary>
        /// Creates <see cref="MailboxFoldersCorrespondence"/> instance for <paramref name="folder"/> if needed.
        /// </summary>
        /// <param name="folder"><see cref="ImapFolder"/> instance.</param>
        private void ActualizeMailboxFoldersCorrespondence(ImapFolder folder)
        {
            Guid folderId = GetFolderId(folder);

            if (folderId.IsEmpty())
            {
                Guid         activityFolderId = CreateActivityFolder(folder);
                EntitySchema schema           = _userConnection.EntitySchemaManager
                                                .GetInstanceByName("MailboxFoldersCorrespondence");
                Entity correspondence = schema.CreateEntity(_userConnection);
                correspondence.SetDefColumnValues();
                correspondence.SetColumnValue("FolderPath", folder.RawName);
                correspondence.SetColumnValue("UId", "0");
                correspondence.SetColumnValue("MailboxId", CurrentMailboxSettingsId);
                correspondence.SetColumnValue("ActivityFolderId", activityFolderId);
                SaveEntity(correspondence);
            }
        }
示例#4
0
        public static List <ImapFolder> ToImapFolders(User user, List <Folder> folders)
        {
            var attributes = new List <string>();

            attributes.Add("\\NoInferiors");

            List <ImapFolder> AllImapFolder = new List <ImapFolder>();

            foreach (var item in folders)
            {
                char delimiter = default(char);
                if (item.Name.Contains("/"))
                {
                    delimiter = '/';
                }

                var newfolder = new ImapFolder()
                {
                    Name = item.Name, Delimiter = delimiter, Attributes = attributes
                };

                if (item.Name.Contains("@"))
                {
                    newfolder.Attributes = attributes;
                }
                AllImapFolder.Add(newfolder);
            }

            var orgdb = Factory.DBFactory.OrgDb(user.CurrentOrgId);

            var address = orgdb.EmailAddress.Query().Where(o => o.UserId == user.Id).SelectAll();

            var defaults = GetDefaultFolders(address);

            foreach (var item in defaults)
            {
                if (!AllImapFolder.Contains(item))
                {
                    AllImapFolder.Add(item);
                }
            }
            return(AllImapFolder);
        }
示例#5
0
        public string GetTrubleMessageText(string sender, string subject, DateTime localDate)
        {
            string result = null;
            string server = ImapServer;


            try
            {
                using (HigLabo.Net.Imap.ImapClient cl = new HigLabo.Net.Imap.ImapClient(server))
                {
                    cl.UserName = Email;
                    cl.Password = Password;
                    cl.Port     = ImapPort;
                    cl.Ssl      = ImapSSL;
                    var bl = cl.Authenticate();

                    if (bl == true)
                    {
                        ImapFolder               folder = cl.SelectFolder("INBOX");
                        SearchResult             list   = cl.ExecuteSearch("UNSEEN UNDELETED");
                        HigLabo.Mime.MailMessage mg     = null;
                        for (int i = 0; i < list.MailIndexList.Count; i++)
                        {
                            mg = cl.GetMessage(list.MailIndexList[i]);
                            if (mg.Date.LocalDateTime == localDate && mg.From.Value.Equals(sender) && mg.Subject.Trim().Equals(subject))
                            {
                                break;
                            }
                        }

                        if (mg != null && mg.BodyText != null)
                        {
                            result = mg.BodyText;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error("Ошибка при повторной загрузке текста сообщения с почтового ящика " + (Email ?? "") + ".\r\nАдрес сервера:" + (server ?? "") + ".\r\nУдаленный порт: " + ImapPort + "\r\nШифрование:" + (ImapSSL ? "Включено" : "Выключено") + "\r\nТекст ошибки:", e);
            }
            return(result);
        }
示例#6
0
        private static void DumpMessages(List<string> paramNames, ImapClient client, ImapFolder f, string upperCmd)
        {
            long lowUid = 1;
            long highUid = f.UidNext;

            string dumpRange = upperCmd.Substring("DUMP".Length).Trim();
            var rangeArgs = dumpRange.Split(new char[] { ':', ' ', '-' }, StringSplitOptions.RemoveEmptyEntries);
            if (rangeArgs.Length > 0)
            {
                if (!long.TryParse(rangeArgs[0], out lowUid))
                {
                    lowUid = 1;
                }
            }

            if (rangeArgs.Length > 1)
            {
                if (!long.TryParse(rangeArgs[1], out highUid))
                {
                    highUid = f.UidNext;
                }
            }

            System.Console.WriteLine("Fetching UIDs in range [{0}:{1}] from folder [\"{2}\"]...",
                lowUid, highUid, client.SelectedFolder.Name);

            long[] msUids = client.FetchUids(lowUid, highUid, true).ToArray();

            System.Console.WriteLine("Fetching [{0}] headers...", msUids.Length);

            const int HeaderBlockSize = 1000;

            for (int i = 0; i < msUids.Length; i += HeaderBlockSize)
            {
                int j = i + HeaderBlockSize;

                if (j >= msUids.Length)
                {
                    j = msUids.Length - 1;
                }

                ImapFetchOption opts =
                    ImapFetchOption.Envelope
                    | ImapFetchOption.BodyStructure
                    | ImapFetchOption.Flags;

                ImapMessage[] ms = client.FetchMessages(msUids[i], msUids[j], opts, paramNames).ToArray();

                foreach (ImapMessage m in ms)
                {
                    DumpMessageInfo(m);
                    System.Console.WriteLine();
                }
            }
        }
示例#7
0
        private static void DumpBody(List<string> paramNames, ImapClient client, ImapFolder f, string upperCmd)
        {
            long uid = -1;
            string uidString = upperCmd.Substring("BODY".Length).Trim();
            if (!long.TryParse(uidString, out uid))
            {
                throw new ArgumentException("Must include valid message UID for BODY dump.", "uid");
            }

            var message = client.FetchMessages(uid, uid, ImapFetchOption.BodyStructure).FirstOrDefault();
            if (null == message)
            {
                System.Console.WriteLine("Message with UID [{0}] not found.  Can not dump its body.", uid);
            }
            else
            {
                foreach (var part in message.BodyParts)
                {
                    DumpBodyPart(client, uid, part);
                }
            }
        }
        /// <summary>
        /// Returns id of the <paramref name="folder"/>.
        /// </summary>
        /// <param name="folder"><see cref="ImapFolder"/> instance.</param>
        /// <returns>id of the <paramref name="folder"/>.</returns>
        private Guid GetFolderId(ImapFolder folder)
        {
            var select = SelectFolderFromDB(folder.RawName) as Select;

            return(select.ExecuteScalar <Guid>());
        }
 /// <summary>
 /// Returns true if <paramref name"folder"/> allowed for synchronization.
 /// </summary>
 /// <param name="folder"><see cref="ImapFolder" instance.</param>
 /// <returns>Returns is <paramref name"folder"/> allowed for synchronization.</returns>
 private bool IsFolderAlowedForSync(ImapFolder folder)
 {
     _log.DebugFormat("[{0}] - [{1}] | folder flags are {2}", _currentMailboxName, folder.Name, folder.Flags);
     return((folder.Flags & (FolderFlags.AllMail | FolderFlags.Trash | FolderFlags.Spam | FolderFlags.Drafts |
                             FolderFlags.Noselect)) == 0);
 }
示例#10
0
        internal static List <ImapFolder> GetDefaultFolders(List <EmailAddress> Address)
        {
            List <ImapFolder> result = new List <ImapFolder>();

            foreach (var item in Folder.ReservedFolder)
            {
                // See https://tools.ietf.org/html/rfc6154 and https://tools.ietf.org/html/rfc3348 for attributes extensions
                var name = item.Value.ToLower();
                if (name == "inbox")
                {
                    // Add Inbox
                    result.Add(new ImapFolder
                    {
                        Name       = "INBOX",
                        Attributes = new List <string>
                        {
                            "\\NoInferiors"
                        }
                    });

                    // Add address sub folders to Inbox
                    if (Address != null && Address.Count() > 1)
                    {
                        foreach (var add in Address)
                        {
                            result.Add(new ImapFolder
                            {
                                Name       = item.Value + "/" + add.Address,
                                Delimiter  = '/',
                                Attributes = new List <string>
                                {
                                    "\\NoInferiors"
                                }
                            });
                        }
                    }
                }
                else
                {
                    // Add Trash, Spam, Drafts, Sent folders
                    var folder = new ImapFolder
                    {
                        Name       = item.Value,
                        Attributes = new List <string>
                        {
                            "\\NoInferiors"
                        }
                    };

                    if (name == "spam")
                    {
                        folder.Attributes.Add("\\Junk");
                    }
                    else
                    {
                        folder.Attributes.Add("\\" + item.Value);
                    }
                    result.Add(folder);
                }
            }

            return(result);
        }
示例#11
0
        private static long DownloadMails(DataContext dc, PcapCapture pcap)
        {
            List <string> log      = new List <string>();
            List <string> logError = new List <string>()
            {
                "", ""
            };

            DateTime dtStart = DateTime.UtcNow;

            log.Add("");
            log.Add("LOGIN PARAMS");
            log.Add("Host name: " + dc.HostName + " (resolved ip = " + System.Net.Dns.GetHostEntry(dc.HostName).AddressList.FirstOrDefault() + ") ");
            log.Add("Port: " + dc.Port);
            log.Add("UseSSL: " + dc.UseSSL.ToString());
            log.Add("User name: " + dc.UserName);
            log.Add("User password (Base64): " + Convert.ToBase64String(Encoding.Default.GetBytes(dc.UserPassword)));
            log.Add("");

            if (dc.MergeFolders == false)
            {
                if (File.Exists(dc.DestinationFolder))
                {
                    try
                    {
                        File.Delete(dc.DestinationFolder);
                    }
                    catch (Exception)
                    {
                        File.Move(dc.DestinationFolder, dc.DestinationFolder + "_old");
                    }
                }
            }
            else
            {
            }


            long totalMessagesDownloaded = 0;

            // clone the list
            TotalMails = dc.EmailFolders.Where(o => o.Selected).Sum(o => o.Messages);

            string lastDirName = dc.DestinationFolder.Split('\\', '/').Where(o => !string.IsNullOrEmpty(o)).Last();
            string superDir    = dc.DestinationFolder.Substring(0, dc.DestinationFolder.Length - lastDirName.Length - 1);

            string logFileName = Path.Combine(superDir, lastDirName + ".log");

            if (!Directory.Exists(superDir))
            {
                Directory.CreateDirectory(superDir);
            }

            if (!dc.MergeFolders)
            {
                // Delete previous log file
                if (File.Exists(logFileName))
                {
                    File.Delete(logFileName);
                }
                // Delete previous pcap file
                if (File.Exists(pcap.OutputFile))
                {
                    try
                    {
                        File.Delete(pcap.OutputFile);
                    }
                    catch { }
                }
            }

            pcap.StartCapture();

            bool      downloadFail = false;
            Exception internalEx   = null;

            object writeEntryBlock = new object();

            using (FileStream zipToOpen = new FileStream(dc.DestinationFolder, FileMode.OpenOrCreate))
            {
                ZipArchiveMode openMode = ZipArchiveMode.Create;

                if (File.Exists(dc.DestinationFolder) && dc.MergeFolders)
                {
                    openMode = ZipArchiveMode.Update;
                }

                using (ZipArchive archive = new ZipArchive(zipToOpen, openMode))
                {
                    try
                    {
                        Parallel.ForEach(dc.EmailFolders, new ParallelOptions()
                        {
                            MaxDegreeOfParallelism = dc.ConcurrentThreads
                        }, (folder) =>
                        {
                            if (folder.Selected == false)
                            {
                                return;
                            }

                            // The default port for IMAP over SSL is 993.
                            using (ImapClient client = new ImapClient())
                            {
                                client.ServerCertificateValidationCallback = (s, c, h, ee) => true;

                                try
                                {
                                    client.Connect(dc.HostName, dc.Port, dc.UseSSL);
                                }
                                catch (ImapProtocolException)
                                {
                                    // try twice
                                    System.Threading.Thread.Sleep(100);
                                    client.Connect(dc.HostName, dc.Port, dc.UseSSL);
                                }

                                // wait 10 seconds if error is too many connection
                                var intWaitTime = 10 * 1000;

                                while (true)
                                {
                                    try
                                    {
                                        client.Authenticate(dc.UserName, dc.UserPassword);
                                        break;
                                    }
                                    catch (MailKit.Security.AuthenticationException ex)
                                    {
                                        System.Threading.Thread.Sleep(intWaitTime *= 2);
                                    }

                                    if (intWaitTime > 15 * 60 * 1000)
                                    {
                                        // is waiting time is greather than 15 min I assume the download fails
                                        throw new Exception("Multiple connetion to host fails");
                                    }
                                }

                                ImapFolder imapFodler = (ImapFolder)client.GetFolder(folder.Folder);

                                folder.IsDownloading = true;

                                folder.DownloadedItems = 0;

                                string destZipFolder = folder.Folder.Replace(imapFodler.DirectorySeparator, '\\');
                                // remove wrong chars

                                var illegalChars = Path.GetInvalidFileNameChars().ToList();
                                // remove folder separator
                                illegalChars.Remove('\\');

                                destZipFolder = string.Join("_", destZipFolder.Split(illegalChars.ToArray()));

                                string messageIdSafeName = "";

                                try
                                {
                                    imapFodler.Open(FolderAccess.ReadOnly);
                                }
                                catch (Exception)
                                {
                                    logError.Add("Error: can't select imap folder '" + folder.Folder + "'");
                                    return;
                                }

                                //IList<IMessageSummary> items = imapFodler.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size);
                                IList <IMessageSummary> items = imapFodler.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size | MessageSummaryItems.InternalDate | MessageSummaryItems.Flags);

                                DateTime dt   = DateTime.Now;
                                long fileSize = 0;
                                MimeMessage msg;

                                long folderSize = items.Sum(o => o.Size ?? 0);

                                List <string> AlreadyExistingEntries = new List <string>();
                                if (dc.MergeFolders)
                                {
                                    AlreadyExistingEntries = archive.Entries.Select(o => o.FullName).OrderBy(o => o).ToList();
                                }

                                foreach (var item in items)
                                {
                                    if (dc.MergeFolders)
                                    {
                                        // search entry before start downloading
                                        if (AlreadyExistingEntries.Any(o => o.StartsWith(destZipFolder + "\\" + item.UniqueId + "_")))
                                        {
                                            logError.Add("Log: message id " + item.UniqueId + " already downloaded from folder '" + folder.Folder + "'");
                                            totalMessagesDownloaded++;
                                            folder.DownloadedItems++;
                                            continue;
                                        }
                                        else
                                        {
                                        }
                                    }

                                    dt       = DateTime.Now;
                                    fileSize = 0;

                                    try
                                    {
                                        msg = imapFodler.GetMessage(item.UniqueId);
                                    }
                                    catch
                                    {
                                        // Second attempt
                                        try
                                        {
                                            msg = imapFodler.GetMessage(item.UniqueId);
                                        }
                                        catch (Exception ex)
                                        {
                                            // in the meanwhile a message has been deleted.. sometimes happens
                                            logError.Add("Error: can't download message id " + item.UniqueId + " from folder '" + folder.Folder + "'");
                                            continue;
                                        }
                                    }

                                    ProgressMails++;

                                    if (folder.Selected == false)
                                    {
                                        continue;
                                    }

                                    // msg not exsist
                                    if (msg.From == null)
                                    {
                                        log.Add("Error: can't save message id " + item.UniqueId + " from folder '" + folder.Folder + "' because has no From field");
                                        continue;
                                    }

                                    totalMessagesDownloaded++;
                                    folder.DownloadedItems++;

                                    messageIdSafeName = System.Text.RegularExpressions.Regex.Replace(msg.Headers["Message-ID"] + "", "[<>\\/:]", "");

                                    string msgPrefix = item.UniqueId + "";

                                    if (item.Flags != null && !item.Flags.Value.HasFlag(MessageFlags.Seen))
                                    {
                                        msgPrefix += "_N";
                                    }

                                    if (string.IsNullOrEmpty(messageIdSafeName))
                                    {
                                        messageIdSafeName = Guid.NewGuid().ToString();
                                    }

                                    var destFileName = destZipFolder + "\\" + msgPrefix + "_" + messageIdSafeName + ".eml";

                                    lock (writeEntryBlock)
                                    {
                                        var entry           = archive.CreateEntry(destFileName);
                                        entry.LastWriteTime = item.InternalDate.Value;
                                        using (Stream s = entry.Open())
                                        {
                                            msg.WriteTo(s);
                                            fileSize = s.Position;
                                            s.Close();
                                        }
                                    }

                                    DownloadSpeed.Add(new Tuple <DateTime, double, long>(dt, DateTime.Now.Subtract(dt).TotalMilliseconds, fileSize));

                                    if (totalMessagesDownloaded % 1024 == 0)
                                    {
                                        GCCollectUtils.CheckAndFreeMemory();
                                        zipToOpen.FlushAsync();
                                    }
                                }

                                folder.IsDownloading = false;

                                try
                                {
                                    imapFodler.Close();
                                }
                                catch (MailKit.ServiceNotConnectedException)
                                {
                                }

                                log.Add("Folder: " + folder.Folder + "\t\t" + folder.DownloadedItems + " emails");
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        // someting worong but the zip file is safe
                        internalEx   = ex;
                        downloadFail = true;
                    }

                    if (pcap != null && pcap.IsCapturing)
                    {
                        // Add pcap file to archive
                        pcap.StopCapture();
                        var pcapName = pcap.OutputFile.Split('\\').Last();
                        if (File.Exists(pcap.OutputFile))
                        {
                            using (FileStream fileStream = new FileStream(pcap.OutputFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                var entry = archive.CreateEntry(pcapName);
                                using (Stream s = entry.Open())
                                {
                                    fileStream.CopyTo(s);
                                    s.Close();
                                }
                            }
                        }

                        log.Add("Pcap: " + pcapName + "");
                    }
                    else
                    {
                        log.Add("No Pcap file.");
                    }
                }
            }

            if (downloadFail)
            {
                throw internalEx;
            }


            log.Add("");

            log.Add("Total emails: " + totalMessagesDownloaded);

            DateTime dtEnd = DateTime.UtcNow;

            log.Add("");
            log.Add("Startd at " + dtStart.ToUniversalTime() + " UTC");
            log.Add("End at " + dtEnd.ToUniversalTime() + " UTC");

            log.Add("");

            dc.PartialPercent = 100;

            log.Add("Export file : " + dc.DestinationFolder);

            dc.Speed30sec = "";
            dc.SpeedTotal = "Calculating hash..";

            string md5  = CalculateMD5(dc.DestinationFolder).Replace("-", "");
            string sha1 = CalculateSHA1(dc.DestinationFolder).Replace("-", "");

            log.Add("MD5 : " + md5);
            log.Add("SHA1 : " + sha1);

            if (File.Exists(logFileName))
            {
                File.Delete(logFileName);
            }

            File.WriteAllLines(logFileName, log.Union(logError));

            dc.PartialPercent = 100;

            dc.Speed30sec = "DONE! ";
            dc.SpeedTotal = string.Format(" It took {0} ", DateTime.UtcNow.Subtract(dtStart));

            return(totalMessagesDownloaded);
        }
        private static long DownloadMails(DataContext dc)
        {
            List <string> log      = new List <string>();
            List <string> logError = new List <string>()
            {
                "", ""
            };

            DateTime dtStart = DateTime.UtcNow;

            log.Add("");
            log.Add("LOGIN PARAMS");
            log.Add("Host name: " + dc.HostName + " (resolved ip = " + System.Net.Dns.GetHostEntry(dc.HostName).AddressList.FirstOrDefault() + ") ");
            log.Add("Port: " + dc.Port);
            log.Add("UseSSL: " + dc.UseSSL.ToString());
            log.Add("User name: " + dc.UserName);
            log.Add("User password: "******"");

            long totalMessagesDownloaded = 0;

            // clone the list
            TotalMails = dc.EmailFolders.Where(o => o.Selected).Sum(o => o.Messages);

            Parallel.ForEach(dc.EmailFolders, new ParallelOptions()
            {
                MaxDegreeOfParallelism = dc.ConcurrentThreads
            }, (folder) =>
            {
                if (folder.Selected == false)
                {
                    return;
                }

                // The default port for IMAP over SSL is 993.
                using (ImapClient client = new ImapClient())
                {
                    client.ServerCertificateValidationCallback = (s, c, h, ee) => true;

                    try
                    {
                        client.Connect(dc.HostName, dc.Port, dc.UseSSL);
                    }
                    catch (ImapProtocolException)
                    {
                        // try twice
                        System.Threading.Thread.Sleep(100);
                        client.Connect(dc.HostName, dc.Port, dc.UseSSL);
                    }

                    // Note: since we don't have an OAuth2 token, disable
                    // the XOAUTH2 authentication mechanism.
                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    client.Authenticate(dc.UserName, dc.UserPassword);

                    ImapFolder imapFodler = (ImapFolder)client.GetFolder(folder.Folder);

                    folder.IsDownloading = true;

                    folder.DownloadedItems = 0;

                    string destFolder = Path.Combine(dc.DestinationFolder, folder.Folder.Replace(imapFodler.DirectorySeparator, '\\'));

                    // If the folder already exsist I have do delete it
                    if (!dc.MergeFolders)
                    {
                        if (Directory.Exists(destFolder))
                        {
                            Directory.Delete(destFolder, true);
                        }
                    }

                    if (!Directory.Exists(destFolder))
                    {
                        Directory.CreateDirectory(destFolder);
                    }

                    string messageIdSafeName = "";

                    int downloadedEmails = 0;

                    try
                    {
                        imapFodler.Open(FolderAccess.ReadOnly);
                    }
                    catch (Exception)
                    {
                        logError.Add("Error: can't select imap folder '" + folder.Folder + "'");
                        return;
                    }

                    //IList<IMessageSummary> items = imapFodler.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size);
                    IList <IMessageSummary> items = imapFodler.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size);

                    DateTime dt   = DateTime.Now;
                    long fileSize = 0;
                    MimeMessage msg;

                    long folderSize = items.Sum(o => o.Size ?? 0);

                    foreach (var item in items)
                    {
                        if (dc.MergeFolders)
                        {
                            var file = Directory.GetFiles(destFolder, item.UniqueId + "_*.eml");
                            if (file.Count() == 1)
                            {
                                logError.Add("Log: message id " + item.UniqueId + " already downloaded from folder '" + folder.Folder + "'");
                                downloadedEmails++;
                                folder.DownloadedItems++;
                                continue;
                            }
                            else
                            {
                            }
                        }

                        dt       = DateTime.Now;
                        fileSize = 0;

                        try
                        {
                            msg = imapFodler.GetMessage(item.UniqueId);
                        }
                        catch
                        {
                            // Second attempt
                            try
                            {
                                msg = imapFodler.GetMessage(item.UniqueId);
                            }
                            catch (Exception ex)
                            {
                                // in the meanwhile a message has been deleted.. sometimes happens
                                logError.Add("Error: can't download message id " + item.UniqueId + " from folder '" + folder.Folder + "'");
                                continue;
                            }
                        }

                        ProgressMails++;

                        if (folder.Selected == false)
                        {
                            continue;
                        }

                        // msg not exsist
                        if (msg.From == null)
                        {
                            log.Add("Error: can't save message id " + item.UniqueId + " from folder '" + folder.Folder + "' because has no From field");
                            continue;
                        }

                        downloadedEmails++;
                        folder.DownloadedItems++;

                        messageIdSafeName = System.Text.RegularExpressions.Regex.Replace(msg.Headers["Message-ID"] + "", "[<>\\/]", "");

                        if (string.IsNullOrEmpty(messageIdSafeName))
                        {
                            messageIdSafeName = Guid.NewGuid().ToString();
                        }
                        else if (messageIdSafeName.Length > 250)
                        {
                            // i'll take the lst 250 characters
                            messageIdSafeName = messageIdSafeName.Substring(messageIdSafeName.Length - 250);
                        }
                        try
                        {
                            using (var fs = new FileStream(Path.Combine(destFolder, item.UniqueId + "_" + messageIdSafeName + ".eml"), FileMode.Create))
                            {
                                msg.WriteTo(fs);
                                fileSize = fs.Length;
                            }
                        }
                        catch (PathTooLongException)
                        {
                            logError.Add("Warning: message id " + item.UniqueId + " from folder '" + folder.Folder + "' will be saved with name '" + item.UniqueId + ".eml' because '" + item.UniqueId + "_" + messageIdSafeName + ".eml' is too long");
                            using (var fs = new FileStream(Path.Combine(destFolder, item.UniqueId + ".eml"), FileMode.Create))
                            {
                                msg.WriteTo(fs);
                                fileSize = fs.Length;
                            }
                        }

                        DownloadSpeed.Add(new Tuple <DateTime, double, long>(dt, DateTime.Now.Subtract(dt).TotalMilliseconds, fileSize));
                    }

                    folder.IsDownloading = false;

                    try
                    {
                        imapFodler.Close();
                    }
                    catch (MailKit.ServiceNotConnectedException)
                    {
                    }

                    log.Add("Folder: " + folder.Folder + "\t\t" + downloadedEmails + " emails");

                    totalMessagesDownloaded += downloadedEmails;
                }
            });

            log.Add("");

            log.Add("Total emails: " + totalMessagesDownloaded);

            DateTime dtEnd = DateTime.UtcNow;

            log.Add("");
            log.Add("Startd at " + dtStart.ToUniversalTime() + " UTC");
            log.Add("End at " + dtEnd.ToUniversalTime() + " UTC");

            log.Add("");

            dc.PartialPercent = 100;

            string lastDirName = dc.DestinationFolder.Split('\\', '/').Where(o => !string.IsNullOrEmpty(o)).Last();
            string superDir    = dc.DestinationFolder.Substring(0, dc.DestinationFolder.Length - lastDirName.Length - 1);
            string zipFileName = Path.Combine(superDir, lastDirName + ".zip");

            if (File.Exists(zipFileName))
            {
                File.Delete(zipFileName);
            }

            dc.PartialPercent = 0;

            dc.Speed30sec = "";
            dc.SpeedTotal = "Creating archive..";

            ZipFile.CreateFromDirectory(dc.DestinationFolder, zipFileName, CompressionLevel.Fastest, true);

            log.Add("Export file : " + zipFileName);

            dc.Speed30sec = "";
            dc.SpeedTotal = "Calculating hash..";

            string md5  = CalculateMD5(zipFileName).Replace("-", "");
            string sha1 = CalculateSHA1(zipFileName).Replace("-", "");

            log.Add("MD5 : " + md5);
            log.Add("SHA1 : " + sha1);

            string logFileName = Path.Combine(superDir, lastDirName + ".log");

            if (File.Exists(logFileName))
            {
                File.Delete(logFileName);
            }

            File.WriteAllLines(logFileName, log.Union(logError));

            Directory.Delete(dc.DestinationFolder, true);

            dc.PartialPercent = 100;

            dc.Speed30sec = "DONE! ";
            dc.SpeedTotal = string.Format(" It took {0} ", DateTime.UtcNow.Subtract(dtStart));

            return(totalMessagesDownloaded);
        }