使用 Outlook 联系人

创建 Outlook 联系人

Aspose.Email for Java 支持使用以下方式创建 Outlook 联系人(VCards) MapiContact 类。 MapiContact 包含许多方法,下面列出了一些。

Aspose.Email for Java 中的联系人结构

以下是 Aspose.Email for Java 中实现的联系人层次结构。每个属性后面给出相应的类名,并提供了指向在线文档的超链接以供进一步参考。

  1. Contact (MapiContact)
    1. 电子地址 (MapiContactElectronicAddressPropertySet)
      1. 电子邮件1 (MapiContactElectronicAddress)
        1. 地址类型
        2. 显示名称
        3. 电子邮件地址
        4. 传真号码
      2. 电子邮件2
      3. 电子邮件3
      4. 住宅传真
      5. 主要传真
      6. 商务传真
    2. 事件 (MapiContactEventPropertySet) 请参阅下面的示例了解如何设置事件。
      1. 生日
      2. 结婚纪念日
    3. 姓名信息 (MapiContactNamePropertySet)
      1. 显示名称
      2. 显示名称前缀
      3. 归档至
      4. 归档ID
      5. 世代
      6. 名字
      7. 缩写
      8. 中间名
      9. 昵称
      10. 姓氏
    4. 个人信息 (MapiContactPersonalInfoPropertySet)
      1. 账户
      2. 商务主页
      3. 计算机网络名称
      4. 客户编号
      5. 免费商务地点
      6. FTP 站点
      7. 性别
      8. 政府身份证号
      9. 爱好
      10. HTML
      11. 即时通讯地址
      12. 语言
      13. 地点
      14. 备注
      15. 组织机构编号
      16. 个人主页
      17. 推荐人姓名
      18. 配偶姓名
    5. 实际地址 (MapiContactPhysicalAddressPropertySet)
      1. 住宅地址 (MapiContactPhysicalAddress)
        1. 地址
        2. 城市
        3. 国家
        4. 国家代码
        5. 邮政编码
        6. 邮政信箱
        7. 州或省
      2. 其他地址
      3. 工作地址
    6. 专业信息
      1. 助理
      2. 公司名称
      3. 部门名称
      4. 经理姓名
      5. 办公室位置
      6. 职业
      7. 职称
    7. 电话号码 (MapiContactTelephonePropertySet)
      1. 助理电话号码
      2. 商务2电话号码
      3. 商务电话号码
      4. 回拨电话号码
      5. 车载电话号码
      6. 公司主要电话号码
      7. 住宅2电话号码
      8. 住宅电话号码
      9. ISDN 号码
      10. 移动电话号码
      11. 其他电话号码
      12. 传呼机电话号码
      13. 主要电话号码
      14. 无线电话号码
      15. 电传号码
      16. TTY/TDD 电话号码

以下代码使用 Aspose.Email 创建 Outlook 联系人,并填充姓名、职业属性、实际地址和电子邮件。它还展示了添加 MapiContactEventPropertySet 到联系人。

|todo:image_alt_text| | :- | |图示:使用 Aspose.Email 编码的 Microsoft Outlook 联系人|

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiContact contact = new MapiContact();

// Set the name properties using MapiContactNamePropertySet
MapiContactNamePropertySet namePropSet = new MapiContactNamePropertySet();
namePropSet.setGivenName("Melissa");
namePropSet.setSurname("MacBeth");
contact.setNameInfo(namePropSet);

// Set the professional properties using MapiContactProfessionalPropertySet
MapiContactProfessionalPropertySet profPropSet = new MapiContactProfessionalPropertySet();
profPropSet.setTitle("Account Representative");
profPropSet.setCompanyName("Contoso Ltd.");
profPropSet.setOfficeLocation("36/2529");
contact.setProfessionalInfo(profPropSet);

// Telephones
MapiContactTelephonePropertySet telephone = new MapiContactTelephonePropertySet();
telephone.setAssistantTelephoneNumber("(831) 758-7214");
telephone.setBusiness2TelephoneNumber("(831) 759-2518");
telephone.setBusinessTelephoneNumber("(831) 758-7285");
telephone.setCallbackTelephoneNumber("(831) 758-7321 (After hours)");
telephone.setCarTelephoneNumber("(831) 758-7201");
telephone.setCompanyMainTelephoneNumber("(831) 758-7368");
telephone.setHome2TelephoneNumber("(831) 758-7256");
telephone.setHomeTelephoneNumber("(831) 758-7257");
telephone.setIsdnNumber("(831) 758-7381");
telephone.setMobileTelephoneNumber("(831) 758-7368");
telephone.setOtherTelephoneNumber("(831) 758-7201");
telephone.setPagerTelephoneNumber("(831) 758-7368");
telephone.setPrimaryTelephoneNumber("(831) 758-7334");
telephone.setRadioTelephoneNumber("(831) 758-7234");
telephone.setTelexNumber("(831) 758-7408");
telephone.setTtyTddPhoneNumber("(800) 806-4474");
contact.setTelephones(telephone);

// Set the physical address using MapiContactPhysicalAddress and MapiContactPhysicalAddressPropertySet
MapiContactPhysicalAddress physAddress = new MapiContactPhysicalAddress();
physAddress.setAddress("144 Hitchcock Rd");
physAddress.setCity("Salinas");
physAddress.setStateOrProvince("CA");
physAddress.setPostalCode("93908");

MapiContactPhysicalAddressPropertySet physAddrPropSet = new MapiContactPhysicalAddressPropertySet();
physAddrPropSet.setWorkAddress(physAddress);
contact.setPhysicalAddresses(physAddrPropSet);

// Set the email information using MapiContactElectronicAddress and its property set
MapiContactElectronicAddress email = new MapiContactElectronicAddress();
email.setAddressType("SMTP");
email.setDisplayName("Melissa MacBeth (melissa@contoso.com)");
email.setEmailAddress("melissa@contoso.com");

MapiContactElectronicAddressPropertySet elecAddrPropSet = new MapiContactElectronicAddressPropertySet();
elecAddrPropSet.setEmail1(email);
contact.setElectronicAddresses(elecAddrPropSet);

contact.save(dataDir + "OutlookContact_out.vcf", ContactSaveFormat.VCard);

向 MapiContact 添加联系人事件信息

Microsoft Outlook 允许用户向联系人添加事件信息。该事件包括生日和结婚纪念日。Aspose.Email 提供了 MapiContactEventPropertySet 用于向联系人添加此信息的类。下面的示例对此进行了详细说明。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(1990, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date birthday = calendar.getTime();

calendar.set(2012, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date weddingDay = calendar.getTime();

MapiContactEventPropertySet contactEventSet = new MapiContactEventPropertySet();
contactEventSet.setBirthday(birthday);
contactEventSet.setWeddingAnniversary(weddingDay);

MapiContact contact = new MapiContact();
contact.setEvents(contactEventSet);

contact.save(dataDir + "Contact_out.msg", ContactSaveFormat.Msg);

创建、保存和读取 Outlook 联系人

Aspose.Email 允许开发者创建 Microsoft Outlook 联系人以及电子邮件。 MapiContact 类提供创建 Outlook 联系人所需的所有联系人属性。本文展示如何使用该类创建、保存和读取 Outlook 联系人。 MapiContact 类。

创建并保存 MapiContact

以下步骤可用于创建并将联系人保存到磁盘:

  1. 实例化一个新的对象 MapiContact 类。
  2. 输入与联系人各属性相关的信息。
  3. 如果有,请将照片数据添加到联系人。
  4. 将联系人保存为 MSG 或 VCard 格式。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiContact contact = new MapiContact("Sebastian Wright", "SebastianWright@dayrep.com");
contact.setNameInfo(new MapiContactNamePropertySet("Bertha", "A.", "Buell"));
contact.setProfessionalInfo(new MapiContactProfessionalPropertySet("Awthentikz", "Social work assistant"));
contact.getPersonalInfo().setPersonalHomePage("B2BTies.com");
contact.getPhysicalAddresses().getWorkAddress().setAddress("Im Astenfeld 59 8580 EDELSCHROTT");
contact.getElectronicAddresses().setEmail1(
        new MapiContactElectronicAddress("Experwas", "SMTP", "BerthaABuell@armyspy.com"));
contact.setTelephones(new MapiContactTelephonePropertySet("06605045265"));

// Set the photo data
byte[] fileContent = Files.readAllBytes(Paths.get(dataDir + "Desert.jpg"));
contact.setPhoto(new MapiContactPhoto(fileContent, MapiContactPhotoImageFormat.Jpeg));

// Save as MSG
contact.save(dataDir + "Contact_out.msg", ContactSaveFormat.Msg);

以版本 3 VCF 格式保存联系人

要将联系人保存为版本 3 的 VCF 格式,请使用 VCardVersion 可枚举项,用于设置 VCardSaveOptions.Version 属性。以下示例代码演示了如何使用 VCardVersion 用于将联系人保存为 VCF 版本 3 格式的可枚举。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the resource directory.
String dataDir = "data/";

MapiContact contact = new MapiContact();
contact.setNameInfo(new MapiContactNamePropertySet("Jane", "A.", "Buell"));
contact.setProfessionalInfo(new MapiContactProfessionalPropertySet("Aspose Pty Ltd", "Social work assistant"));
contact.getPersonalInfo().setPersonalHomePage("Aspose.com");
contact.getElectronicAddresses().setEmail1(new MapiContactElectronicAddress("test@test.com"));
contact.getTelephones().setHomeTelephoneNumber("06605040000");

VCardSaveOptions opt = new VCardSaveOptions();
opt.setVersion(VCardVersion.V30);

contact.save(dataDir + "V30.vcf", opt);

读取 MapiContact

该 MapiContact 该类可用于加载 Microsoft Outlook MSG 文件以及 VCard 格式的联系人。以下代码示例展示如何将保存为 MSG 和 VCF 的 Outlook 联系人加载到 MapiContact.

从 MSG 加载联系人

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiMessage msg = MapiMessage.load(dataDir + "messageMapi.msg");
MapiContact mapiContact = (MapiContact) msg.toMapiMessageItem();

System.out.println(mapiContact.getNameInfo().getDisplayName());

从 VCard 加载联系人

以下代码展示如何创建一个 MapiContact 通过使用以下方式从 VCard 文件加载数据 VCardLoadOptions:

MapiContact mapiContact = MapiContact.fromVCard("contact.vcf", new VCardLoadOptions());
System.out.println(mapiContact.getNameInfo().getDisplayName());

使用指定编码加载 VCard 联系人

支持的方法: MapiContact.fromVCard(String, Encoding)

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiContact contactReadFromFile = MapiContact.fromVCard(dataDir + "microsoft.vcf", StandardCharsets.UTF_8);

使用自定义选项从 vCard 加载 MAPI 联系人

为了在将 vCard(.vcf)文件转换为 MAPI 联系人时提供更大的灵活性,Aspose.Email for Java 提供了 MapiContact.fromVCard(String filePath, VCardLoadOptions options) 重载。它提供了对 vCard 文件解释方式的改进控制——尤其是在处理不同的 vCard 格式、编码或高级解析场景时。

加载的联系人随后可用于 PST 文件、MSG 导出或其他 Outlook 兼容格式。

MapiContact mapiContact = MapiContact.fromVCard("contact.vcf", new VCardLoadOptions());
System.out.println(mapiContact.getNameInfo().getDisplayName());

保存具有扩展字段的 VCard 文件

该 UseExtensions 属性属于 VCardSaveOptions 允许您控制是否使用扩展(X-)在保存 vCard 文件时可使用的字段。当设置为 true (默认),允许扩展,以兼容自定义字段和附加联系信息。

以下代码示例展示了在保存联系人时如何禁用扩展字段:

MapiContact contact = MapiContact.fromVCard("contact.vcf");

VCardSaveOptions options = new VCardSaveOptions();
options.setUseExtensions(false);

contact.save("contact_out.vcf", options);

读取并设置联系人的产品标识符

vCard 文件在 PRODID 字段。Aspose.Email 在联系人在 vCard 与 MSG 之间转换时保留此标识符,因此您始终可以追溯联系人的来源。该标识符可通过以下成员获取:

以下代码片段读取 vCard 文件中的产品标识符,将联系人转换为 MSG,并使用新标识符保存:

// Read the product identifier from a vCard file
VCardContact vcard = VCardContact.load("contact.vcf");
System.out.println("vCard product ID: " + vcard.getExplanatoryInfo().getProdId());

// The identifier is kept when the contact is converted to MSG
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
vcard.save(msgStream, ContactSaveFormat.Msg);
MapiMessage msg = MapiMessage.load(new ByteArrayInputStream(msgStream.toByteArray()));
MapiContact contact = (MapiContact) msg.toMapiMessageItem();
System.out.println("MapiContact product ID: " + contact.getProductId());

// Save the contact to MSG with a different product identifier
MapiContactSaveOptions options = new MapiContactSaveOptions();
options.setProductId("-//Contoso//Address Book 2.0//EN");
contact.save("contact_out.msg", options);

要为新联系人设置标识符,请调用 setProductId 或通过 VCardSaveOptions 在将联系人保存为 vCard 时:

MapiContact contact = new MapiContact("John Smith", "john.smith@example.com");
contact.setProductId("-//Contoso//Address Book 2.0//EN");
contact.save("contact_out.vcf", ContactSaveFormat.VCard);

// Override the identifier for a single save operation
VCardSaveOptions options = new VCardSaveOptions();
options.setProductId("-//Contoso//Export Tool 1.0//EN");
contact.save("contact_export.vcf", options);

将多联系人 VCF 文件转换为 MapiDistributionList

Aspose.Email 支持将多联系人 VCF 文件转换为 MapiDistributionList 对象,使您能够轻松管理并将多个联系人直接导入您的应用程序。此功能可通过以下静态方法访问 MapiDistributionList 类:

  • static MapiDistributionList fromVCF(String filePath)
  • static MapiDistributionList fromVCF(InputStream stream)

下面的代码示例演示了如何使用此功能:

// Convert a multi-contact VCF file into a MapiDistributionList
MapiDistributionList dlist = MapiDistributionList.fromVCF("contacts.vcf");

System.out.println(dlist.getDisplayName() + ": " + dlist.getMembers().size() + " member(s)");

生成的分发列表可以写回 VCF 文件,采用所需的 vCard 版本:

MapiDistributionList dlist = MapiDistributionList.fromVCF("contacts.vcf");

MapiDistributionListSaveOptions options = new MapiDistributionListSaveOptions(ContactSaveFormat.VCard);
options.setVersion(VCardVersion.V30);

dlist.save("distribution_list.vcf", options);

直接处理 vCard 文件

当您的源和目标都是 vCard 文件且不需要 Outlook/MAPI 功能时,请使用 VCardContact 类而不是 MapiContact。它直接读取和写入 VCF,支持 vCard 2.1/3.0/4.0 和每个文件多个联系人。参见 使用 vCard(VCF)文件 有关 vCard 对象模型的详细说明。

将联系人信息呈现为 MHTML

可以使用 Aspose.Email API 将 Outlook 联系人转换为 MHTML。此示例展示如何将 VCard 加载到 MapiContact 然后在以下帮助下转换为 MHTML MailMessage API。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

// Load the VCF contact and convert it to a MailMessage for rendering to MHTML
MapiContact contact = MapiContact.fromVCard(dataDir + "Contact.vcf");

ByteArrayOutputStream os = new ByteArrayOutputStream();
contact.save(os, ContactSaveFormat.Msg);

MapiMessage msg = MapiMessage.load(new ByteArrayInputStream(os.toByteArray()));
MailMessage eml = msg.toMailMessage(new MailConversionOptions());

// Prepare the MHT format options
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setCheckBodyContentEncoding(true);
mhtSaveOptions.setPreserveOriginalBoundaries(true);
mhtSaveOptions.setMhtFormatOptions(MhtFormatOptions.RenderVCardInfo | MhtFormatOptions.WriteHeader);
mhtSaveOptions.setRenderedContactFields(ContactFieldsSet.NameInfo | ContactFieldsSet.PersonalInfo
        | ContactFieldsSet.Telephones | ContactFieldsSet.Events);

eml.save(dataDir + "Contact_out.mhtml", mhtSaveOptions);