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.
- MapiContactElectronicAddressPropertySet contains a set of MapiContactElectronicAddress.
- MapiContactEventPropertySet
- MapiContactNamePropertySet
- MapiContactPersonalInfoPropertySet
- MapiContactPhysicalAddressPropertySet contains a set of MapiContactPhysicalAddress.
- MapiContactProfessionalPropertySet
- MapiContactTelephonePropertySet
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.
- Contact (MapiContact)
- Electronic Addresses (MapiContactElectronicAddressPropertySet)
- Email1 (MapiContactElectronicAddress)
- Address Type
- Display Name
- Email Address
- Fax Number
- Email2
- Email3
- Home Fax
- Primary Fax
- Business Fax
- Email1 (MapiContactElectronicAddress)
- Events (MapiContactEventPropertySet)
See below for an example of how to set events.
- Birthday
- Wedding Anniversary
- Name Info (MapiContactNamePropertySet)
- Display Name
- Display Name Prefix
- File Under
- File Under ID
- Generation
- Given Name
- Initials
- Middle Name
- Nick Name
- Surname
- Personal Info (MapiContactPersonalInfoPropertySet)
- Account
- Business Home Page
- Computer Network Name
- Customer ID
- Free Business Location
- FTP Site
- Gender
- Government ID Number
- Hobbies
- HTML
- Instant Messaging Address
- Language
- Location
- Notes
- Organizational ID Number
- Personal Home Page
- Referred by Name
- Spouse Name
- Physical Address (MapiContactPhysicalAddressPropertySet)
- Home Address (MapiContactPhysicalAddress)
- Address
- City
- Country
- Country Code
- Postal Code
- Post Office Box
- State Or Province
- Other Address
- Work Address
- Home Address (MapiContactPhysicalAddress)
- Professional Info
- Assistant
- Company Name
- Depart Name
- Manager Name
- Office Location
- Profession
- Title
- Telephones (MapiContactTelephonePropertySet)
- Assistant Telephone Number
- Business2 Telephone Number
- Business Telephone Number
- Callback Telephone Number
- Car Telephone Number
- Company Main Telephone Number
- Home2 Telephone Number
- Home Telephone Number
- ISDN Number
- Mobile Telephone Number
- Other Telephone Number
- Pager Telephone Number
- Primary Telephone Number
- Radio Telephone Number
- Telex Number
- TTY/TDD Phone Number
- Electronic Addresses (MapiContactElectronicAddressPropertySet)
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.
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Figure: A Microsoft Outlook contact coded in with Aspose.Email | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
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 | |
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); | |
calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); | |
Date birthday = calendar.getTime(); | |
calendar.set(2012, 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:
- Instantiate a new object of the MapiContact class.
- Enter information related to various properties of the contact.
- Add photo data to the contact, if any.
- 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 | |
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 Photo Data | |
File fi = new File(dataDir + "Desert.jpg"); | |
byte[] fileContent = Files.readAllBytes(fi.toPath()); | |
MapiContactPhoto photo = new MapiContactPhoto(fileContent, MapiContactPhotoImageFormat.Jpeg); | |
contact.setPhoto(photo); | |
//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 = Utils.getSharedDataDir(DistributionList.class) + "outlook/"; | |
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 | |
MapiMessage msg = MapiMessage.fromFile(dataDir + "messageMapi.msg"); | |
MapiContact mapiContact = (MapiContact) msg.toMapiMessageItem(); |
Load a contact from VCard
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
MapiContact mapiContact = MapiContact.fromVCard(dataDir + "microsoft.vcf"); |
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 | |
MapiContact contactReadFromFile = MapiContact.fromVCard( "microsoft.vcf", StandardCharsets.UTF_8); |
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 = Utils.getSharedDataDir(RenderingContactInformationToMhtml.class) + "outlook/"; | |
//Load VCF Contact and convert to MailMessage for rendering to MHTML | |
MapiContact contact = MapiContact.fromVCard(dataDir + "ContactsSaqib Razzaq.vcf"); | |
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
contact.save(os, ContactSaveFormat.Msg); | |
MapiMessage msg = MapiMessage.fromStream(new ByteArrayInputStream(os.toByteArray())); | |
MailConversionOptions op = new MailConversionOptions(); | |
MailMessage eml = msg.toMailMessage(op); | |
//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 + "ContactsSaqib Razzaq_out.mhtml", mhtSaveOptions); | |
System.out.println("Execution Completed."); |