Пример #1
0
        /// <summary>
        /// Сохранение вложений в выбранных папках, полученные текущей датой
        /// </summary>
        public int SaveAttachments()
        {
            MessageDate messageDate = new MessageDate();

            messageDate.ShowDialog();
            if (messageDate.DialogResult != DialogResult.OK)
            {
                return(0);
            }
            //messageDate.date

            int count = 0;

            OutlookApp.Session.Logon();
            foreach (string folderName in FolderNames)
            {
                Outlook.Folder folder = GetFolder(folderName);
                if (folder == null)
                {
                    continue;
                }

                ProcessBar pb = Forms.ProcessBar.Init("Сканирование папки " + folder.Name, folder.Items.Count, 1, folder.Name);
                pb.Show();
                foreach (object item in folder.Items)
                {
                    if (pb.Cancel)
                    {
                        break;
                    }

                    if (!(item is Outlook.MailItem mail))
                    {
                        pb.Action();
                        continue;
                    }

                    pb.Action(mail.ReceivedTime.Date.ToString());
                    if (mail.Attachments.Count == 0)
                    {
                        continue;
                    }
                    if (mail.ReceivedTime.Date < messageDate.DateStart || mail.ReceivedTime.Date > messageDate.DateEnd)
                    {
                        continue;
                    }

                    string path = Globals.ThisWorkbook.Path + "\\MailFromProviders\\" + DateTime.Today.ToString("dd.MM.yyyy") + '\\';
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    foreach (Outlook.Attachment attach in mail.Attachments)
                    {
                        if (!attach.FileName.Contains("xls"))
                        {
                            continue;
                        }
                        attach.SaveAsFile(path + attach.FileName);
                        count++;
                    }
                }
                pb.Close();
            }
            return(count);
        }