private void UpdateFolder(OutlookFolder folder)
        {
            if (folder.DefaultItemType != OlItemType.olAppointmentItem && folder.DefaultItemType != OlItemType.olTaskItem && folder.DefaultItemType != OlItemType.olContactItem)
            {
                string wrongFolderMessage = string.Format("Wrong ItemType in folder '{0}'. It should be a calendar, task or contact folder.", folder.Name);
                MessageBox.Show(wrongFolderMessage, "Configuration Error");
                return;
            }

            _selectedFolder    = new FolderDescriptor(folder.EntryId, folder.StoreId);
            SelectedFolderName = folder.Name;
            OutlookFolderType  = folder.DefaultItemType;
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            //Load PST file
            Spire.Email.Outlook.OutlookFile olf = new OutlookFile(@"..\..\..\..\..\..\Data\Sample.pst");
            //Load Outlook MSG file
            OutlookItem item = new OutlookItem();

            item.LoadFromFile(@"..\..\..\..\..\..\Data\Sample.msg");
            //Select the "Inbox" folder
            OutlookFolder inboxFolder = olf.RootOutlookFolder.GetSubFolder("Inbox");

            //Add the MSG to "Inbox" folder
            inboxFolder.AddItem(item);
            MessageBox.Show("Completed");
        }
Esempio n. 3
0
        private OutlookTasksWrapper GetOutlookEntriesForSelectedTimeRange()
        {
            var         disposeOutlookInstances = false;
            Application application             = null;
            NameSpace   nameSpace = null;
            MAPIFolder  defaultOutlookCalendar = null;
            Items       outlookItems           = null;
            var         outlookTasks           = new List <ReminderTask>();

            //Close  and Shutdown
            try
            {
                // Get Application and Namespace
                GetOutlookApplication(out disposeOutlookInstances, out application, out nameSpace, ProfileName);

                // Get Default Calendar
                defaultOutlookCalendar = OutlookTaskList != null
                    ? nameSpace.GetFolderFromID(OutlookTaskList.EntryId, OutlookTaskList.StoreId)
                    : nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderTasks);

                if (OutlookTaskList == null)
                {
                    OutlookTaskList = new OutlookFolder
                    {
                        Name    = defaultOutlookCalendar.Name,
                        EntryId = defaultOutlookCalendar.EntryID,
                        StoreId = defaultOutlookCalendar.StoreID
                    };
                }
                // Get outlook Items
                outlookItems = defaultOutlookCalendar.Items;

                if (outlookItems != null)
                {
                    var items     = outlookItems.Cast <TaskItem>();
                    var taskItems = items as TaskItem[] ?? items.ToArray();
                    if (taskItems.Any())
                    {
                        var id = defaultOutlookCalendar.EntryID;
                        foreach (var appointmentItem in taskItems)
                        {
                            try
                            {
                                var app = GetTaskFromItem(id, appointmentItem);
                                outlookTasks.Add(app);
                            }
                            catch (Exception exception)
                            {
                                Logger.Error(exception);
                            }
                            finally
                            {
                                Marshal.FinalReleaseComObject(appointmentItem);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return(new OutlookTasksWrapper
                {
                    Tasks = null,
                    WaitForApplicationQuit = disposeOutlookInstances
                });
            }
            finally
            {
                if (disposeOutlookInstances)
                {
                    if (nameSpace != null)
                    {
                        nameSpace.Logoff();
                    }
                }

                //Unassign all instances
                if (outlookItems != null)
                {
                    Marshal.FinalReleaseComObject(outlookItems);
                    outlookItems = null;
                }

                if (defaultOutlookCalendar != null)
                {
                    Marshal.FinalReleaseComObject(defaultOutlookCalendar);
                    defaultOutlookCalendar = null;
                }

                if (nameSpace != null)
                {
                    Marshal.FinalReleaseComObject(nameSpace);
                    nameSpace = null;
                }

                if (disposeOutlookInstances)
                {
                    // Casting Removes a warninig for Ambigous Call
                    application.Quit();
                    Marshal.FinalReleaseComObject(application);
                }
                application = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return(new OutlookTasksWrapper
            {
                Tasks = outlookTasks,
                WaitForApplicationQuit = disposeOutlookInstances
            });
        }