Working with Contacts in PST File

Reading Multiple Contacts in VCard Format

The code sample below demonstrates how to read a VCF file, check if it contains multiple contacts, and if so, load the contacts from the file into a list of VCardContact objects. The code uses the following methods:

try (InputStream stream = new FileInputStream("test.vcf")) {
    if (VCardContact.isMultiContacts(stream)) {
        List<VCardContact> contacts = VCardContact.loadAsMultiple(stream, Charset.forName("utf-8"));
    }
}

Adding Contact to PST

Create New PST, Add Sub-folders and Messages showed how to create a PST file and add a subfolder to it. With Aspose.Email you can add a MapiContact to the Contacts subfolder of a PST file that you have created or loaded. Below are the steps to add MapiContact to a PST:

  1. Create a MapiContact object.
  2. Set the MapiContact properties using different constructors and methods.
  3. Create a PST using the PersonalStorage.create() method.
  4. Create a pre-defined folder (Contacts) at the root of the PST file by accessing the root folder and then calling the addMapiMessageItem() method.

The code snippet below shows how to create a MapiContact and then add it to the Contacts folder of a newly created PST file.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Contact #1
MapiContact contact1 = new MapiContact("Sebastian Wright", "SebastianWright@dayrep.com");
// Contact #2
MapiContact contact2 = new MapiContact("Wichert Kroos", "WichertKroos@teleworm.us", "Grade A Investment");
// Contact #3
MapiContact contact3 = new MapiContact("Christoffer van de Meeberg", "ChristoffervandeMeeberg@teleworm.us", "Krauses Sofa Factory", "046-630-4614");
// Contact #4
MapiContact contact4 = new MapiContact();
contact4.setNameInfo(new MapiContactNamePropertySet("Margaret", "J.", "Tolle"));
contact4.getPersonalInfo().setGender(MapiContactGender.Female);
contact4.setProfessionalInfo(new MapiContactProfessionalPropertySet("Adaptaz", "Recording engineer"));
contact4.getPhysicalAddresses().getWorkAddress().setAddress("4 Darwinia Loop EIGHTY MILE BEACH WA 6725");
contact4.getElectronicAddresses().setEmail1(new MapiContactElectronicAddress("Hisen1988", "SMTP", "MargaretJTolle@dayrep.com"));
contact4.getTelephones().setBusinessTelephoneNumber("(08)9080-1183");
contact4.getTelephones().setMobileTelephoneNumber("(925)599-3355");
// Contact #5
MapiContact contact5 = new MapiContact();
contact5.setNameInfo(new MapiContactNamePropertySet("Matthew", "R.", "Wilcox"));
contact5.getPersonalInfo().setGender(MapiContactGender.Male);
contact5.setProfessionalInfo(new MapiContactProfessionalPropertySet("Briazz", "Psychiatric aide"));
contact5.getPhysicalAddresses().getWorkAddress().setAddress("Horner Strasse 12 4421 SAASS");
contact5.getTelephones().setBusinessTelephoneNumber("0650 675 73 30");
contact5.getTelephones().setHomeTelephoneNumber("(661)387-5382");
// Contact #6
MapiContact contact6 = new MapiContact();
contact6.setNameInfo(new MapiContactNamePropertySet("Bertha", "A.", "Buell"));
contact6.setProfessionalInfo(new MapiContactProfessionalPropertySet("Awthentikz", "Social work assistant"));
contact6.getPersonalInfo().setPersonalHomePage("B2BTies.com");
contact6.getPhysicalAddresses().getWorkAddress().setAddress("Im Astenfeld 59 8580 EDELSCHROTT");
contact6.getElectronicAddresses().setEmail1(new MapiContactElectronicAddress("Experwas", "SMTP", "BerthaABuell@armyspy.com"));
contact6.setTelephones(new MapiContactTelephonePropertySet("06605045265"));
PersonalStorage pst = PersonalStorage.create(dataDir + "MapiContactToPST_out.pst", FileFormatVersion.Unicode);
FolderInfo contactFolder = pst.createPredefinedFolder("Contacts", StandardIpmFolder.Contacts);
contactFolder.addMapiMessageItem(contact1);
contactFolder.addMapiMessageItem(contact2);
contactFolder.addMapiMessageItem(contact3);
contactFolder.addMapiMessageItem(contact4);
contactFolder.addMapiMessageItem(contact5);
contactFolder.addMapiMessageItem(contact6);

Save contacts information from PST file in MSG Format

This article shows how to access contact information from a Microsoft Outlook PST file and save contacts to disk in MSG format. To do so, use the PersonalStorage and MapiContact classes to get and display the contact information.

To get a contact’s information:

  1. Load the PST file in the PersonalStorage class.
  2. Browse the Contacts folder.
  3. Get the contents of the Contacts folder to get the message collection.
  4. Loop through the message collection.
  5. Call PersonalStorage.extractMessage() and then toMapiMessageItem() method to get the contact information in the MapiContact class.
  6. Use MapiContact properties to access the contact information.
  7. Call the PersonalStorage.extractMessage() method to get the contact information in the MapiMessage class.
  8. Call the MapiMessage.save() method to save the contact to disk in MSG format.

Below is a sample code that retrieves all the contacts information from the PST file and saves it to disk in MSG format.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = Utils.getSharedDataDir(AccessContactInformationFromPSTFile.class) + "outlook/";
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "SampleContacts.pst");
// Get the Contacts folder
FolderInfo folderInfo = pst.getRootFolder().getSubFolder("Contacts");
// Loop through all the contacts in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (int i = 0; i < messageInfoCollection.size(); i++) {
MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get_Item(i);
// Get the contact information
MapiContact contact = (MapiContact) pst.extractMessage(messageInfo).toMapiMessageItem();
// Display some contents on screen
System.out.println("Name: " + contact.getNameInfo().getDisplayName() + "\n");
// Save to disk in MSG format
if (contact.getNameInfo().getDisplayName() != null) {
MapiMessage message = pst.extractMessage(messageInfo); // Get rid of illegal characters that cannot be used as a file name
String messageName = message.getSubject().replace(":", " ").replace("\\", " ").replace("?", " ").replace("/", " ");
message.save(dataDir + messageName + ".msg");
}
}

Save Contacts Information from Outlook PST to Disk in vCard format

This article shows how to access contact information from a Microsoft Outlook PST file and save the contact to disk in vCard (VCF) format. It uses the PersonalStorage and MapiContact classes to get the contact information.

Below are the steps to get the contacts information:

  1. Load the PST file in PersonalStorage class.
  2. Browse the Contacts folder.
  3. Get the contents of the Contacts folder to get the message collection.
  4. Loop through the message collection.
  5. Call the PersonalStorage.extractMessage() method to get the contact information in the MapiContact class.
  6. Use the properties of the MapiContact class to access the contact information.

The program below loads a PST file from disk and saves all the contacts in vCard (VCF) format. The VCF files can then be used in any other program that can load the standard vCard contact file. If you open any VCF file in Microsoft Outlook, it will look like the one in the below screenshot.

todo:image_alt_text
Figure: A vCard saved with Aspose.Email
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "Outlook.pst");
// Get the Contacts folder
FolderInfo folderInfo = pst.getRootFolder().getSubFolder("Contacts");
// Loop through all the contacts in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (int i = 0; i < messageInfoCollection.size(); i++) {
MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get_Item(i);
// Get the contact information
MapiContact contact = (MapiContact) pst.extractMessage(messageInfo).toMapiMessageItem();
// Display some contents on screen
System.out.println("Name: " + contact.getNameInfo().getDisplayName() + " - " + messageInfo.getEntryIdString());
// Save to disk in vCard VCF format
contact.save(dataDir + "Contacts" + contact.getNameInfo().getDisplayName() + ".vcf", ContactSaveFormat.VCard);
}