Skapa, spara och läs Outlook‑kontakter i C++
Aspose.Email för C++ låter dig skapa och hantera Microsoft Outlook‑kontakter programmässigt. MapiContact klassen tillhandahåller alla kontaktrelaterade egenskaper inklusive namn, yrkesinformation, adresser, e‑postkonton, telefonnummer, kategorier, anpassade fält och foton.
Denna artikel visar hur du skapar, sparar och läser Outlook‑kontakter i MSG- och vCard‑format (VCF).
Exemplen i denna artikel använder dessa rubriker och namnrymder:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <MhtSaveOptions.h>
#include <MhtFormatOptions.h>
#include <MsgLoadOptions.h>
#include <ContactFieldsSet.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactNamePropertySet.h>
#include <Mapi/MapiContactProfessionalPropertySet.h>
#include <Mapi/MapiContactPersonalInfoPropertySet.h>
#include <Mapi/MapiContactPhysicalAddressPropertySet.h>
#include <Mapi/MapiContactPhysicalAddress.h>
#include <Mapi/MapiContactElectronicAddressPropertySet.h>
#include <Mapi/MapiContactElectronicAddress.h>
#include <Mapi/MapiContactTelephonePropertySet.h>
#include <Mapi/MapiContactOtherPropertySet.h>
#include <Mapi/MapiContactPhoto.h>
#include <Mapi/MapiContactPhotoImageFormat.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiDistributionList.h>
#include <Mapi/MapiDistributionListSaveOptions.h>
#include <Mapi/MapiDistributionListMemberCollection.h>
#include <Mapi/ContactSaveFormat.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MailConversionOptions.h>
#include <PersonalInfo/vCard/VCardContact.h>
#include <PersonalInfo/vCard/VCardIdentificationInfo.h>
#include <PersonalInfo/vCard/VCardLoadOptions.h>
#include <PersonalInfo/vCard/VCardSaveOptions.h>
#include <PersonalInfo/vCard/VCardVersion.h>
#include <system/io/memory_stream.h>
#include <system/text/encoding.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::PersonalInfo::VCard;
Skapa och spara en Outlook‑kontakt
För att skapa en Outlook‑kontakt och spara den på disk, följ stegen nedan:
- Instansiera MapiContact.
- Fyll i namn, yrkes-, personliga och adressuppgifter.
- Lägg till elektroniska adresser och telefonnummer.
- Tilldela kategorier, körsträcka, fakturering och anpassade fält.
- Eventuellt bädda in ett kontaktfoto.
- Spara kontakten i MSG- eller vCard‑format (VCF).
Detaljerna för en kontakt är grupperade i egendomssatser: MapiContactNamePropertySet för namnet, MapiContactProfessionalPropertySet för företaget och befattningen, MapiContactPhysicalAddressPropertySet för postadresserna, MapiContactElectronicAddressPropertySet för e‑post‑ och faxadresser, MapiContactTelephonePropertySet för telefonnumren och MapiContactOtherPropertySet för de återstående fälten såsom påminnelsen och användarfälten. MapiContact::Save skriver kontakten som MSG eller som vCard, beroende på ContactSaveFormat.
SharedPtr<MapiContact> contact = MakeObject<MapiContact>();
contact->set_NameInfo(MakeObject<MapiContactNamePropertySet>(u"Bertha", u"A.", u"Buell"));
contact->set_ProfessionalInfo(MakeObject<MapiContactProfessionalPropertySet>(u"Awthentikz", u"Social work assistant"));
contact->get_PersonalInfo()->set_PersonalHomePage(u"B2BTies.com");
contact->get_PhysicalAddresses()->get_WorkAddress()->set_Address(u"Im Astenfeld 59 8580 EDELSCHROTT");
contact->get_ElectronicAddresses()->set_Email1(MakeObject<MapiContactElectronicAddress>(u"Experwas", u"SMTP", u"BerthaABuell@armyspy.com"));
contact->set_Telephones(MakeObject<MapiContactTelephonePropertySet>(u"06605045265"));
contact->get_PersonalInfo()->set_Children(MakeArray<String>({u"child1", u"child2", u"child3"}));
contact->set_Categories(MakeArray<String>({u"category1", u"category2", u"category3"}));
contact->set_Mileage(u"Some test mileage");
contact->set_Billing(u"Test billing information");
contact->get_OtherFields()->set_Journal(true);
contact->get_OtherFields()->set_Private(true);
contact->get_OtherFields()->set_ReminderTime(DateTime(2014, 1, 1, 0, 0, 55));
contact->get_OtherFields()->set_ReminderTopic(u"Test topic");
contact->get_OtherFields()->set_UserField1(u"ContactUserField1");
contact->get_OtherFields()->set_UserField2(u"ContactUserField2");
// a photo is stored with the format of its data
contact->set_Photo(MakeObject<MapiContactPhoto>(IO::File::ReadAllBytes(u"portrait.jpg"), MapiContactPhotoImageFormat::Jpeg));
contact->Save(u"Contact_out.msg", ContactSaveFormat::Msg);
contact->Save(u"Contact_out.vcf", ContactSaveFormat::VCard);
Läs en kontakt med MapiContact
Den MapiContact klassen kan ladda Outlook‑kontakter sparade i både MSG‑ och VCF‑format.
Läs in en kontakt från MSG
En MSG‑fil som innehåller en kontakt laddas som vilket annat Outlook‑objekt som helst, med MapiMessage::Load. ToMapiMessageItem returnerar sedan objektet som en MapiContact. Kontrollerar get_SupportedType först försäkrar den att filen verkligen innehåller en kontakt – DynamicCast kastar ett fel när objektet är av en annan typ.
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"Contact_out.msg");
if (msg->get_SupportedType() == MapiItemType::Contact)
{
SharedPtr<MapiContact> mapiContact = DynamicCast<MapiContact>(msg->ToMapiMessageItem());
Console::WriteLine(u"Name: " + mapiContact->get_NameInfo()->get_DisplayName());
Console::WriteLine(u"Company: " + mapiContact->get_ProfessionalInfo()->get_CompanyName());
Console::WriteLine(u"E-mail: " + mapiContact->get_ElectronicAddresses()->get_Email1()->get_EmailAddress());
}
Ladda en kontakt från vCard (VCF)
En vCard‑fil kan läsas på två sätt. VCardContact::Load ger en VCardContact som speglar vCard‑strukturen – rätt val när data förblir i vCard‑format (se Arbeta med vCard‑filer). MapiContact::FromVCard konverterar vCardet direkt till en Outlook‑kontakt, klar att sparas som MSG eller läggas till i en PST‑fil.
// the vCard object model
SharedPtr<VCardContact> vcard = VCardContact::Load(u"Contact_out.vcf");
Console::WriteLine(u"vCard display name: " + vcard->get_IdentificationInfo()->get_DisplayName());
// the same file as an Outlook contact
SharedPtr<MapiContact> contact = MapiContact::FromVCard(u"Contact_out.vcf");
Console::WriteLine(u"MapiContact display name: " + contact->get_NameInfo()->get_DisplayName());
Styr hur ett vCard läses och skrivs
MapiContact::FromVCard har överlagringar som tar en VCardLoadOptions objekt eller en kodning, vilket är viktigt för filer skrivna i en äldre kodsida. På vägen ut, VCardSaveOptions väljer vCard‑version och avgör genom UseExtensions om den utökade X- egenskaper kan skrivas – stäng av det när målprogrammet endast förstår standardfälten.
// load a vCard with explicit options
SharedPtr<VCardLoadOptions> loadOptions = MakeObject<VCardLoadOptions>();
loadOptions->set_PreferredEncoding(System::Text::Encoding::get_UTF8());
SharedPtr<MapiContact> contact = MapiContact::FromVCard(u"Contact.vcf", loadOptions);
Console::WriteLine(contact->get_NameInfo()->get_DisplayName());
// write it back as vCard 3.0, without the extended X- properties
SharedPtr<VCardSaveOptions> saveOptions = MakeObject<VCardSaveOptions>();
saveOptions->set_Version(VCardVersion::V30);
saveOptions->set_UseExtensions(false);
contact->Save(u"ContactV3.vcf", saveOptions);
Distributionslistor och multi‑kontakt‑VCF‑filer
En VCF‑fil som innehåller flera kontakter motsvarar en MapiDistributionList: FromVCF läser in en sådan fil, och MapiDistributionListSaveOptions med ContactSaveFormat::VCard skriver en tillbaka.
// a multi-contact VCF file becomes a distribution list
SharedPtr<MapiDistributionList> dlist = MapiDistributionList::FromVCF(u"contacts.vcf");
Console::WriteLine(u"Members: " + Convert::ToString(dlist->get_Members()->get_Count()));
// and a distribution list can be written back as a multi-contact VCF file
SharedPtr<MapiDistributionListSaveOptions> options = MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::VCard);
options->set_Version(VCardVersion::V30);
dlist->Save(u"distribution_list.vcf", options);
Rendera en kontakt som MHTML
Ett kontaktkort kan publiceras som MHTML – en e‑postvänlig sida som visar fälten på samma sätt som Outlook. Kontakten sparas i en MSG‑ström, läses tillbaka som en MapiMessage, konverterad till en MailMessage och sparas med MhtFormatOptions::RenderVCardInfo. RenderedContactFields väljer de fältgrupper som visas, från ContactFieldsSet.
SharedPtr<MapiContact> contact = MapiContact::FromVCard(u"Contact.vcf");
// the contact travels through an MSG stream into a MailMessage
SharedPtr<IO::MemoryStream> stream = MakeObject<IO::MemoryStream>();
contact->Save(stream, ContactSaveFormat::Msg);
stream->set_Position(0);
SharedPtr<MapiMessage> msg = MapiMessage::Load(stream, MakeObject<MsgLoadOptions>());
SharedPtr<MailMessage> eml = msg->ToMailMessage(MakeObject<MailConversionOptions>());
// RenderVCardInfo writes the contact card into the body
SharedPtr<MhtSaveOptions> mhtSaveOptions = MakeObject<MhtSaveOptions>();
mhtSaveOptions->set_CheckBodyContentEncoding(true);
mhtSaveOptions->set_PreserveOriginalBoundaries(true);
mhtSaveOptions->set_MhtFormatOptions(MhtFormatOptions::WriteHeader | MhtFormatOptions::RenderVCardInfo);
// and these are the field groups it contains
mhtSaveOptions->set_RenderedContactFields(ContactFieldsSet::NameInfo | ContactFieldsSet::PersonalInfo |
ContactFieldsSet::Telephones | ContactFieldsSet::Events);
eml->Save(u"ContactCard.mhtml", mhtSaveOptions);
Läs och sätt produktidentifieraren för en kontakt
En vCard‑fil registrerar produkten som skapade den i PRODID fält. Aspose.Email behåller denna identifierare när en kontakt konverteras mellan vCard och MSG, så du kan alltid spåra kontaktens källa. Identifieraren är tillgänglig via följande medlemmar:
MapiContact::get_ProductId()ochMapiContact::set_ProductId()- hämta eller sätt identifieraren för produkten som skapade ett MapiContact.ContactSaveOptions::set_ProductId()- anger identifieraren som skrivs när kontakten sparas. Metoden finns i alla sparalternativ för kontakter, inklusive MapiContactSaveOptions och VCardSaveOptions.- VCardExplanatoryInfo::get_ProdId() - returnerar identifieraren för en kontakt som laddats med VCardContact klass.
Följande kodexempel läser produktidentifieraren från en vCard‑fil, konverterar kontakten till MSG och sparar den med en ny identifierare:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactSaveOptions.h>
#include <Mapi/ContactSaveFormat.h>
#include <Mapi/MapiItemType.h>
#include <PersonalInfo/vCard/VCardContact.h>
#include <PersonalInfo/vCard/VCardExplanatoryInfo.h>
#include <system/io/memory_stream.h>
#include <system/console.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::PersonalInfo::VCard;
// Read the product identifier from a vCard file
SharedPtr<VCardContact> vcard = VCardContact::Load(u"contact.vcf");
Console::WriteLine(u"vCard product ID: " + vcard->get_ExplanatoryInfo()->get_ProdId());
// The identifier is kept when the contact is converted to MSG
auto msgStream = MakeObject<IO::MemoryStream>();
vcard->Save(msgStream, ContactSaveFormat::Msg);
msgStream->set_Position(0);
SharedPtr<MapiMessage> msg = MapiMessage::Load(msgStream);
if (msg->get_SupportedType() == MapiItemType::Contact)
{
auto contact = System::DynamicCast<MapiContact>(msg->ToMapiMessageItem());
Console::WriteLine(u"MapiContact product ID: " + contact->get_ProductId());
// Save the contact to MSG with a different product identifier
auto options = MakeObject<MapiContactSaveOptions>();
options->set_ProductId(u"-//Contoso//Address Book 2.0//EN");
contact->Save(u"contact_out.msg", options);
}
För att sätta identifieraren för en ny kontakt, anropa set_ProductId() eller skicka identifieraren genom VCardSaveOptions vid sparande av kontakten som ett vCard:
#include <Mapi/MapiContact.h>
#include <Mapi/ContactSaveFormat.h>
#include <PersonalInfo/vCard/VCardSaveOptions.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::PersonalInfo::VCard;
auto contact = MakeObject<MapiContact>(u"John Smith", String(u"john.smith@example.com"));
contact->set_ProductId(u"-//Contoso//Address Book 2.0//EN");
contact->Save(u"contact_out.vcf", ContactSaveFormat::VCard);
// Override the identifier for a single save operation
auto options = MakeObject<VCardSaveOptions>();
options->set_ProductId(u"-//Contoso//Export Tool 1.0//EN");
contact->Save(u"contact_export.vcf", options);
Se även
- Arbeta med vCard (VCF)-filer - formatneutral
VCardContactklass. - Arbete med kontakter i PST-fil - kontakter lagrade i en PST.