Working with Outlook Contacts

Create Outlook Contact

Aspose.Email for Java supports creating Outlook contacts (VCards) using the MapiContact class. MapiContact contains many methods some of which are given below.

Contact Structure in Aspose.Email for Java

Below is the hierarchy implemented for contacts in Aspose.Email for Java. The relevant class name is given against each property. Hyperlinks are provided to the online documentation for further reference.

  1. Contact (MapiContact)
    1. Electronic Addresses (MapiContactElectronicAddressPropertySet)
      1. Email1 (MapiContactElectronicAddress)
        1. Address Type
        2. Display Name
        3. Email Address
        4. Fax Number
      2. Email2
      3. Email3
      4. Home Fax
      5. Primary Fax
      6. Business Fax
    2. Events (MapiContactEventPropertySet) See below for an example of how to set events.
      1. Birthday
      2. Wedding Anniversary
    3. Name Info (MapiContactNamePropertySet)
      1. Display Name
      2. Display Name Prefix
      3. File Under
      4. File Under ID
      5. Generation
      6. Given Name
      7. Initials
      8. Middle Name
      9. Nick Name
      10. Surname
    4. Personal Info (MapiContactPersonalInfoPropertySet)
      1. Account
      2. Business Home Page
      3. Computer Network Name
      4. Customer ID
      5. Free Business Location
      6. FTP Site
      7. Gender
      8. Government ID Number
      9. Hobbies
      10. HTML
      11. Instant Messaging Address
      12. Language
      13. Location
      14. Notes
      15. Organizational ID Number
      16. Personal Home Page
      17. Referred by Name
      18. Spouse Name
    5. Physical Address (MapiContactPhysicalAddressPropertySet)
      1. Home Address (MapiContactPhysicalAddress)
        1. Address
        2. City
        3. Country
        4. Country Code
        5. Postal Code
        6. Post Office Box
        7. State Or Province
      2. Other Address
      3. Work Address
    6. Professional Info
      1. Assistant
      2. Company Name
      3. Depart Name
      4. Manager Name
      5. Office Location
      6. Profession
      7. Title
    7. Telephones (MapiContactTelephonePropertySet)
      1. Assistant Telephone Number
      2. Business2 Telephone Number
      3. Business Telephone Number
      4. Callback Telephone Number
      5. Car Telephone Number
      6. Company Main Telephone Number
      7. Home2 Telephone Number
      8. Home Telephone Number
      9. ISDN Number
      10. Mobile Telephone Number
      11. Other Telephone Number
      12. Pager Telephone Number
      13. Primary Telephone Number
      14. Radio Telephone Number
      15. Telex Number
      16. TTY/TDD Phone Number

The following code uses Aspose.Email to create an Outlook contact and fills it with name, professional properties, physical address, and email. It also shows adding MapiContactEventPropertySet to the contact.

todo:image_alt_text
Figure: A Microsoft Outlook contact coded in with Aspose.Email
// 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);

Adding Contact Event Information to a MapiContact

Microsoft Outlook lets users add event information to a contact. The event holds the birthday and wedding anniversary. Aspose.Email provides the MapiContactEventPropertySet class for adding this information to a contact. This is elaborated in the following example.

// 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);

Creating, Saving and Reading Outlook Contacts

Aspose.Email allows developers to create Microsoft Outlook contacts as well as email messages. The MapiContact class provides all contact properties required to create an Outlook contact. This article shows how to create, save and read an Outlook contact using the MapiContact class.

Create and Save a MapiContact

The following steps can be used to create and save a contact to disc:

  1. Instantiate a new object of the MapiContact class.
  2. Enter information related to various properties of the contact.
  3. Add photo data to the contact, if any.
  4. Save the contact as MSG or VCard format.
// 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);

Save Contact in Version 3 VCF Format

To save the contact in version 3 VCF format, use the VCardVersion enumerable to set the VCardSaveOptions.Version property. The following sample code demonstrates the use of VCardVersion enumerable to save the contact VCF version 3 format.

// 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);

Read a MapiContact

The MapiContact class can be used to load both Microsoft Outlook MSG files as well as VCard format contacts. The following code samples show how to load Outlook contacts saved as MSG and VCF into MapiContact.

Load a Contact from 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());

Load a contact from VCard

The following code shows how to create a MapiContact by loading data from a VCard file using VCardLoadOptions:

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

Load VCard Contact with specified Encoding

Supported Method: 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);

Load MAPI Contacts from vCard with Custom Options

To provide more flexibility when converting vCard (.vcf) files into MAPI contacts, Aspose.Email for Java provides the MapiContact.fromVCard(String filePath, VCardLoadOptions options) overload. It offers improved control over how vCard files are interpreted — especially when working with different vCard formats, encodings, or advanced parsing scenarios.

The loaded contact can then be used within PST files, MSG export, or other Outlook-compatible formats.

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

Save VCard Files with Extended Fields

The UseExtensions property of VCardSaveOptions allows you to control whether extended (X-) fields may be used when saving vCard files. When set to true (the default), extensions are permitted, providing compatibility with custom fields and additional contact information.

The following code sample shows how to disable the extended fields when saving a contact:

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

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

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

Read and Set the Product Identifier of a Contact

A vCard file records the product that created it in the PRODID field. Aspose.Email keeps this identifier when a contact is converted between vCard and MSG, so you can always trace the source of a contact. The identifier is available through the following members:

  • MapiContact.getProductId() and setProductId(String) - get or set the identifier of the product that created the contact.
  • ContactSaveOptions.setProductId(String) - sets the identifier written when the contact is saved. The method is available in all contact save options, including MapiContactSaveOptions and VCardSaveOptions.
  • VCardExplanatoryInfo.getProdId() - returns the identifier of a contact loaded with the VCardContact class.

The following code snippet reads the product identifier from a vCard file, converts the contact to MSG, and saves it with a new identifier:

// 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);

To set the identifier for a new contact, call setProductId or pass it through VCardSaveOptions when saving the contact as a 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);

Convert Multi-Contact VCF Files to MapiDistributionList

Aspose.Email supports the conversion of multi-contact VCF files into MapiDistributionList objects, making it easy to manage and import multiple contacts directly into your applications. This feature is accessible through the following static methods of the MapiDistributionList class:

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

The code sample below demonstrates how to use this feature:

// 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)");

The resulting distribution list can be written back to a VCF file in the required vCard version:

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

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

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

Working with vCard Files Directly

When your source and target are vCard files and you do not need Outlook/MAPI features, use the VCardContact class instead of MapiContact. It reads and writes VCF directly, supports vCard 2.1/3.0/4.0 and multiple contacts per file. See Working with vCard (VCF) Files for a detailed description of the vCard object model.

Rendering Contact Information to MHTML

Outlook Contact can be converted to MHTML using Aspose.Email API. This example shows how a VCard is loaded into MapiContact and then converted to MHTML with the help of 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);