0

I try this code :

Microsoft.Office.Interop.Outlook.Application myApp = new   Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");

mapiNameSpace.Logon(null, null, false, false);
mapiNameSpace.Logon("MyEmailID", "PasswordOfMyEmail", Missing.Value, true);

Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
MessageBox.Show(myInbox.Items.Count + "");

But the MessageBox display me 0 (myInbox.Items.Count).

So maybe I am not able to access my outlook account !!!

I use Visual Studio 2010.

Can someone help me please ?

Hassan IS
  • 15
  • 7
  • possible duplicate of [Fetching all mails in Inbox from Exchange Web Services Managed API and storing them as a .eml files](http://stackoverflow.com/questions/20662855/fetching-all-mails-in-inbox-from-exchange-web-services-managed-api-and-storing-t) – Aron Jul 17 '14 at 08:26
  • Excuse me, but it's not duplicated, I'm still in my add in and I want to get my mails received or (any other items) from my Outlook 2010. – Hassan IS Jul 17 '14 at 08:38
  • What do you MEAN by "add in"? It looks like you are connecting to an unrelated Outlook instance and trying to get emails out. There appears to be nothing "add in" about your code. If you just care about how many emails you have, please take a look at EWS instead. Otherwise, please state how your program is an "add in". – Aron Jul 17 '14 at 08:41
  • I mean by add-in a plug-in C# added for outlook – Hassan IS Jul 17 '14 at 08:47
  • Is it VSTO? COM Interop? What technology? AND WHY WILL YOU REFUSE TO USE EWS? – Aron Jul 17 '14 at 08:49
  • Yes it's VSTO 2010, this technology is to develop a plug-in for outlook in C #, I do not refuse the use of EWS but I do not know EWS and this is not my mission. – Hassan IS Jul 17 '14 at 08:55

2 Answers2

0

Firstly, if you are in an add-in, you should never create a new instance of the Outlook.Application object - you already have it exposed to your addin through globals.

Secondly, do not call Logon - Outlook will do that for you. That method is really for the external applications, not addins.

Thirdly, you call Logon once with null for the profile name. If you do not want to pass an optional parameter, pass Missing.Value, not null. And the second call to Logon (why do you need it?) will do absolutely nothing. Passing any email will not work anyway - the parameter must be the profile name as seen in Control Panel | Mail | Show Profiles.

And most importantly, when do you make that call? Does your Inbox actually contain any messages? Or is it a brand new profile and Outlook did not yet have a chance to download the messages from the mailbox?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • you're right, then I took another method. : Explorer currExplorer = Globals.ThisAddIn.Application.ActiveExplorer(); MAPIFolder myInbox = Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder; object item = currExplorer.Selection[1]; if (item is MailItem) { ... } Thank you for your explanation :) – Hassan IS Jul 18 '14 at 06:03
0

So you can use :

Explorer currentExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
MAPIFolder myInbox = Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder;

try 
{
    if ( (currentExplorer != null) && (currentExplorer.Selection != null) && (currentExplorer.Selection.Count > 0) )
    {
        object item = currentExplorer.Selection[1];
        if (item is MailItem) 
        {
            MailItem mailItem = item as MailItem;
            MessageBox.Show(myInbox.Items.Count + "");
        }
    }
}
Hassan IS
  • 15
  • 7