Find multiple mail boxes at root parent? - c#

I got a little pet project for work. We have three mail boxes where two of them we need to get told, when we get new emails. I wanted to make a program for that (mostly just for the learning experience) and dived into the Interop libraries of Microsoft.
The two mailboxes where we need notifications, I first need to locate those mailboxes and then I can assign things like event listeners, etc. But I don't know how to achieve this.
To find the root, I do this:
outlookNameSpace = outlookApplication.GetNamespace("MAPI");
And then I can start dragging out folders and assign them to variables like so:
supportInbox = outlookNameSpace.Folders["Omitted"].Folders["Inbox"];
pensionInbox = outlookNameSpace.Folders["Omitted"].Folders["Inbox"];
But the code says that it cannot find the folders with the names (that I replaced with "Omitted" just for sake of privacy).
What am I doing wrong?

If you are accessing multiple Mailboxes, you have to first ensure that they are both loaded in the current Outlook profile. Otherwise, you need to ensure that the currently logged on user has delegate access to the Mailbox you want to access and use the NameSpace.GetSharedDefaultFolder method to open their Inbox. However, you only have access to the Inbox with Delegate scenarios.
Otherwise, to get specific folders for a specific Mailbox in the current profile, you can try this approach:
How to: Obtain a Folder Object from a Folder Path
http://msdn.microsoft.com/en-us/library/ff868990(v=office.15).aspx
You may also need to loop through NameSpace.Stores to get a specific Store object for the Mailbox, and access a folder via Store.GetRootFolder or Store.GetDefaultFolder.

Related

Is it possible for a user to only select file save location once?

I've been trying to learn how to handle saving normal .txt files in UWP, and have realized that it's quite locked down compared to WPF, especially in the sense of what folders you can access without requesting the user to select a location. I have searched for various ways this might be possible but found no working answer.
Question Description:
I basically would love to know if this is possible, and preferably a point in the direction where I can learn how exactly to do this.
Application settings page requires user to select folder where files are saved.
Application remembers this between launches (unsure if this is possible, but I can't require the user to select the folder on every launch)
Application saves files to the specified folder.
In my understanding, this should be possible, as the user is the one specifying the location via filepicker, but is it possible to have this work between launches so that the user wont be required to re-select the save folder?
I need to figure this out, as I would like my application to support selecting attached network drives, cloud storage folders, etc.
Any help is very much appreciated, and if there are any questions I will answer them to the best of my ability.
Fow this purpose there are two access lists designed: FutureAccessList and MostRecentlyUsedList. Once the user has picked up the folder with the picker, you add it to such list and receive a token, which you save for future purpose in LocalSettings:
ApplicationData.Current.LocalSettings.Values["MyFolder"] = StorageApplicationPermissions.FutureAccessList.Add(pickedFolder);
Then later, once you want to access that folder, you can do it like this:
StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(ApplicationData.Current.LocalSettings.Values["MyFolder"].ToString());
You can't save a StorageFolder or a path to it in settings, hence the UWP app needs permissions to access the folder. Using above access lists solves this problem.
I believe you want to save user settings and keep it somewhere so that next time when they launch the application, they can use the same settings.
Please check out this tutorial from Microsoft, which describes how to do exactly that.
https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx

C# Interop.Outlook: Folder.AddToPFFavorites() without adding all subfolders?

In Outlook, I'm trying to add a shared public folder (provided by Exchange) to the list of favorite folders programmatically.
I've written an Outlook-AddIn for this, that uses the Microsoft.Office.Interop.Outlook library. In this library, there's only one way to add public folders to the favorite list:
Folder.AddToPFFavorites().
The problem: When calling this method, Outlook not only adds the folder itself to the favorites list, but also ALL subfolders. In our company, we have a huge tree of subfolders attached to some folders, so I get major performance problems (Outlook completely crashes when the folder to add has too many subfolders).
Do you know a way to programmatically add only the folder itself to the favorites, without any subfolders?
The Outlook object model doesn't provide anything for that. You have to use the AddToPFFavorites method of the Folder class.

Ews, get public subfolders where emails have been added within a given period

Is there a way - by using Microsoft.Exchange.WebServices in C# or Exchange Management Shell- to get a list of all subfolders within a Public folder, where emails have been added within a given period of time?
Thanks in advance
Shungite
With EWS you can check the PidTagLocalCommitTimeMax https://msdn.microsoft.com/en-us/library/jj663242(v=exchg.80).aspx on each folder you want to evaluate which should contain the time of the last change to the folder. The other easy way to do this and probably more accurate with public folder is just enumerate the last Item in each folder and check the date-time. Whatever method you want to use will require a separate operation on each folder you want to check.
With the EMS you can use Get-PublicFolderStatistics https://technet.microsoft.com/en-us/library/aa997949(v=exchg.150).aspx which has LastModification and AccessTime as well as the ability to report on Items. This will generally be quicker then EWS and not subject to permission issues.

C# Outlook distinguish folder by its special type

I need to check if a given folder has a special type. For example, I need to check if a folder is an outbox folder. I know I could retrieve the outbox folder with Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
but that way I could only access the outbox folder of my main mailbox. Since I'm working with multiple mailboxes, that won't be enough.
I know I could difference the folder by checking its name but since the name is localized and it should work in every language, that's not acceptable solution.
Or maybe there is a way to retrieve all folders of a special type like outbox that would bring me a step forward.
Thanks for any replies.
I use
HashSet<string> excludeFolderIds = new HashSet<string>();
Outlook.Stores stores = application.Session.Stores;
foreach(Outlook.Store store in stores){
//exclude outbox folder from event
Outlook.MAPIFolder exclude = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
excludeFolderIds.Add(exclude.EntryID);
}
and check later if a folder has this type by calling excludeFolderIds.Contains(folder.EntryID)
so store.GetDefaultFolder(OlDefaultFolder) is the key to get Default folders from a different account

How to identify "my" custom folder in an Outlook AddIn

I'm trying to develop an AddIn - or rather, just a proof-of-concept for now, to see if what I have in mind is actually even possible - for Outlook (2010, to be exact), in .NET/C# and I'm facing the following problem:
The AddIn is supposed to offer a new custom Folder (on the top level of the hierarchy, i.e. next to all the other main items, like Tasks, Calendar, Contacts, etc.) in which to offer items to the user. So I figured that in the Startup method of the AddIn I could simply do something like
Outlook.Folder parent = inBox.Parent as Outlook.Folder;
Outlook.Folder myCustomFolder = (Outlook.Folder)parent.Folders.Add("My Custom Folder");
... and that does in fact work. However, there's a problem after quitting Outlook and starting it again. Since the folder is being persisted by Outlook, it is still there the next time Outlook launches and initializes the AddIn again, so the creation of the folder fails because an object of the same name already exists. But I don't see any way how to tell that this is "my" folder from last time.
I don't want to rely on its name to identify the folder (that's just too unreliable to even consider; users might want to rename it, other AddIns might exist that create a folder of the same name, not to mention localization problems etc.), but what else can I use to determine that the custom folder has already been created?
I would either have to be able to somehow add a "tag" do the folder so I can later recognize it as "mine" - or alternatively would need some kind of id that uniquely identifies the folder (and which remains constant even between launches of Outlook!) so I can recognize it by that.
I have been looking at the EntryID and StoreID fields of the [MAPI]Folder object, but from the (sadly, not very detailed) description at the MSDN, I'm not sure if I can rely on them, because apparently they can change under certain conditions.
Any suggestions?
Unless the folder is deleted and then recreated, the entry won't change. But it won't be the same folder anyway - just another folder that might have the same name.
What Outlook does is store the special folders' entryids on the root IPM folder and/or the Inbox folder.
Since you cannot set named properties on folder in Exchange and you cannot just pick your own property tag without risking running into a conflict, create a hidden message in the Inbox folder(which is always present in the default store) and store the folder entry id along with whatever else configuration properties you might need. To make sure your config hidden message is unique, pick a unique mesage class, e.g. IPM.Note.MyCompany.MyAdddin.Config.
Hidden messages can be accessed using MAPIFolder.GetStorage in the Outlook Object Model or RDOFolder.HiddenItems in Redemption.

Categories

Resources