Outlook Interop AddressEntry Exception
I've written an outlook plugin that retrieves the sender's SMTP email address for a mailitem. It is working fine on most machines, however, I have one machine (my new development machine) that throws a COMException every time it tries to resolve the SMTP address for an email from an exchange user. Below is the code I'm using...
private string SenderEmail(MailItem item)
{
if (item == null)
{
return "";
}
else
{
string senderEmail = string.Empty;
if (item.SenderEmailType.ToUpper() == "EX")
senderEmail = GetEmailAddressFromOU(item.SenderEmailAddress);
else
senderEmail = item.SenderEmailAddress;
return senderEmail;
}
}
private string GetEmailAddressFromOU(string ouName)
{
string emailAddress = string.Empty;
NameSpace oNS = ((Microsoft.Office.Interop.Outlook.Application)OutlookAppObj).GetNamespace("MAPI");
Recipient recip = oNS.CreateRecipient(ouName);
recip.Resolve();
ExchangeUser exUser = recip.AddressEntry.GetExchangeUser();
emailAddress = exUser.PrimarySmtpAddress开发者_StackOverflow社区;
Marshal.ReleaseComObject(exUser);
Marshal.ReleaseComObject(recip);
Marshal.ReleaseComObject(oNS);
return emailAddress;
}
The following COMException occurs when accessing the AddressEntry property of the Recipient object:
Message = "The attempted operation failed. An object could not be found."
I'm using Windows 7 (64bit), using Outlook 2010, however this same code works on other machines with the same OS and Outlook version. It also works fine on my previous development machine which was also Windows 7 (32bit) and Outlook 2010.
I've searched StackOverflow and Google for any resolution, but haven't found any.
Can anyone shed some light on this problem?
Still not sure what was causing the problem, but deleting all my E-Mail accounts in Outlook and re-adding them, fixed the problem.
To fix account problems try deleting the OST file.
This link explains how to do it:
http://social.technet.microsoft.com/Forums/en/w7itprogeneral/thread/d8fe1d52-4f95-4158-ab2f-13cab5cbabf9
精彩评论