Rrecipient List

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Does anyone have an example of C# code on how to get Email Rrecipients
List from Active Directory?
 
Hi Peter,

I did not figure what do you mean very well.
Do you mean you wants to get the items in the Contact Folder in the Outlook
or the recipient of a MailMessage?
Here is some code that will automation outlook to Enumerate the Contact Box
or Enumerate the recipients in a sentmail.

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace AutoOutLook
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
//Enumerate the Contact Box
Outlook.Application olApp = new Outlook.ApplicationClass();
Outlook.MAPIFolder mf =
olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolde
rContacts);
foreach(object ci in mf.Items)
{
if (ci as Outlook.ContactItem != null)
Console.WriteLine(((Outlook.ContactItem)ci).FullName);
}
//Enumerate the recipients in a sentmail
Outlook.MAPIFolder mf2 =
olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolde
rSentMail);
Console.WriteLine(mf2.Items.Count);
Outlook.MailItem mi = (Outlook.MailItem)mf2.Items.GetFirst();
foreach (object recip in mi.Recipients)
{
if (recip as Outlook.Recipient != null)
Console.WriteLine(((Outlook.Recipient)recip).Name);
}
}
}
}

If I misunderstanding you meaning, can you describe what do you mean more
detailed and what do you want to do detailed?
I will appreciate your efforts.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
That's not what I meant, but actually this might work better for me.

Thank you very much!


I am getting the following error in the example:

foreach statement cannot operate on variables of type 'Outlook.Items'
because 'Outlook.Items' does not contain a definition for 'GetEnumerator',
or it is inaccessible

I am using Office XP.

How can iterate through all of the Items either using foreach or for loop?
 
Hi Peter,

The Object Modal of outlook catalogized the items as a few MAPI folders,
e.g. Contacts, Send Mail and so on.
So if we wants to iterate all the item in the outlook we may need to get
the folder first.
e.g.
Outlook.MAPIFolder mf =
olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolde
rContacts);
The code line above will retrieve the Contact folder and then we can
iterate the items in the folder.

The code I post will runs fine on my side.
Have you tried the exact code I post in my last post?
Which code line gives out the error message?

To isolate the problem, you may try to add a reference to the outlook and
then copy and paste the code below into an console application for a test.

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace AutoOutLook
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Outlook.Application olApp = new Outlook.ApplicationClass();
Outlook.MAPIFolder mailFolder =
olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolde
rInbox);
Outlook.MailItem mItem = (Outlook.MailItem)mailFolder.Items[1];
Console.WriteLine(mItem.Subject);
foreach( object mi in mailFolder.Items)
{
if ( mi as Outlook.MailItem !=null)
Console.WriteLine(((Outlook.MailItem)mi).Subject);
}

}
}
}

You may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I found the problem with the code, Office 10 does not implement a
GetEnumerator() method that's why it does not work on my computer.

But I am back to the original problem, is there a way to get email address
list from GAL using Active Directory or some other method without Outlook or
any other COM programs? Because Outlook displays the dialog box when you
try to access an email address, and that gets annoying realy fast. I am
converting an Outlook Form to C# program, because of the problems with the
new security features and I need to display the Global Address List for the
user.

Peter

Thanks
 
Back
Top