简体   繁体   中英

Outlook AddIn: creating new ContactItem

I use the following code to assign some data to an appropriate ContactItem (Sender) of given MailItem. If Sender.GetContact() returns null, I'm trying to create a new ContactItem.

        Outlook.MailItem myItem = (Outlook.MailItem)this.OutlookItem;            
        Outlook.ContactItem currentContact = myItem.Sender.GetContact();
        if (currentContact != null)
        {
            currentContact.Body = "Example";
            currentContact.Save();
        }
        else
        {
            currentContact = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olContactItem) as Outlook.ContactItem;
            currentContact.Email1DisplayName = myItem.SenderName;
            currentContact.Email1Address = myItem.SenderEmailAddress;
            currentContact.Email1AddressType = myItem.SenderEmailType;
            currentContact.Body = "Example";
            currentContact.Save();                
        }

But this does not seem to work fine for me. The next time I'm getting the contact of that MailItem (see the following code), it returns null. Again. And again.

        Outlook.MailItem myItem = (Outlook.MailItem)this.OutlookItem;            
        Outlook.ContactItem currentContact = myItem.Sender.GetContact();

Is there something wrong? It seems like that new ContactItem is not assigned to Sender.

GetContact will return ContactItem object only if the outgoing message (does not work for incoming) had the contact explicitly added as a recipient.

GetContact will not check if you happen to have a contact item with the same email address.

If you need to find a matching contact, explicitly use MAPIFolder.Items.Find on the Contacts folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM