Using Outlook API to get to a specific folder - c#

I'm trying to write some C# code to get to a specific folder in an Outlook mailbox. I have the following code:
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
Outlook.Recipient oRecip = oNS.CreateRecipient("AccountNameHere");
oRecip.Resolve();
if (oRecip.Resolved)
{
oInbox = oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderInbox);
oInboxMsgs = oInbox.Items;
ItemCount = oInboxMsgs.Count;
Console.Writeline("There are {0] items.", ItemCount.ToString())
}
This will get me to to the "Inbox" folder. I'm trying to get to a folder at the same level as the Inbox folder. I believe I need to use GetFolderFromID instead of GetSharedDefaultFolder, but I don't understand how to use it. Is there a way to iterate through all the top level folders? How might I determine the EntryID and StoreID of the folder?
Thanks!

You can use the Folders collection member of the Outlook.NameSpace object. That way you can iterate through the collection and find your folder by it's name. In case you still want to use GetFolderFromID, you can use OutlookSpy tool to get the EntryID and StoreID values.

Related

Outlook AutoComplete Email List

I want extract list all autocomplete email addresses in outlook 2016 (office 365) using C#.
I am using following code;
NameSpace olNS = this.Application.GetNamespace("MAPI");
MAPIFolder inboxFolder = olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
StorageItem storage = inboxFolder.GetStorage("IPM.Configuration.Autocomplete", OlStorageIdentifierType.olIdentifyByEntryID);
PropertyAccessor propertyAcc = storage.PropertyAccessor;
byte[] bytes = propertyAcc.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x7C090102");
When I execute above code I get the following error
Cannot create StorageItem in this folder. Either the folder is read-only, or a storage item is not allowed in this folder.
Please help in this regard
Thanks
You need to pass OlStorageIdentifierType.olIdentifyByMessageClass instead of OlStorageIdentifierType.olIdentifyByEntryID:
NameSpace olNS = this.Application.GetNamespace("MAPI");
MAPIFolder inboxFolder = olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
StorageItem storage = inboxFolder.GetStorage("IPM.Configuration.Autocomplete", OlStorageIdentifierType.olIdentifyByMessageClass);
PropertyAccessor propertyAcc = storage.PropertyAccessor;
byte[] bytes = (byte[])propertyAcc.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x7C090102");
The code listed above works like a charm on my machine!
However, be aware, the GetStorage returns an error if the store type of the folder is not supported. The following stores return an error when GetStorage is called:
Hotmail store
Internet Message Access Protocol (IMAP) stores
Delegate stores
Public folder stores

how to access my yahoo or gmail account in outlook (c#)?

I have 3 data files:
Outlook data file (is empty)
Gmail data file (from my gmail pop3)
Yahoo data file (from my yahoo imap)
I can access the Outlook data file inbox (which is always empty, I don't know how to automatically move from my google and yahoo account to my outlook data file) with
this code:
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
I have 2 questions:
How can I make my gmail and yahoo account automatically move to my outlook data file?
How to code to access my inbox in my gmail and yahoo account?
I have tried this function to make return to my MAPIFolder:
public Outlook.MAPIFolder GetInbox(string userName)
{
Outlook.Application oOutlook = new Outlook.Application();
Outlook.NameSpace oNs = oOutlook.GetNamespace("MAPI");
Outlook.Recipient oRep = oNs.CreateRecipient(userName);
Outlook.MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, Outlook.OlDefaultFolders.olFolderInbox);
return inbox;
}
But it didn't work. help me pleaseee..
I'm not sure I understand your first question, would you like to join all the data files into the main outlook data file?
About your second question, Outlook data files are represented by stores in the API, and if you call GetDefaultFolder on the Session object, you'll always end up with getting the default store's default folder (Inbox of your Outlook data file).
You can list all the store files and use GetDefaultFolder on them to retrieve the inbox folders for each store/data file:
Outlook.Stores stores = this.Appliction.Session.Stores;
foreach( Store store in stores )
{
Outlook.Folder inboxOfStore = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//Do stuff with your inbox folder.. Check store properties for infomation on which data file this store represents
}
See documentation for more information:
http://msdn.microsoft.com/en-us/library/office/bb176405%28v=office.12%29.aspx

Open Specific MailItem in Outlook from C#

I want to open a specific email in Outlook from my C# winforms application.
At the moment I have got the following code:
//...Get Folder & Entry ID for last Email in Sent Box
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder mySentBox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook.MailItem myMail = ((Outlook.MailItem)mySentBox.Items[1]);
string guid = myMail.EntryID;
string folderEntryID = mySentBox.EntryID;
string folderStoreID = mySentBox.StoreID;
string mailAddressee = myMail.To;
MessageBox.Show(mailAddressee);
//...Attempt to Open that Email at a later date
Outlook.MAPIFolder getFolder = (Outlook.MAPIFolder)mapiNameSpace.GetFolderFromID(folderEntryID, folderStoreID);
Outlook.MailItem getItem = (Outlook.MailItem)getFolder.Items.Find("[EntryID] = " + guid);
getItem.Display();
The first an second parts of the code will be run at different times, although they are in the same Method for testing at the moment.
The first part obtains the relevant IDs for the mst recently sent email from Outlook. This part seems to work fine as evidence my the Messagebox I've built in.
The second part however is not working and I'm struggling to find the right code to access and open that specific mailItem having obtained its ID and folder location in the first part.
Anyone able to complete this little project for me please.
Done it at last by replacing the following lines of code . . .
Outlook.MAPIFolder getFolder = (Outlook.MAPIFolder)mapiNameSpace.GetFolderFromID(folderEntryID, folderStoreID);
Outlook.MailItem getItem = (Outlook.MailItem)getFolder.Items.Find("[EntryID] = " + guid);
with this . . .
Outlook.MailItem getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(guid, folderStoreID);
You can not use EntryId with the _Items.Find method. The MSDN reference is here.
There is also a remark which could be interesting for your project:
"The Entry ID changes when an item is moved into another store, for
example, from your Inbox to a Microsoft Exchange Server public folder,
or from one Personal Folders (.pst) file to another .pst file.
Solutions should not depend on the EntryID property to be unique
unless items will not be moved."
MailItem.EntryID Property (Outlook).

Synchronize single IMAP folder

I am trying to write an application that presents the user with a list of mail headers from a selected folder from shared IMAP folder tree. Users of the application use Outlook 2007 or 2010, and have the shared IMAP folder tree mapped to their account.
I am able to access a folder and iterate over the mail items using Outlook Interop like this:
Outlook.Application outlook = new Outlook.Application();
Outlook._NameSpace ns = outlook.GetNamespace("MAPI");
Outlook.Folder folder = MyFolderFinder(...);
foreach (var item in folder.Items)
{
if (item is Outlook.MailItem)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;
// do something with mailItem
}
}
Unfortunately this method does not appear to synchronize the folder, so unless the user does a manual "Update Folder" using Outlook, they may see a stale list.
Is there a way to programmatically synchrionize a single IMAP folder using Outlook Interop?
Using the new mail receive event, I think you can do this.
private void ThisApplication_Startup(object sender, System.EventArgs e)
{
this.NewMail += new Microsoft.Office.Interop.
Outlook.ApplicationEvents_11_NewMailEventHandler(
ThisApplication_NewMail);
}
void ThisApplication_NewMail()
{
}
Reference: http://msdn.microsoft.com/en-us/library/ms268998(v=vs.80).aspx

Outlook Interop: how to iterate all the calendars?

I want to get all events from all calendars, how do i iterate thru all calendar folders and then all events for each calendar?
If I had to guess, though I'm just getting in to Outlook myself, I would suggest the following:
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Then something along the lines of
foreach (outlook.MAPIFolder subFolder in folder.Folders)
{
// do something with subFolder
}
And you could probably create something recursive to exhaust all possibilities of the MAPIFolder.Folders property.
EDIT Ultimately, try stepping through in the debugger one you've gotten the default folder and see what you're left with. My guess is this will have the information you need.

Categories

Resources