使用jacob库删除和更新Outlook联系人

时间:2013-06-28 09:20:36

标签: java outlook jacob

我正在使用jacob库。使用jacob库并按照this教程我可以在outlook中添加联系人。现在我想使用jacob删除和更新该联系人。我想知道有没有办法删除使用jacob的Outlook联系人。

我正在使用此代码在Outlook中添加联系人。这里的电子邮件ID是唯一ID。

        ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
        Dispatch oOutlook = axOutlook.getObject();
        Dispatch createContact = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(2)).toDispatch();

        Dispatch.put(createContact,"LastName",cont.getLastName());
        Dispatch.put(createContact,"FirstName",cont.getFirstName());
        Dispatch.put(createContact,"Title",cont.getTitle());
        Dispatch.put(createContact,"Email1Address",cont.getPrimaryEmail());

        Dispatch.call(createContact, "Save");

1 个答案:

答案 0 :(得分:1)

JACOB是一个围绕COM IDispatch调用的非常薄的包装器,因此如果您想知道如何在Outlook中执行任何特定任务,那么起点将是官方Outlook Object Model documentation

您的特定情况,查找和删除联系人,是通过

执行的
namespace = outlookApplication.GetNamespace("MAPI")
contactsFolder = namespace.GetDefaultFolder(olFolderContacts)
contact = contactsFolder.items.find( "[Email1Address] = 'mail@server.com' )

if (contact != null)
{
    contact.Delete
}

下半部分工作是将这些调用转换为JACOB发言。假设您找到了联系人项目,代码将类似于

ActiveXComponent outlookApplication = new ActiveXComponent("Outlook.Application");
Dispatch namespace = outlookApplication.getProperty("Session").toDispatch();

Dispatch contactsFolder = Dispatch.call(namespace, "GetDefaultFolder", new Integer(10)).toDispatch();
Dispatch contactItems = Dispatch.get(contactsFolder, "items");
String filter = String.format("[Email1Address] = '%s'", cont.getPrimaryEmail());
Dispatch contact = Dispatch.call(contactItems, "find", filter);

if (contact != null)
{
    Dispatch.call(contactItem, "Delete");
}