How to list all opened calendars in Outlook c# - c#

How can I iterate over all opened (or maybe pinned) calendars in Outlook (as in picture below)?
The top (mine calendar) I can find by:
NameSpace mapiNamespace = outlookApp.GetNamespace("MAPI");
MAPIFolder ownerFolder = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
The below (shared calendars) I can find by:
NameSpace mapiNamespace = outlookApp.GetNamespace("MAPI");
Recipient recipient = mapiNamespace.CreateRecipient("*******Test*****");
if (recipient.Resolve())
{
MAPIFolder sharedFolder = mapiNamespace.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderCalendar);
}
But how to iterate opened (or maybe pinned) shared calendars without knowledge of recipient?
Thank you in advance for your answer.

Related

Outlook email to pdf security prompt

I have a task which i need to create a program that converts outlook email to pdf.
this is my code
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
MAPIFolder rootFolder = outlookNs.Stores["Blah"].GetRootFolder();
List<MailItem> mailItems = new List<MailItem>();
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders)
{
if (folder.Name == "Inbox")
{
Items items = folder.Items;
foreach (object item in items)
{
if (item is MailItem)
{
MailItem mailItem = item as MailItem;
string fileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "New folder", mailItem.EntryID + mailItem.SenderName.Replace("/", "") + ".msg");
mailItem.SaveAs(fileName, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);
}
}
}
}
the code is working but the outlook contains thousands of email. The outlook prompt a message every 10 minutes similar to the screenshot below
is there a way to avoid getting the message? Programatically or a setting will do?
Basically, it's not related to the programming instead of it's related the outlook security settings.
For every version of outlook you can find the settings for this pop-up just follow the instruction on this blog.
You can do a setting in your out look.
Mine is outlook 2013.
File->options : a window opens
In the window select Trust Center
You can see a button Trust center Settings
Options in window changes. Select Programmatic access
UnCheck the radio button Never warn me about suspicious activity (not recommended)
Through program, you can change below registry settings:
Go to "HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\15.0\outlook\security"
Change below settings programatically:
PromptSimpleMAPISend -- 2
PromptSimpleMAPINameResolve -- 2
PromptSimpleMAPIOpenMessage -- 2
By default when outlook is installed, the above values comes with zero value. What I do in my program is, I turn them to "2" programatically just before sending the email and turn them back to zero later point of time.

using Interop.outlook and ews

I am using EWS for searching for and displaying emails. However the final step of the project is to save specific messages as .msg files on the file system. I understand that this is not possible with EWS, so I will need to use Interop.Outlook to accomplish this. My question is: What is the best way to find the Outlook message given the information available from EWS. I have attempted to associate the Message.Id and ConversationId obtained from exchange via EWS with Outlook's messageId but have so far been unsuccessful.
Here is my current (failed) code for finding the ConversationID:
OUTLOOK.Application olApp = new OUTLOOK.Application();
OUTLOOK.NameSpace olNS = olApp.GetNamespace("MAPI");
OUTLOOK.MAPIFolder oFolder = olNS.GetDefaultFolder(OUTLOOK.OlDefaultFolders.olFolderInbox);
OUTLOOK.Items oItems = oFolder.Items;
String sFilter = string.Format("#SQL=\"http://schemas.microsoft.com/mapi/proptag/0x1035001F\" = '{0}'", missive.ConversationID.UniqueId);
object obj = oItems.Find(sFilter);
OUTLOOK.MailItem oEmail = (OUTLOOK.MailItem)obj;
if (oEmail != null)
{
return oEmail;
}
else
{
throw new Exception("MAIL ITEM NOT IN OUTLOOK");
}
As a side: I was looking for a reference for Outlookd filters That is the property names for the [property]=value version; and the hex values for use with the #SQL version. Does someone have a link to a good reference for that?
There's a ConvertIdType request you can use; see: https://msdn.microsoft.com/en-us/library/office/bb856559(v=exchg.140).aspx.
For a listing of MAPI properties and their DASL names and property tag values, see: https://msdn.microsoft.com/en-us/library/office/cc815517.aspx. Although Outlook Spy is a great tool for this as well.

Interop.Outlook - get all emails(Items) within a subfolder

I'm trying to get the body(or any other attribute) of an email which is inside a specific folder in outlook.
I'm using the interop.outlook assembly.
I have done the following so far. But when trying to call an item in myInbox, there are no attributes at all.
Application myApp = new ApplicationClass();
NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Folders["QC"];
The following brings no expected attributes
myInbox.Items[1].
In addition, the next step is to click a link inside the body of the email. just want to know if it's even possible.
Any help will be much appreciated.
This is how I'm doing;
Outlook.Application myApp = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Folders["QC"];
This should bring all mails in Inbox. Then call;
Outlook.MailItem mailItem = myInbox.Items[1];
This mailItem contains all the attributes you need.
Explanation: The mailFolder.Items[1] is an Outlook.Items object which has no attributes you require. You need to cast it to an Outlook.MailItem object to achieve this.

Outlook ContactItem DeleteEvent

Currently I´m writing a sync tool for GMail contacts and outlook, but there is a little problem:
I need an event in my addin when the user deletes a contact, otherwise the sync tool would detect the missing contact on the outlook side and the tool will create the contact from the google side.
I´m accessing all Outlook contacts from the default folder with this code:
Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = GetCurrentNamespace();
Microsoft.Office.Interop.Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
IList<Microsoft.Office.Interop.Outlook.ContactItem> items = new List<Microsoft.Office.Interop.Outlook.ContactItem>();
foreach (var contact in contacts.Items)
{
items.Add(contact as Microsoft.Office.Interop.Outlook.ContactItem);
}
return items;
Edit 1:
I already tried to subscribe to an BeforeDelete Event as John Saunders commented, but with no success. When I try to delete a contact in Outlook the event wont get fired.
Code:
Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = GetCurrentNamespace();
_contactMapiFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
//IList<Microsoft.Office.Interop.Outlook.ContactItem> items = new List<Microsoft.Office.Interop.Outlook.ContactItem>();
this._contacts = new List<Microsoft.Office.Interop.Outlook.ContactItem>();
foreach (var contact in _contactMapiFolder.Items)
{
Outlook.ContactItem item = contact as Microsoft.Office.Interop.Outlook.ContactItem;
item.BeforeDelete += ItemOnBeforeDelete;
this._contacts.Add(item);
}
return this._contacts;
Can anybody provide me an example what events are available for such mapi (especially contact folders) folders are available and how they are working?
It is a really bad idea to set up an event sink on each and every item in a folder.
When Items.ItemRemove even fires, there is no way for you to figure out which item was deleted. You have no choice but to compare the current collection with what you have on the server or in some kind of local cache.
You can try to use Redemption (I am its author) and its RDOItems.ItemRemove event - it passes the value of the PR_INSTANCE_KEY MAPI property from the folder contents table. If you cache the values of the PR_INSTANCE_KEY property for all items ahead of time (you can use RDOITems.MAPITable.ExecSQL for that), you can figure out which item was deleted without looping through all items in the folder.

Outlook Interop 2010 C# Moving Item to Shared OlDefaultFolders.olFolderSentMail

SOmething odd is happening. I'm trying to copy and move and item from a local MAPI folder to a remtoe Sent Items folder using GetSharedDefaultFolder. It works for the inbox folder but not sent items, even though i have permissions to it. Any ideas would be great thank you.
The Error is 'Could not complete the operation. One or more parameter values are not valid'
The code sample is:
Outlook.MailItem cItem = (mailmsg as Outlook.MailItem).Copy() as Outlook.MailItem;
Outlook.NameSpace ns = this.Application.GetNamespace("MAPI");
//ns.Logon()
Outlook.Recipient recipient = ns.CreateRecipient("realusera#domain.com");
recipient.Resolve();
if (recipient.Resolved)
{
MessageBox.Show("Resolved user");
Outlook.MAPIFolder mapifld = ns.GetSharedDefaultFolder(recipient, Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);
cItem = (Outlook.MailItem)cItem.Move(mapifld);
}
According to MS documentation on this API, the olFolderSentMail is one of the default folders that is NOT allowed.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._namespace.getshareddefaultfolder.aspx
Excerpt:
FolderType can be one of the following OlDefaultFolders constants: olFolderCalendar, olFolderContacts, olFolderDrafts, olFolderInbox, olFolderJournal, olFolderNotes, or olFolderTasks. (The constants olFolderDeletedItems, olFolderOutbox, olFolderJunk, olFolderConflicts, olFolderLocalFailures, olFolderServerFailures, olFolderSyncIssues, olPublicFoldersAllPublicFolders, olFolderRssSubscriptions, olFolderToDo, olFolderManagedEmail, and olFolderSentMail cannot be specified for this argument.)

Categories

Resources