Fast searching an Outlook Address list - c#

I want to search an huge address list from outlook for an AddressEntry by the email address. Searching by name is not problem, as you can write:
Microsoft.Office.Interop.Outlook.AddressEntry = AddressEntries[Name];
But I want to find the Entry by its email. This code is working but extremely slow:
public static string GetUserDataByEmailAddress(string EmailAddress)
{
Microsoft.Office.Interop.Outlook.Application OLApp = null;
bool OutlookWasRunning = false;
string UserName = string.Empty;
if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
OLApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
OutlookWasRunning = true;
}
else
{
OLApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = OLApp.GetNamespace("MAPI");
nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
nameSpace = null;
OutlookWasRunning = false;
}
Microsoft.Office.Interop.Outlook.AddressLists GALs = null;
GALs = OLApp.Session.AddressLists;
if (GALs == null) { throw new System.Exception("ERROR: Unable to get address book collection from MS Outlook!"); }
Microsoft.Office.Interop.Outlook.AddressList gal = null;
gal = GALs["Globale Adressliste"];
if (gal == null) { throw new System.Exception("ERROR: Unable to get address book 'Global Address List' from MS Outlook!"); }
foreach (Microsoft.Office.Interop.Outlook.AddressEntry ent in gal.AddressEntries)
{
if(ent.Address == EmailAddress) { UserName = ent.Name; }
}
if (!OutlookWasRunning) { OLApp.Quit(); }
return UserName;
}
Ok, but this way works very slow. Now, I tried to use the AddressEntries as an IEnumeratable to do this:
var output = from a in gal.AddressEntries.AsQueryable() where (a as Microsoft.Office.Interop.Outlook.AddressEntry).Address == EmailAddress select a;
Doing that, I get the error:
Severity Code Description Project File Line Suppression State Error CS1936 Could not find an implementation of the query pattern for source type 'IQueryable'. 'Where' not found.
Does anybody know an fast way to search for the correct AddressEntry?
Regards,
Jan

Do not loop through all items in a container - some containers can contain tens of thousands of entries or more.
You can use Namespace.CreateRecipient / Recipient.Resolve in OOM - that will resolve a name (or address) against all containers. This is equivalent to typing a name in the To edit box in Outlook and hitting Ctrl+K.
If you want to resolve against a particular container (e.g. "All Users" GAL container), you would need to use Extended MAPI (C++ or Delphi only). You can use Redemption (I am its author - any language) - it exposes RDOSession.AddressBook.ResolveName / ResolveNameEx and RDOAddresList.ResolveName / ResolveNameEx .
Keep in mind that resolving against a particular container (PR_ANR resolution in MAPI) might not work if using an SMTP address (GAL limitation).

Related

Getting header information using c#

I am trying to create an outlook add-in in C# which collects header information from new emails in the inbox. Googling it says to use the following code to get the header information for the email.
mailitem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F/")
However when I use this the error Object reference not set to an instance of an object. Click the schema address also says the resource is no longer there is there another way of getting this or do I need to use a different language?
For reference I have added the below.
private void Quarantine()
{
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = (Outlook.Items)inBox.Items;
Outlook.MailItem MailItem = null;
items.Restrict("[UnRead] = true");
var destFolder = inBox.Folders["test"];
string StrRegex = #"(Final Score - [-][0-9] | Final Score - [2][0 - 1] | Final Score - [0 - 1][0-9])";
Regex Reg = new Regex(StrRegex);
foreach (object email in items)
{
MailItem = email as Outlook.MailItem;
String Header= MailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F/");
if (!(Reg.IsMatch(Header)))
{
MailItem.Move(destFolder);
}
}
}
}
as operator will return null is the object is not a MailItem. Your code never checks for that. You can have objects other than MailItem in the Inbox folder, such as ReportItem, MeetingItem, etc.

Outlook email search crashes

i am working on a code that get's a list and checks if the items is in emails subjects, i am trying to go through all the emails to compare to the list but the program crashes after 190 emails or so (there are 289 emails in the folder)
I searched online for solutions for this problem and nothing worked for me
oApp = new Microsoft.Office.Interop.Outlook.Application();
// Get the MAPI namespace.
oNS = oApp.GetNamespace("mapi");
// Log on by using the default profile or existing session (no dialog box).
oNS.Logon(Missing.Value, Missing.Value, false, true);
// Alternate logon method that uses a specific profile name.
// TODO: If you use this logon method, specify the correct profile name
// and comment the previous Logon line.
//oNS.Logon("profilename",Missing.Value,false,true);
//Get the Inbox folder.
oInbox = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
//Get the Items collection in the Inbox folder.
oItems = oInbox.Items;
MessageBox.Show(oItems.Count+"");
oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetLast();
public void SearchEmail()
{
if (counter<oItems.Count)
{
try
{
string s;
if (oMsg != null)
{
s = oMsg.Subject;
for (int i = 0; i < allTickets.Count; i++)
{
label2.Text = oMsg.Subject;
string ticketID = allTickets[i].ToString();
if (s.Contains(ticketID) && oMsg.UnRead)
{
unreadTickets.Add(ticketID);
}
}
}
oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetPrevious();
counter++;
button2.Text = counter + "";
}
//Error handler.
catch (Exception x)
{
//MessageBox.Show(x.ToString());
}
}
}
the catch exception gives me this:
unable to cast com object of type 'system.__comobject' to interface type 'microsoft.office.interop.outlook
and afterward i am getting this error:
nullreferenceexception object reference not set to an instance of an object
Keep in mind that Inbox can contain items other than MailItem, such as ReportItem or MeetingItem. That would explain the cast error. You need to treat items as a generic object and read the Class property (exposed by all OOM objects) using reflection - if it is 43 (olMailItem), you can cast it to MailItem. Or you can use the "as" operator and check for null.
Also keep in mind that the Items collection is not sorted in any particular order unless you first call Items.Sort and specify a sort order; otheriwise what you get from Items.GetLast is undetermined.

C# Outlook VSTO add-in error while trying to get item in an Exchange setup

I'm developing an Office VSTO add-in in C# which looks up calendar appointments in Outlook, and reads/writes some data in them.
Recently, one of the clients had issues with the add-in, namely they can't read/write the calendar appointment, and it throws an exception :
The operation failed.
There's not much info from the exception log, but I suspect they have synchronization problems with Exchange.
I asked the client, and they said, that they also have a random popup in Outlook as well, which sometimes happens when there's a mishap while syncing with Exchange. I told them to 'repair' the Outlook data files, but that didn't fix the problem.
The outlook items are basically looked up based on their Outlook EntryIDs OR a Subject (the subjects are unique, and for the sake of simplicity I translated the code a little bit)
...main alghorythm...
Outlook.AppointmentItem calAppointment = null;
calAppointment = SearchforCalendarMatch(EntryID, Subject); //we try to find either by EntryID or by Subject
if (calAppointment != null)
{
calAppointment.Start = StartDate;
calAppointment.End = FinishDate;
calAppointment.Body = Notes;
calAppointment.Save(); //we're changing the found calendar appointment here
}
...
public Outlook.AppointmentItem SearchforCalendarMatch(String EntryID, String Subject)
{
Outlook.NameSpace ns = null;
Outlook.MAPIFolder calendarFolder = null;
Outlook.Items calendarFolderItems = null;
Outlook.Items filteredcalendarFolderItems = null;
Outlook.AppointmentItem calAppointment = null;
Outlook.Application OutlookApp = new Outlook.Application();
outlookversion = OutlookApp.Version;
ns = OutlookApp.Session;
//Try to find the calendar appointment by the EntryID
dynamic OutlookItem = ns.GetItemFromID(t.Text28);
if (OutlookItem != null)
{
if (OutlookItem is Outlook.AppointmentItem)
{
Outlook.AppointmentItem foundItem = (Outlook.AppointmentItem)OutlookItem;
return foundItem;
}
}
//If the EntryID was missing, we try to find the calendar appointment by the Subject.
//(original code is very long, and there are multiple things here, but let's just assume that 100% sure that the subject is unique, so it will find it)
String SubjectMatch = "[Subject] = '" + Subject + "'";
filteredcalendarFolderItems = calendarFolderItems.Restrict(SubjectMatch);
for (int i = 1; i <= filteredcalendarFolderItems.Count; i++)
{
//appointment has to be one of these
calAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)filteredcalendarFolderItems[i];
if (!calAppointment.IsConflict) //conflict check here, not sure if it helps at all
{
return calAppointment; //this is not the complete code, but this is the basic idea of it.
}
}
}
Any ideas how I could make the application recognize these failed Exchange syncs, and handle them differently?
I would still like to sync in these cases, if it's possible... (change the 'local data' in Outlook, then let Outlook handle everything from that on)
You need to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

Error when opening some Outlook MailItems with GetItemFromID using EntryID from Outlook Spy

When I Use OutlookSpy to get the EntryIDs from MailItems in a particular folder and supply them to the following code:
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
try
{
object obj = mapiNameSpace.GetItemFromID(sEntryID);
if (obj is Outlook.MailItem)
{
var getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(sEntryID);
getItem.Display();
}
}
catch (Exception Ex)
{
Global.Common.LogError("Error accessing MailItem", Ex, "EntryID " + sEntryID + " not found in " + sFolder, "Warning");
}
I get unknown messaging errors for some EntryID values and successful display of the messages in Outlook with others. Can anyone suggest what attributes the MailItems might have which will affect whether I can display them successfully using GetItemFromID or any other method of displaying all messages by EntryID reliably?
Was the message store where the message resides touched in the active Outlook session? The way MAPI providers work, when a provider is loaded by MAPI, it registers the set of entry id guids (bytes 5-20 in the entry id) that it will handle. If the particular PST store has not been touched in the current session, MAPI does not know anything about its entry ids.
You can either access all the stores in the current session first (to make sure MAPI knows about their entry ids) or use the store entry id (second parameter, optional) when calling GetItemFromId - this way Outlook will open the store first, then ask the store to open the item. You can also call Namespace.AddStore / AddStoreEx to load the given PST file if it is not already in the current profile.
You might also want to log the exception details (Ex.Message) in your exception handler.
Thanks to all the respondents - this explains why sometimes particular messages would open and sometimes they would not. By getting the StoreId using the following code:
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Object oStoreID = Common.GetFolder(myApp, sFolder).StoreID;
try
{
object obj = mapiNameSpace.GetItemFromID(sEntryID,oStoreID);
if (obj is Outlook.MailItem)
{
Outlook.MailItem getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(sEntryID,oStoreID);
getItem.Display();
}
}
Where
public static Outlook.Folder GetFolder(Outlook.Application App, string folderPath)
{
Outlook.Folder folder;
string backslash = #"\";
try
{
if (folderPath.StartsWith(#"\\"))
{
folderPath = folderPath.Remove(0, 2);
}
String[] folders =
folderPath.Split(backslash.ToCharArray());
folder =
App.Session.Folders[folders[0]]
as Outlook.Folder;
if (folder != null)
{
for (int i = 1; i <= folders.GetUpperBound(0); i++)
{
Outlook.Folders subFolders = folder.Folders;
folder = subFolders[folders[i]]
as Outlook.Folder;
if (folder == null)
{
return null;
}
}
}
return folder;
}
catch { return null; }
}
All MailItems now display in Outlook.
I'll just throw this in here for posterity-- Outlook 2002 requires that the entry ID supplied to GetItemFromID use upper-case hex characters.

Get closest Domain Controller in current AD site without hard coding information

For instances when Active Directory takes too long to replicate data between sites, I need to ensure that the local AD replica contains the most up to date information.
How can I get a list of DomainControllers for the current site?
I haven't found anything on Codeproject or on StackOverflow
Going to all this trouble is probably wasted effort. Unless you are experiencing issues with the built in logic for finding a domain controller you should just go with the built in method that returns one. According to Microsoft it automatically tries to find the closes one: http://technet.microsoft.com/en-us/library/cc978016.aspx.
Just use the static DomainController.FindOne method and pass in your directorycontext.
Update
Alright, try the code below, let me know how it works for you. It pings each, returns the roundtrip time, if -1 (no connection) it skips it. Flags PDC status if present. Orders by PDC status, followed by ping round trip.
static void Main(string[] args)
{
var dcsInOrder = (from DomainController c in Domain.GetCurrentDomain().DomainControllers
let responseTime = Pinger(c.Name)
where responseTime >=0
let pdcStatus = c.Roles.Contains(ActiveDirectoryRole.PdcRole)
orderby pdcStatus, responseTime
select new {DC = c, ResponseTime = responseTime}
).ToList();
foreach (var dc in dcsInOrder)
{
System.Console.WriteLine(dc.DC.Name + " - " + dc.ResponseTime);
}
System.Console.ReadLine();
}
private static int Pinger(string address)
{
Ping p = new Ping();
try
{
PingReply reply = p.Send(address, 3000);
if (reply.Status == IPStatus.Success) return (int)reply.RoundtripTime;
}
catch { }
return -1;
}
First, I'll answer the question that you actually asked:
System.DirectoryServices.ActiveDirectory.ActiveDirectorySite.GetComputerSite().Servers
But it seems like you're asking how to make sure that you're talking to the closest domain controller possible. Windows doesn't exactly provide this functionality, the best it will do is give you a domain controller in the same site that the code is running from.
I think the first thing to check is that you have your sites and subnets configured correctly. Run Active Directory Sites and Services, and make sure that subnets and domain controllers are assigned to the correct sites.
This MSDN page (and the Technet article in Peter's answer) says that you must be searching by the DNS name for the DC Locator to attempt to find a DC in the current site. I don't know if the Name property of the Domain class is the DNS domain name.
I have to assume that DomainController.FindOne is a wrapper for DsGetDcName. At that link, you can find how to turn on tracing for that function. You can use this if you still have problems, or maybe you should just PInvoke this function.
Here is a code sample that has no hard coding of DCs. Comments and criticism are welcome.
/// <summary>
/// For best results ensure all hosts are pingable, and turned on.
/// </summary>
/// <returns>An ordered list of DCs with the PDCE first</returns>
static LinkedList<DomainController> GetNearbyDCs()
{
LinkedList<DomainController> preferredDCs = new LinkedList<DomainController>();
List<string> TestedDCs = new List<string>();
using (var mysite = ActiveDirectorySite.GetComputerSite())
{
using (var currentDomain = Domain.GetCurrentDomain())
{
DirectoryContext dctx = new DirectoryContext(DirectoryContextType.Domain, currentDomain.Name);
var listOfDCs = DomainController.FindAll(dctx, mysite.Name);
foreach (DomainController item in listOfDCs)
{
Console.WriteLine(item.Name );
if (IsConnected(item.IPAddress))
{
// Enumerating "Roles" will cause the object to bind to the server
ActiveDirectoryRoleCollection rollColl = item.Roles;
if (rollColl.Count > 0)
{
foreach (ActiveDirectoryRole roleItem in rollColl)
{
if (!TestedDCs.Contains(item.Name))
{
TestedDCs.Add(item.Name);
if (roleItem == ActiveDirectoryRole.PdcRole)
{
preferredDCs.AddFirst(item);
break;
}
else
{
if (preferredDCs.Count > 0)
{
var tmp = preferredDCs.First;
preferredDCs.AddBefore(tmp, item);
}
else
{
preferredDCs.AddFirst(item);
}
break;
}
}
}
}
else
{
// The DC exists but has no roles
TestedDCs.Add(item.Name);
if (preferredDCs.Count > 0)
{
var tmp = preferredDCs.First;
preferredDCs.AddBefore(tmp, item);
}
else
{
preferredDCs.AddFirst(item);
}
}
}
else
{
preferredDCs.AddLast(item);
}
}
}
}
return preferredDCs;
}
static bool IsConnected(string hostToPing)
{
string pingurl = string.Format("{0}", hostToPing);
string host = pingurl;
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 3000);
if (reply.Status == IPStatus.Success)
return true;
}
catch { }
return result;
}
Here's my approach using powershell but I'm sure it's a simple implementation in c#, etc. If DHCP is setup correctly, the Primary DNS server in your subnet should be the closest Domain Controller. So the following code should grab the first DNS IP and resolve it to the hostname of the closest DC. This doesn't require RSAT or credentials and contains no specific properties of the current domain.
$NetItems = #(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -ComputerName $env:COMPUTERNAME)
foreach ($objItem in $NetItems)
{
if ($objItem.{DNSServerSearchOrder}.Count -ge 1)
{
$PrimaryDNS = $objItem.DNSServerSearchOrder[0]
$domain = $objItem.DNSDomain
break
}
}
[System.Net.Dns]::GetHostbyAddress($PrimaryDNS).hostname -replace ".$($domain)",""

Categories

Resources