Esempio n. 1
0
        public static void Run()
        {
            //ExStart:1
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_IMAP();
            // Create an instance of the ImapClient class
            ImapClient imapClient = new ImapClient();

            // Specify host, username and password, and set port for your client
            imapClient.Host            = "imap.gmail.com";
            imapClient.Username        = "******";
            imapClient.Password        = "******";
            imapClient.Port            = 993;
            imapClient.SecurityOptions = SecurityOptions.Auto;

            imapClient.UseMultiConnection = MultiConnectionMode.Enable;

            ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo;

            ImapFolderInfo           info  = imapClient.GetFolderInfo(mailboxInfo.Inbox.Name);
            ImapFolderInfoCollection infos = new ImapFolderInfoCollection();

            infos.Add(info);

            imapClient.Backup(infos, dataDir + @"\ImapBackup.pst", BackupOptions.Recursive);
            //ExEnd:1
        }
Esempio n. 2
0
        internal async Task BindMailboxAsync(ImapMailboxInfo mailbox, IEnumerable <ImapMailboxInfo> subscriptions)
        {
            try {
                using (var context = new DatabaseContext()) {
                    context.Mailboxes.Attach(_mailbox);
                    context.Entry(_mailbox).State = EntityState.Modified;

                    _mailbox.Name         = mailbox.Fullname;
                    _mailbox.Delimiter    = mailbox.Delimiter.ToString(CultureInfo.InvariantCulture);
                    _mailbox.Flags        = mailbox.Flags.Aggregate((c, n) => c + ';' + n);
                    _mailbox.IsSubscribed = IsSubscribed || subscriptions.Any(x => x.Name == mailbox.Name);

                    await context.SaveChangesAsync();
                }
            } catch (Exception ex) {
                Logger.Error(ex);
            }
        }
Esempio n. 3
0
        private static int GetParentFolderIndex(List<ImapMailboxInfo> mailboxes, ImapMailboxInfo newMailbox, string separator)
        {
            var potentialParentIndexes = new List<int>();
            for (var ind = 0; ind < mailboxes.Count; ++ind)
            {
                if (newMailbox.name.StartsWith(mailboxes[ind].name + separator))
                {
                    potentialParentIndexes.Add(ind);
                }
            }

            var parentIndex = -1;
            if (potentialParentIndexes.Count > 0)
            {
                parentIndex = potentialParentIndexes.OrderByDescending(index => mailboxes[index].name.Length).First();
            }
            return parentIndex;
        }
        private static int GetParentFolderIndex(List <ImapMailboxInfo> mailboxes, ImapMailboxInfo new_mailbox, string separator)
        {
            var potential_parent_indexes = new List <int>();

            for (int ind = 0; ind < mailboxes.Count; ++ind)
            {
                if (new_mailbox.name.StartsWith(mailboxes[ind].name + separator))
                {
                    potential_parent_indexes.Add(ind);
                }
            }

            var parent_index = -1;

            if (potential_parent_indexes.Count > 0)
            {
                parent_index = potential_parent_indexes.OrderByDescending(index => mailboxes[index].name.Length).First();
            }
            return(parent_index);
        }
Esempio n. 5
0
        public static void Run()
        {
            //ExStart: 1
            ImapClient imapClient = new ImapClient();

            imapClient.Host                = "<HOST>";
            imapClient.Port                = 993;
            imapClient.Username            = "******";
            imapClient.Password            = "******";
            imapClient.SupportedEncryption = EncryptionProtocols.Tls;
            imapClient.SecurityOptions     = SecurityOptions.SSLImplicit;

            ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo;

            Console.WriteLine(mailboxInfo.Inbox);
            Console.WriteLine(mailboxInfo.DraftMessages);
            Console.WriteLine(mailboxInfo.JunkMessages);
            Console.WriteLine(mailboxInfo.SentMessages);
            Console.WriteLine(mailboxInfo.Trash);
            //ExEnd: 1

            Console.WriteLine("ImapSpecialUseMailboxes executed successfully.");
        }
        public static IEnumerable <ImapMailboxInfo> ParseImapMailboxes(string imap_response, string server = "",
                                                                       Dictionary
                                                                       <string, Dictionary <string, MailboxInfo> >
                                                                       special_domain_folders              = null,
                                                                       string[] skip_imap_flags            = null,
                                                                       Dictionary <string, int> imap_flags = null)
        {
            var mailboxes = new List <ImapMailboxInfo>();

            var domain_special_folders = new Dictionary <string, MailboxInfo>();

            if (special_domain_folders != null && special_domain_folders.Keys.Contains(server))
            {
                domain_special_folders = special_domain_folders[server];
            }


            var response = ListNewlineRegex.Replace(imap_response, m => "\" ");

            var t = Regex.Split(response, "\r\n");

            for (var i = 0; i < t.Length - 1; i++)
            {
                var m = SysfolderRegex.Match(t[i]);

                if (!m.Success)
                {
                    continue;
                }

                var name      = m.Groups[3].Value;
                var separator = m.Groups[2].Value;
                var flags     = m.Groups[1].Value.ToLower();

                if (!string.IsNullOrEmpty(name))
                {
                    if (name[0] == '\"' && name[name.Length - 1] == '\"')
                    {
                        name = name.Substring(1, name.Length - 2);
                    }

                    name = name.Replace("\\\"", "\"").Replace("\\\\", "\\");
                }

                var new_mailbox = new ImapMailboxInfo
                {
                    folder_id = MailFolder.Ids.inbox,
                    name      = name,
                    tags      = new string[] {}
                };

                if (new_mailbox.name.ToLower() != "inbox")
                {
                    var utf8_name = FolderNameDecodeHelper.Replace(new_mailbox.name, DecodeUtf7);
                    if (domain_special_folders.ContainsKey(utf8_name.ToLower()))
                    {
                        var info = domain_special_folders[utf8_name.ToLower()];
                        if (info.skip)
                        {
                            continue;
                        }

                        new_mailbox.folder_id = info.folder_id;
                    }
                    else
                    {
                        var look_for_parent = false;

                        var flags_match = ImapFlagRegex.Matches(flags);
                        if (flags_match.Count > 0)
                        {
                            var matches = new List <string>();
                            for (var j = 0; j < flags_match.Count; j++)
                            {
                                matches.Add(flags_match[j].Groups[1].Value);
                            }

                            if (
                                matches.Any(
                                    @group =>
                                    skip_imap_flags != null && skip_imap_flags.Contains(
                                        @group.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())))
                            {
                                continue;
                            }

                            if (imap_flags != null)
                            {
                                var flag = imap_flags.FirstOrDefault(f => matches.Contains(f.Key));
                                if (null != flag.Key)
                                {
                                    new_mailbox.folder_id = flag.Value;
                                    // special case for inbox - gmail l10n issue
                                    if (MailFolder.Ids.inbox == flag.Value && new_mailbox.name.ToLower() != "inbox")
                                    {
                                        new_mailbox.name = "inbox";
                                    }
                                }
                                else
                                {
                                    look_for_parent = true;
                                }
                            }
                        }
                        else
                        {
                            look_for_parent = true;
                        }

                        if (look_for_parent)
                        {
                            // if mailbox is potentialy child - add tag. Tags looks like Tag1/Tag2/Tag3
                            const string tag_for_store_separator = "/";
                            var          tag = utf8_name.Replace(separator, tag_for_store_separator);

                            var parent_index = GetParentFolderIndex(mailboxes, new_mailbox, separator);

                            if (parent_index >= 0)
                            {
                                var parent = mailboxes[parent_index];
                                new_mailbox.folder_id = parent.folder_id;

                                // if system mailbox - removes first tag
                                // if not system mailbox child - removes same count of tags as in parent
                                if (!parent.tags.Any())
                                {
                                    tag = tag.Substring(tag.IndexOf(tag_for_store_separator, StringComparison.Ordinal));
                                }
                            }

                            new_mailbox.tags = new[] { tag };
                        }
                    }
                }
                mailboxes.Add(new_mailbox);
            }

            return(mailboxes);
        }
        // gets mailboxes, messages from wich we should get
        public static IEnumerable<ImapMailboxInfo> GetImapMailboxes(Imap4Client client, string server)
        {
            var mailboxes = new List<ImapMailboxInfo>();

            var special_domain_folders = new Dictionary<string, MailQueueItemSettings.MailboxInfo>();
            if (MailQueueItemSettings.SpecialDomainFolders.Keys.Contains(server))
                special_domain_folders = MailQueueItemSettings.SpecialDomainFolders[server];

            // get all mailboxes
            var response = client.Command("LIST \"\" \"*\"");
            var t = Regex.Split(response, "\r\n");
            for (var i = 0; i < t.Length - 2; i++)
            {
                var m = SysfolderRegex.Match(t[i]);
                if (!m.Success)
                    continue;

                var new_mailbox = new ImapMailboxInfo
                    {
                        folder_id = MailFolder.Ids.inbox,
                        name = m.Groups[3].Value,
                        tags = new string[]{}
                    };
                var separator = m.Groups[2].Value;
                if (new_mailbox.name.ToLower() != "inbox")
                {
                    var utf8_name = FolderNameDecodeHelper.Replace(new_mailbox.name, DecodeUtf7);
                    if (special_domain_folders.ContainsKey(utf8_name.ToLower()))
                    {
                        var info = special_domain_folders[utf8_name.ToLower()];
                        if (info.skip)
                            continue;

                        new_mailbox.folder_id = info.folder_id;
                    }
                    else
                    {
                        var look_for_parent = false;

                        var flags_match = ImapFlagRegex.Matches(m.Groups[1].Value.ToLower());
                        if (flags_match.Count > 0)
                        {
                            var matches = new List<string>();
                            for (var j = 0; j < flags_match.Count; j++)
                            {
                                matches.Add(flags_match[j].Groups[1].Value);
                            }

                            if (
                                matches.Any(
                                    @group =>
                                    MailQueueItemSettings.SkipImapFlags.Contains(
                                        @group.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())))
                                continue;

                            var flag = MailQueueItemSettings.ImapFlags.FirstOrDefault(f => matches.Contains(f.Key));
                            if (null != flag.Key)
                            {
                                new_mailbox.folder_id = flag.Value;
                                // special case for inbox - gmail l10n issue
                                if (MailFolder.Ids.inbox == flag.Value && new_mailbox.name.ToLower() != "inbox")
                                    new_mailbox.name = "inbox";
                            }
                            else
                            {
                                look_for_parent = true;
                            }
                        }
                        else
                        {
                            look_for_parent = true;
                        }

                        if (look_for_parent)
                        {
                            // if mailbox is potentialy child - add tag. Tags looks like Tag1/Tag2/Tag3
                            const string tag_for_store_separator = "/";
                            var tag = utf8_name.Replace(separator, tag_for_store_separator);

                            var parent_index = GetParentFolderIndex(mailboxes, new_mailbox, separator);

                            if (parent_index >= 0)
                            {
                                var parent = mailboxes[parent_index];
                                new_mailbox.folder_id = parent.folder_id;

                                // if system mailbox - removes first tag
                                // if not system mailbox child - removes same count of tags as in parent
                                if (!parent.tags.Any())
                                    tag = tag.Substring(tag.IndexOf(tag_for_store_separator, StringComparison.Ordinal));
                            }

                            new_mailbox.tags = new[] { tag };
                        }
                    }
                }
                mailboxes.Add(new_mailbox);
            }

            return mailboxes;
        }
Esempio n. 8
0
        // gets mailboxes, messages from wich we should get
        public static IEnumerable <ImapMailboxInfo> GetImapMailboxes(Imap4Client client, string server)
        {
            var mailboxes = new List <ImapMailboxInfo>();

            var special_domain_folders = new Dictionary <string, MailQueueItemSettings.MailboxInfo>();

            if (MailQueueItemSettings.SpecialDomainFolders.Keys.Contains(server))
            {
                special_domain_folders = MailQueueItemSettings.SpecialDomainFolders[server];
            }

            // get all mailboxes
            var response = client.Command("LIST \"\" \"*\"");
            var t        = Regex.Split(response, "\r\n");

            for (var i = 0; i < t.Length - 2; i++)
            {
                var m = SysfolderRegex.Match(t[i]);
                if (!m.Success)
                {
                    continue;
                }

                var new_mailbox = new ImapMailboxInfo
                {
                    folder_id = MailFolder.Ids.inbox,
                    name      = m.Groups[3].Value,
                    tags      = new string[] {}
                };
                var separator = m.Groups[2].Value;
                if (new_mailbox.name.ToLower() != "inbox")
                {
                    var utf8_name = FolderNameDecodeHelper.Replace(new_mailbox.name, DecodeUtf7);
                    if (special_domain_folders.ContainsKey(utf8_name.ToLower()))
                    {
                        var info = special_domain_folders[utf8_name.ToLower()];
                        if (info.skip)
                        {
                            continue;
                        }

                        new_mailbox.folder_id = info.folder_id;
                    }
                    else
                    {
                        var look_for_parent = false;

                        var flags_match = ImapFlagRegex.Matches(m.Groups[1].Value.ToLower());
                        if (flags_match.Count > 0)
                        {
                            var matches = new List <string>();
                            for (var j = 0; j < flags_match.Count; j++)
                            {
                                matches.Add(flags_match[j].Groups[1].Value);
                            }

                            if (
                                matches.Any(
                                    @group =>
                                    MailQueueItemSettings.SkipImapFlags.Contains(
                                        @group.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())))
                            {
                                continue;
                            }

                            var flag = MailQueueItemSettings.ImapFlags.FirstOrDefault(f => matches.Contains(f.Key));
                            if (null != flag.Key)
                            {
                                new_mailbox.folder_id = flag.Value;
                                // special case for inbox - gmail l10n issue
                                if (MailFolder.Ids.inbox == flag.Value && new_mailbox.name.ToLower() != "inbox")
                                {
                                    new_mailbox.name = "inbox";
                                }
                            }
                            else
                            {
                                look_for_parent = true;
                            }
                        }
                        else
                        {
                            look_for_parent = true;
                        }

                        if (look_for_parent)
                        {
                            // if mailbox is potentialy child - add tag. Tags looks like Tag1/Tag2/Tag3
                            const string tag_for_store_separator = "/";
                            var          tag = utf8_name.Replace(separator, tag_for_store_separator);

                            var parent_index = GetParentFolderIndex(mailboxes, new_mailbox, separator);

                            if (parent_index >= 0)
                            {
                                var parent = mailboxes[parent_index];
                                new_mailbox.folder_id = parent.folder_id;

                                // if system mailbox - removes first tag
                                // if not system mailbox child - removes same count of tags as in parent
                                if (!parent.tags.Any())
                                {
                                    tag = tag.Substring(tag.IndexOf(tag_for_store_separator, StringComparison.Ordinal));
                                }
                            }

                            new_mailbox.tags = new[] { tag };
                        }
                    }
                }
                mailboxes.Add(new_mailbox);
            }

            return(mailboxes);
        }
Esempio n. 9
0
        public static IEnumerable<ImapMailboxInfo> ParseImapMailboxes(string imapResponse, string server = "",
                                                                      Dictionary
                                                                          <string, Dictionary<string, MailboxInfo>>
                                                                          specialDomainFolders = null,
                                                                      string[] skipImapFlags = null,
                                                                      Dictionary<string, int> imapFlags = null)
        {
            var mailboxes = new List<ImapMailboxInfo>();

            var domainSpecialFolders = new Dictionary<string, MailboxInfo>();
            if (specialDomainFolders != null && specialDomainFolders.Keys.Contains(server))
                domainSpecialFolders = specialDomainFolders[server];


            var response = ListNewlineRegex.Replace(imapResponse, m => "\" ");

            var t = Regex.Split(response, "\r\n");

            for (var i = 0; i < t.Length - 1; i++)
            {
                var m = SysfolderRegex.Match(t[i]);

                if (!m.Success)
                    continue;

                var name = m.Groups[3].Value;
                var separator = m.Groups[2].Value;
                var flags = m.Groups[1].Value.ToLower();

                if (!string.IsNullOrEmpty(name))
                {
                    if (name[0] == '\"' && name[name.Length - 1] == '\"')
                    {
                        name = name.Substring(1, name.Length - 2);
                    }

                    name = name.Replace("\\\"", "\"").Replace("\\\\", "\\");
                }

                var newMailbox = new ImapMailboxInfo
                    {
                        folder_id = MailFolder.Ids.inbox,
                        name = name,
                        tags = new string[] {}
                    };

                if (newMailbox.name.ToLower() != "inbox")
                {
                    var utf8Name = FolderNameDecodeHelper.Replace(newMailbox.name, DecodeUtf7);
                    if (domainSpecialFolders.ContainsKey(utf8Name.ToLower()))
                    {
                        var info = domainSpecialFolders[utf8Name.ToLower()];
                        if (info.skip)
                            continue;

                        newMailbox.folder_id = info.folder_id;
                    }
                    else
                    {
                        var lookForParent = false;

                        var flagsMatch = ImapFlagRegex.Matches(flags);
                        if (flagsMatch.Count > 0)
                        {
                            var matches = new List<string>();
                            for (var j = 0; j < flagsMatch.Count; j++)
                            {
                                matches.Add(flagsMatch[j].Groups[1].Value);
                            }

                            if (
                                matches.Any(
                                    @group =>
                                    skipImapFlags != null && skipImapFlags.Contains(
                                        @group.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())))
                                continue;

                            if (imapFlags != null)
                            {
                                var flag = imapFlags.FirstOrDefault(f => matches.Contains(f.Key));
                                if (null != flag.Key)
                                {
                                    newMailbox.folder_id = flag.Value;
                                    // special case for inbox - gmail l10n issue
                                    if (MailFolder.Ids.inbox == flag.Value && newMailbox.name.ToLower() != "inbox")
                                        newMailbox.name = "inbox";
                                }
                                else
                                {
                                    lookForParent = true;
                                }
                            }
                        }
                        else
                        {
                            lookForParent = true;
                        }

                        if (lookForParent)
                        {
                            // if mailbox is potentialy child - add tag. Tags looks like Tag1/Tag2/Tag3
                            const string tag_for_store_separator = "/";
                            var tag = utf8Name.Replace(separator, tag_for_store_separator);

                            var parentIndex = GetParentFolderIndex(mailboxes, newMailbox, separator);

                            if (parentIndex >= 0)
                            {
                                var parent = mailboxes[parentIndex];
                                newMailbox.folder_id = parent.folder_id;

                                // if system mailbox - removes first tag
                                // if not system mailbox child - removes same count of tags as in parent
                                if (!parent.tags.Any())
                                    tag = tag.Substring(tag.IndexOf(tag_for_store_separator, StringComparison.Ordinal));
                            }

                            newMailbox.tags = new[] {tag};
                        }
                    }
                }
                mailboxes.Add(newMailbox);
            }

            return mailboxes;
        }