Example #1
0
        public List <Email> GetMessages(string entryID, int numPerPage, int pageNum)
        {
            List <Email> list = new List <Email>();

            Outlook.MAPIFolder f;

            // if no ID specified, open the inbox
            if (string.IsNullOrEmpty(entryID))
            {
                f = _nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            }
            else
            {
                f = _nameSpace.GetFolderFromID(entryID, "");
            }

            // to handle the sorting, one needs to cache their own instance of the items object
            Outlook.Items items = f.Items;

            // sort descending by received time
            items.Sort("[ReceivedTime]", true);

            // pull in the correct number of items based on number of items per page and current page number
            for (int i = (numPerPage * pageNum) + 1; i <= (numPerPage * pageNum) + numPerPage && i <= items.Count; i++)
            {
                // ensure it's a mail message
                Outlook.MailItem mi = (items[i] as Outlook.MailItem);
                if (mi != null)
                {
                    list.Add(new Email(mi.EntryID, mi.SenderEmailAddress, mi.SenderName, mi.Subject, mi.ReceivedTime, mi.Size));
                }
            }

            return(list);
        }
Example #2
0
 public IFolder GetFolderFromID(string folderId)
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.NameSpace nmspace = com.Add(_app.Session);
         NSOutlook.Folder    f       = (NSOutlook.Folder)nmspace.GetFolderFromID(folderId);
         return(Mapping.Wrap <IFolder>(f));
     }
 }
Example #3
0
 protected NSOutlook.MAPIFolder CloneComObject()
 {
     using (ComRelease com = new ComRelease())
     {
         NSOutlook.Application app     = com.Add(_item.Application);
         NSOutlook.NameSpace   session = com.Add(app.Session);
         NSOutlook.MAPIFolder  folder  = session.GetFolderFromID(EntryID);
         return(folder);
     }
 }
        static void Main(string[] args)
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook.NameSpace   oNS  = oApp.GetNamespace("mapi");
            oNS.Logon("appsystemacc", "App@12345", false, false);
            // this is how you set those properties from the session
            var EntryID = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).EntryID;
            var StoreID = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).StoreID;

            var folderID = oNS.GetFolderFromID(EntryID, StoreID);
        }
Example #5
0
 private static void ShowFolderInfo(Outlook.NameSpace outlookNameSpace, ref Outlook.MAPIFolder inbox, Outlook.Application app)
 {
     MessageBox.Show("Za chwilę pojawi Ci się okno z wyborem folderu.\n" +
                     "Rozsądnym wyborem będzie zaznaczenie folderu \"Inbox\" skrzynki \"NC MailBox\"" +
                     "\n(jest to wybór skrzynki dla której będą sprawdzane maile przychodzące pod kątem poprawności)" +
                     "\nPS Jak nic nie wybierzesz, albo wybierz coś aby wywalić to Ci się to uda...");
     if (app.Session.PickFolder() is Outlook.Folder folder)
     {
         FolderStoreID = folder.StoreID;
         FolderEntryID = folder.EntryID;
         inbox         = outlookNameSpace.GetFolderFromID(folder.EntryID, folder.StoreID);
     }
 }
 private static void ShowFolderInfo(Outlook.NameSpace outlookNameSpace, ref Outlook.MAPIFolder inbox, Outlook.Application app)
 {
     MessageBox.Show("Za chwilę pojawi się okno z wyborem folderu.\n" +
                     "Rozsądnym wyborem będzie zaznaczenie folderu \"Inbox\" dla skrzynki \"NC MailBox\"" +
                     "\n(jest to wybór skrzynki, dla której będą sprawdzane maile przychodzące pod kątem poprawności)");
     if (app.Session.PickFolder() is Outlook.Folder folder)
     {
         FolderStoreID = folder.StoreID;
         FolderEntryID = folder.EntryID;
         inbox         = outlookNameSpace.GetFolderFromID(folder.EntryID, folder.StoreID);
     }
     else
     {
         MessageBox.Show("Ooops . . . Something went wrong in ShowFolderInfo.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Example #7
0
        /// <summary>
        /// Get the foldername from the FolderId and StoreId
        /// </summary>
        /// <param name="FolderId"></param>
        /// <param name="StoreId"></param>
        /// <returns></returns>
        private string GetFolderById(string FolderId, string StoreId)
        {
            object missing    = Missing.Value;
            string foldername = string.Empty;

            // if invalid input data then exit returning empty string
            if (FolderId == string.Empty || StoreId == string.Empty)
            {
                return(string.Empty);
            }

            //get the Outlook Application Object
            Outlook.Application outlookApplication = new Outlook.Application();

            //get the namespace object
            Outlook.NameSpace nameSpace = outlookApplication.GetNamespace("MAPI");

            //Logon to Session, here we use an already opened Outlook session
            nameSpace.Logon(missing, missing, false, false);

            //get the InboxFolder
            Outlook.MAPIFolder Folder = nameSpace.GetFolderFromID(FolderId, StoreId);
            if (Folder != null)
            {
                foldername = Folder.Name;
            }

            //release used resources

            Marshal.ReleaseComObject(Folder);

            //logof from namespace
            nameSpace.Logoff();

            //release resources
            Marshal.ReleaseComObject(nameSpace);
            Marshal.ReleaseComObject(outlookApplication.Application);

            return(foldername);
        }
Example #8
0
 public static void Config(Outlook.NameSpace outlookNameSpace, ref Outlook.MAPIFolder inbox, Outlook.Application app)
 {
     if (LoadConfiguration())
     {
         //super... niech się dzieje zawsze w tle i tyle
         if (FolderEntryID.Equals("") || FolderStoreID.Equals(""))
         {
             ShowFolderInfo(outlookNameSpace, ref inbox, app);
             SaveConfiguration();
         }
         else
         {
             inbox = outlookNameSpace.GetFolderFromID(FolderEntryID, FolderStoreID);
         }
     }
     else
     {
         //nie mamy zapisanej konfiguracji - właściwie to pierwsze uruchomienie
         ShowFolderInfo(outlookNameSpace, ref inbox, app);
         SaveConfiguration();
     }
 }
 private static Outlook.MAPIFolder GetCommonViewsFolder()
 {
     try
     {
         if (null == m_NameSpace)
         {
             Initialise();
         }
         Outlook.Store            olStore            = m_NameSpace.DefaultStore;
         Outlook.PropertyAccessor olPropertyAccessor = olStore.PropertyAccessor;
         string commonViewsEntryId = olPropertyAccessor.BinaryToString(olPropertyAccessor.GetProperty(PR_COMMON_VIEWS_ENTRY_ID));
         if (commonViewsEntryId != string.Empty)
         {
             return(m_NameSpace.GetFolderFromID(commonViewsEntryId));
         }
         return(null);
     }
     catch (Exception exception)
     {
         Console.WriteLine("Unable to get common views folder. Error: " + exception.ToString());
         return(null);
     }
 }
Example #10
0
 public void setInbox(string folderId)
 {
     inbox                = ns.GetFolderFromID(folderId);
     _inboxItems          = inbox.Items;
     _inboxItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(inboxItems_ItemAdd);
 }
Example #11
0
        private static void CreateOutlookInstance()
        {
            if (OutlookApplication == null || _outlookNamespace == null)
            {
                CreateOutlookApplication();

                if (OutlookApplication == null)
                {
                    throw new NotSupportedException("Could not create instance of 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and retry.");
                }

                CreateOutlookNamespace();

                if (_outlookNamespace == null)
                {
                    throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and retry.");
                }
                else
                {
                    Logger.Log("Connected to Outlook: " + VersionInformation.GetOutlookVersion(OutlookApplication), EventType.Debug);
                }
            }

            //Just try to access the outlookNamespace to check, if it is still accessible, throws COMException, if not reachable
            try
            {
                if (string.IsNullOrEmpty(ContactsSynchronizer.SyncContactsFolder))
                {
                    _outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                }
                else
                {
                    _outlookNamespace.GetFolderFromID(ContactsSynchronizer.SyncContactsFolder);
                }
            }
            catch (COMException ex)
            {
                if (ex.ErrorCode == unchecked ((int)0x80029c4a))
                {
                    Logger.Log(ex, EventType.Debug);
                    throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
                }
                else if (ex.ErrorCode == unchecked ((int)0x80040111)) //"The server is not available. Contact your administrator if this condition persists."
                {
                    try
                    {
                        Logger.Log("Trying to logon, 1st try", EventType.Debug);
                        _outlookNamespace.Logon("", "", false, false);
                        Logger.Log("1st try OK", EventType.Debug);
                    }
                    catch (Exception e1)
                    {
                        Logger.Log(e1, EventType.Debug);
                        try
                        {
                            Logger.Log("Trying to logon, 2nd try", EventType.Debug);
                            _outlookNamespace.Logon("", "", true, true);
                            Logger.Log("2nd try OK", EventType.Debug);
                        }
                        catch (Exception e2)
                        {
                            Logger.Log(e2, EventType.Debug);
                            throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", e2);
                        }
                    }
                }
                else
                {
                    Logger.Log(ex, EventType.Debug);
                    throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
                }
            }
        }