I have built an Outlook 2010 Addin using C#. I created an Inspector wrapper and I am using it to capture when an item is added to the Sent Items folder.
I then capture some details about that item and do something with it.
It's been working great, but I've recently come across someone who has multiple email accounts in Outlook. My addin seems only to capture the items from 1 of the sent item folders.
sentfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
sentitems = sentfolder.Items;
sentitems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(sentitems_ItemAdd);
_wrappedInspectors = new Dictionary<Guid, InspectorWrapper>();
_inspectors = Globals.ThisAddIn.Application.Inspectors;
_inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(WrapInspector);
Obviously I just need to iterate through the folders for the different accounts and identify each of the sent items folders but I am unsure how to achieve that.
If the user has multiple email accounts, the Session.Stores or Session.Accounts should provide access to the other shared mailboxes they use.
See this related SO post on how to get access to shared mailboxes.
Related
I'm working on a plugin for Outlook. I need to always have an up-to-date list of Outlook accounts (File->Add Account), but when I take this data from NameSpace.Accounts, they are relevant only at the time Outlook starts. If I add another account or delete the old one, these changes will not be reflected in NameSpace.Accounts. Is there any way to refresh this data?
I am using the following code to get accounts
var outlookObj = new Outlook.Application();
var accounts = outlookObj.Application.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
//do something
}
Firstly, do not use new Outlook.Application() in an addin - an instance of the Application object is provided to your addin on startup, and it is not handicapped by the security prompts..
Also try to avoid caching/keeping a reference to the Accounts or Account object.
If using Redemption is an option (I am its author), it exposes RDOAccounts collection (which is never cached) and, unlike the Accounts collection in OOM, also exposes events when accounts are added/deleted/modified/rearranged.
The Outlook object model doesn't provide such events for accounts. But you may try to handle the Stores.StoreAdd event which is fired when a Store has been added to the current session either programmatically or through user action.
Note, as a possible workaround you may consider using a third-party library such as Redemption which can monitor changes to Accounts in Outlook.
There is no need to create a new Application instance in the code:
var outlookObj = new Outlook.Application();
Instead, in VSTO based add-ins you need to use the ThisAddin class where the Application property is provided out of the box.
I'm currently experiencing some strange behavior when sending e-mails using Outlook (Office 365 Pro Plus).
Our software can generate e-mails and show a new mail compose to the user with attachments which they selected from our software. (We use both Redemption from our software as Outlook Object Model using an Outlook Add-in Express add-in)
Below you can find the code for generating the e-mail using Redemption:
bool createEmail = false;
RDOSession session = new RDOSession();
session.Logon("", "", false, false, null, false);
RDOFolder folder = session.GetDefaultFolder(rdoDefaultFolders.olFolderDrafts);
RDOMail mail = folder.Items.Add("IPM.Note");
... Code for selecting receivers (returns string with emailadresses) ...
... Code for converting word documents to PDF / Adding the attachments to the mailitem ...
RDOUserProperty userProp = mail.UserProperties.Add("DMS_AttachmentCount", rdoUserPropertyType.olInteger, false, false);
userProp.Value = mail.Attachments.Count;
mail.Display();
I noticed that I'm not using
mail.Save();
Could that be the issue?
When we show the user the generated e-mail all attachments are in place and the user is able to open the attachments.
So far so good...
Here comes the problem:
On sending the e-mail, sometimes (i mean really sometimes) some of the
attachments are deleted from the e-mail. When the user goes to it's
sent items and selects the e-mail, some of the attachments are
deleted.
Since this occurs randomly, I'm not able to debug this issue.
What have I tried already:
Turn off ESET Nod32 and all of it's e-mail components
Disabled Office Secured Mode
Exclude certain file extensions from being scanned.
Checked for third party add-ins, so far only the list below is installed
The rest of the add-ins are default by Outlook
My question(s):
Is there somebody out there that has experienced the same problem?
How can I determine what happens inside the Item Send event?
What else can I turn off or apply on the server that might prevent this issue from happening?
Thanks.
We are using the EWS API (2010) with usage : SubscribeToPullNotifications ( FolderIDs, 300, CurrentWatermark, EventType.NewMail ) to read the emails from an exchange account into our program. However, even though the folder ids that are sent unique to each folder, when the email events are received back, i gets only the events from all folders except Sent Items folder.
The complete flow which i am using :
Initial Sync : All folders [including sent items] ) are synced along with their mails. This information is stored to DB.
Subsequent sync : The unique folder ids, along with the water mark received is sent for taking the next / updated batch of mails. (This doesn't give sent items folder mails).
Did any one face this issue ?. Do i need to do any extra steps to ensure the sent items folders are also synced and their mails are received in.
Your subscribing to the EventType.NewMail event this event only occurs when new email is delivered to the Inbox. When somebody sends an Email a copy of that email is saved to the SentItems folder but this won't trigger the NewMail event because its not a delivery its just a creation so you need to also listen for EventType.Created to pickup new emails that are added to the SendItems folders.
A good tool to test this with is the EWSEditor https://ewseditor.codeplex.com/ it will let you subscribe to all events and you can then see exactly what event occurs when actions are taken on a Mailbox without needing to write any code.
Cheers
Glen
Hello I am wondering if it is possible to send a search query to Outlook 2010 from my WinForms app. That is, not search the .PST file as I've been searching around and finding, I'm trying to display a list of results in Outlook as if I typed in the search box myself.
If it is possible, any example code would be helpful. Additionally, is it possible to directly perform a search in All Mail Items versus, usually when you do a search it combs the current folder. Thanks.
If you want to access the Outlook data (mail for example) you have to add a COM reference to the Microsoft Outlook X.X Object library.
For Outlook you can use COM interop. Open the Add Reference dialog and select the .NET tab, then add a reference to the Microsoft.Office.Interop.Outlook assembly.
Afterwards don't forget to add the namespace "Microsoft.Office.Interop.Outlook" to your using clauses.
Now you can create an instance of the Outlook application object:
Microsoft.Office.Interop.Outlook.Application outlook;
outlook = new Microsoft.Office.Interop.Outlook.Application();
Let's perform a query on your inbox:
MAPIFolder folder =
outlook.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox);
IEnumerable<MailItem> mail =
folder.Items.OfType<MailItem>().Where(m => m.Subject == "Test").Select(m => m);
You specify the folder you want to search as a parameter for the GetDefaultFolder(...) method. You can specify other folder besides the inbox.
olFolderSentMail
olFolderOutbox
olFolderJunk
...
Check out each possible value on MSDN:
OlDefaultFolders Enumeration
Stefan Cruysbergs created an OutlookProvider component which acts as a wrapper for the Outlook application object. You can use LINQ to query this provider and retrieve data such as the contacts, mail...etc.. Just download his code and check it out. This should be enough to get you started.
I am in the process of writing an application that sets a signature based on pre-acquired data for each Microsoft Outlook account(a user may have multiple Outlook accounts for various purposes).
I am able to set a signature for the default account, but I have yet to find how to set a signature for each Outlook account individually. I have done a lot of my own research and poked around the Microsoft.Office.Interop objects without much luck.
Is there a way to achieve this?
To choose the Outlook profile programmatically, you just use
Microsoft.Office.Interop.Outlook.Application App =
new Microsoft.Office.Interop.Outlook.Application();
NameSpace mapi = App.GetNamespace("MAPI");
mapi.Logon(profileName);
obviously setting the profileName to what is shown in the dropdown list upon starting Outlook (if you do not set a default profile in the control panel email settings).
This however is problematic in a number of ways since Outlook does not support multiple sessions even though the MAPI logon does:
http://msdn.microsoft.com/en-us/library/bb219914(v=office.12).aspx
Meaning: if Outlook is already running, you can even set NewSession to true, but it won't help. It will give you the currently-logged-in profile regardless of what name you set. If you have an Outlook zombie (I got that while testing, check with task manager), i.e. an Outlook without an UI showing up, the problem is the same.
If you can ensure Outlook does not run while doing stuff with signatures, you should be fine though.