Esempio n. 1
0
        public void ReportHam(object sender, RibbonControlEventArgs e)
        {
            Outlook.Application outlookApp = this.Application;

            DialogResult result = MessageBox.Show(global::OutlookSPAMReport.AllResources.MessageBoxConfirmReportHam, "Spam Reporter", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.No)
            {
                return;
            }

            IEnumerable <Outlook.MailItem> selectedEmails = GetSelectedEmails();

            try
            {
                Outlook.MailItem hamReportMailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                string storeID = outlookApp.ActiveExplorer().CurrentFolder.StoreID;
                foreach (Outlook.Account account in outlookApp.Session.Accounts)
                {
                    if (account.DeliveryStore.StoreID == storeID)
                    {
                        hamReportMailItem.SendUsingAccount = account;
                        break;
                    }
                }


                hamReportMailItem.Body    = "Report ham";
                hamReportMailItem.Subject = "Report ham";
                hamReportMailItem.To      = appSettings.Default.HamTo;
                foreach (Outlook.MailItem oneEmail in selectedEmails)
                {
                    hamReportMailItem.Attachments.Add(oneEmail, Outlook.OlAttachmentType.olEmbeddeditem);
                }
                hamReportMailItem.Send();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            foreach (Outlook.MailItem oneEmail in selectedEmails)
            {
                try
                {
                    Outlook.Folder     parentFolder = (Outlook.Folder)oneEmail.Parent;
                    Outlook.Store      itemStore    = parentFolder.Store;
                    Outlook.MAPIFolder inboxFolder  = itemStore.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                    if (parentFolder.FolderPath != inboxFolder.FolderPath)
                    {
                        oneEmail.Move(inboxFolder);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Esempio n. 2
0
        public IMailFolder GetDefaultFolder(string folderName)
        {
            Outlook.OlDefaultFolders olFolderType;

            if (_folderTypes.TryGetValue(folderName, out olFolderType) == false)
            {
                return(null);
            }

            Outlook.Folder folder = _store.GetDefaultFolder(olFolderType) as Outlook.Folder;
            return(new MailFolderProviderOM(folder));
        }
        /// <summary>
        /// Returns one of "Default" folders in the mailbox
        /// For list of possible folders refer to
        /// https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook.oldefaultfolders(v=vs.15).aspx
        /// This class supports the following:
        /// "olFolderInbox", "olFolderDeletedItems", "olFolderDrafts", "olFolderJunk", "olFolderOutbox", "olFolderSentMail"
        /// </summary>
        /// <param name="folderName">String representing one of default folders (ex.: "olFolderInbox"). </param>
        /// <returns>MailFolder object or null if string does not match supported value</returns>
        public MailFolder GetDefaultFolder(string folderName)
        {
            Outlook.OlDefaultFolders olFolderType;
            try
            {
                olFolderType = GetFolderTypeByName(folderName);
            }
            catch (KeyNotFoundException)
            {
                return(null);
            }

            Outlook.Folder folder = _store.GetDefaultFolder(olFolderType) as Outlook.Folder;
            return(new MailFolder(folder));
        }
Esempio n. 4
0
        public static async Task <List <Outlook.Folder> > GetCalendars(bool autoLaunchOutlook)
        {
            var folders = new List <Outlook.Folder>();

            if (!autoLaunchOutlook && !OutlookRunning())
            {
                return(folders);
            }

            Outlook.Application outlook;
            Outlook.Stores      stores;

            try
            {
                await EnsureOutlookIsRunningAsync();

                outlook = new Outlook.Application();
                stores  = outlook.Session.Stores;
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Exception initializing Outlook in GetCalendars.\n{e}");

                // this can generally be ignored (it's often a COM RETRYLATER when Outlook is stuck
                // booting up etc). Regardless, there is no remediation, so let's bail.
                //
                // Note: Yes, this is a double return, yes the for loop will just exit and it will return
                //       anyway, but who knows what will get added post the for at some point in the future
                //       and what chaose that will cause. This exception is exceedingly rare so would rather
                //       fail fast.
                return(folders);
            }

            // foreach on COM objects can sometimes get into weird states when encountering
            // a corrupt pst,  where null objects repeat themselves, and a foreach goes into
            // an infinite loop, so prefer traditional for
            //
            // Note: These are one-based arrays
            //       See: https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._stores.item?view=outlook-pia#Microsoft_Office_Interop_Outlook__Stores_Item_System_Object_
            for (int i = 1; i <= stores?.Count; i++)
            {
                Outlook.Store store = null;

                try
                {
                    // this is in the try since sometimes COM will freak out and throw
                    // IndexOutOfRangeException even though we're < Count (corrupt pst situation)
                    store = stores[i];

                    // ignore public folders (causes slow Exchange calls, and we don't have a use case
                    // for interactions with those)
                    if (store.ExchangeStoreType == Outlook.OlExchangeStoreType.olExchangePublicFolder)
                    {
                        continue;
                    }

                    var folder = (Outlook.Folder)store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
                    System.Diagnostics.Debug.WriteLine($"Found calendar: {folder.Name} in store {store.DisplayName}");

                    folders.Add(folder);
                }
                catch (Exception e)
                {
                    // Not every root folder has a calendar (for example, Public folders), so this exception can be ignored
                    // This also catches cases where store cannot be retrieved from a specific index (even though the API
                    // says it is there. In those cases you'll see something like:
                    //  System.Runtime.InteropServices.COMException (0x80040119): Outlook cannot start because a data file to
                    //  send and receive messages cannot be found. Check your settings in this Microsoft Outlook profile. <snip>
                    Debug.WriteLine($"Failed to get Calendar for {store?.DisplayName} type: {store?.ExchangeStoreType}:\n{e}");
                }
            }

            return(folders);
        }
Esempio n. 5
0
        private void StartRun()
        {
            string _Subject  = GetSubject();
            string _strPath  = GetAttachpath();
            string _strPromt = "Обробка закінчена";

            Outlook.Application objOutlook   = new Outlook.Application();
            Outlook.Explorer    _actExplorer = objOutlook.Application.ActiveExplorer();

            Outlook.Stores stores = objOutlook.Application.Session.Stores;

            if (_actExplorer == null)
            {
                ToolStripStatusLabel1.Text = "OutLook not running!";
                return;
            }



            Outlook.Store storework = null;
            for (int i = 1; i <= stores.Count; i++)
            {
                if (stores[i].DisplayName == GetStore())
                {
                    storework = stores[i];
                    break;
                }
            }


            if (storework == null)
            {
                ToolStripStatusLabel1.Text = "OutLook store:" + GetStore() + "not found!";
                return;
            }


            Outlook.MAPIFolder inBox         = storework.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items      inBoxItems    = inBox.Items; //Outlook.MAPIFolder inBox
            Outlook.MAPIFolder OutBoxSubject = null;

            foreach (Outlook.Folder folder in inBox.Folders)
            {
                if (folder.Name.Equals(_Subject))
                {
                    OutBoxSubject = folder;
                }
            }

            if (OutBoxSubject == null)
            {
                OutBoxSubject = inBox.Folders.Add(_Subject);
            }


            ToolStripStatusLabel1.Text = "Вхідних : " + inBoxItems.Count;
            //inBoxItems = inBoxItems.Restrict( " [Unread] = true");
            inBoxItems = inBoxItems.Restrict("[Subject] = " + _Subject + "");

            ToolStripStatusLabel2.Visible    = true;
            ToolStripStatusLabel3.Visible    = true;
            ToolStripProgressBarMain.Visible = true;
            ToolStripProgressBarMain.Maximum = inBoxItems.Count;
            ToolStripProgressBarMain.Value   = 0;

            try
            {
                // BASIC CICLE

                SetRunStage(true);

                int _allattachment = 0, _allmail = 1;

                for (int i = inBoxItems.Count; i > 0; i--)

                {
                    Application.DoEvents();
                    Outlook.MailItem workLetter = inBoxItems[i] as Outlook.MailItem;

                    ToolStripStatusLabel3.Text = "Обробка листа: " + _allmail++;
                    ToolStripProgressBarMain.Value++;
                    Application.DoEvents();


                    if (!GetRunStage())
                    {
                        _strPromt = "Обробка перервана";

                        break;
                    }

                    Application.DoEvents();

                    if (workLetter.Subject == _Subject)

                    {
                        if (workLetter.Attachments.Count > 0)
                        {
                            string _folder = "";

                            _allattachment++;

                            for (int j = 1; j <= workLetter.Attachments.Count; j++)
                            {
                                _folder = MakeFolderForAttacment(workLetter.Attachments[j].FileName);
                                if (!Directory.Exists(_folder))
                                {
                                    Directory.CreateDirectory(_folder);
                                }
                                workLetter.Attachments[j].SaveAsFile(Path.Combine(_folder + "\\" + workLetter.Attachments[j].FileName));
                                ToolStripStatusLabel2.Text = "Оброблено вкладень: " + _allattachment;
                            }
                            workLetter.Move(OutBoxSubject);
                        }

                        else
                        {
                            workLetter.Delete();
                        }
                    }
                }
            }// end try


            catch (Exception ex)
            {
                string errorInfo = (string)ex.Message
                                   .Substring(0, 11);
                if (errorInfo == "Cannot save")
                {
                    MessageBox.Show(@"Bad folder to a save attacment");
                }
                MessageBox.Show(ex.Message);
                _strPromt = "OutLook was lost!";
            }

            // finally
            {
                EndRun(_strPromt);
                inBoxItems = null;
                objOutlook = null;
            }
        }