Working with Outlook Contacts using C++ Email Parser Library
Creating, Saving and Reading Contacts
Like MapiMessage, Aspose.Email allows you to create Outlook contacts. The MapiContact class provides all the contact related 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 Outlook Contact
To create a contact and save it to disc:
- Instantiate a new object of the MapiContact class.
- Enter contact property information.
- Add photo data (if any).
- Save the contact as MSG or VCard format.
The following code snippet shows you how to create and save outlook contact with C++ Email Parser Library or API.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// The path to the File directory. | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
System::SharedPtr<MapiContact> contact = System::MakeObject<MapiContact>(); | |
contact->set_NameInfo(System::MakeObject<MapiContactNamePropertySet>(L"Bertha", L"A.", L"Buell")); | |
contact->set_ProfessionalInfo(System::MakeObject<MapiContactProfessionalPropertySet>(L"Awthentikz", L"Social work assistant")); | |
contact->get_PersonalInfo()->set_PersonalHomePage(L"B2BTies.com"); | |
contact->get_PhysicalAddresses()->get_WorkAddress()->set_Address(L"Im Astenfeld 59 8580 EDELSCHROTT"); | |
contact->get_ElectronicAddresses()->set_Email1(System::MakeObject<MapiContactElectronicAddress>(L"Experwas", L"SMTP", L"BerthaABuell@armyspy.com")); | |
contact->set_Telephones(System::MakeObject<MapiContactTelephonePropertySet>(L"06605045265")); | |
contact->get_PersonalInfo()->set_Children(System::MakeArray<System::String>({L"child1", L"child2", L"child3"})); | |
contact->set_Categories(System::MakeArray<System::String>({L"category1", L"category2", L"category3"})); | |
contact->set_Mileage(L"Some test mileage"); | |
contact->set_Billing(L"Test billing information"); | |
contact->get_OtherFields()->set_Journal(true); | |
contact->get_OtherFields()->set_Private(true); | |
contact->get_OtherFields()->set_ReminderTime(System::DateTime(2014, 1, 1, 0, 0, 55)); | |
contact->get_OtherFields()->set_ReminderTopic(L"Test topic"); | |
contact->get_OtherFields()->set_UserField1(L"ContactUserField1"); | |
contact->get_OtherFields()->set_UserField2(L"ContactUserField2"); | |
contact->get_OtherFields()->set_UserField3(L"ContactUserField3"); | |
contact->get_OtherFields()->set_UserField4(L"ContactUserField4"); | |
// Add a photo | |
{ | |
System::SharedPtr<System::IO::FileStream> fs = System::IO::File::OpenRead(dataDir + L"Desert.jpg"); | |
System::ArrayPtr<uint8_t> buffer = System::MakeArray<uint8_t>(fs->get_Length(), 0); | |
fs->Read(buffer, 0, buffer->get_Length()); | |
contact->set_Photo(System::MakeObject<MapiContactPhoto>(buffer, Aspose::Email::Outlook::MapiContactPhotoImageFormat::Jpeg)); | |
} | |
// Save the Contact in MSG format | |
contact->Save(dataDir + L"MapiContact_out.msg", Aspose::Email::Outlook::ContactSaveFormat::Msg); | |
// Save the Contact in VCF format | |
contact->Save(dataDir + L"MapiContact_out.vcf", Aspose::Email::Outlook::ContactSaveFormat::VCard); |
Reading a MapiContact
The MapiContact class can be used to load both Outlook MSG and VCard format contacts. The following code snippet shows you how to load Outlook contacts saved as MSG and VCF into a MapiContact.
Loading a Contact from MSG
The following code snippet shows you how to loading a contact from MSG.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
System::SharedPtr<MapiMessage> msg = MapiMessage::FromFile(dataDir + L"Contact.msg"); | |
System::SharedPtr<MapiContact> mapiContact = System::DynamicCast<Aspose::Email::Outlook::MapiContact>(msg->ToMapiMessageItem()); |
Loading a Contact from VCard
The following code snippet shows you how to loading a contact from vCard.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
auto vcfTest = VCardContact::Load(dataDir + L"Contact.vcf"); | |
System::SharedPtr<MapiContact> contact = MapiContact::FromVCard(dataDir + L"Contact.vcf"); |