Outlook 联系人管理
本文介绍了独立的 Outlook MapiContact 项目保存为 MSG 或 vCard(VCF)。对于与格式无关的 VCardContact 直接读取和写入 vCard 文件的类,请参见 使用 vCard(VCF)文件。要在 PST 中存储和读取联系人,请参见 在 PST 文件中管理联系人.
与 MapiMessage 类似,Aspose.Email 允许您创建 Outlook 联系人。 MapiContact 该类提供了创建 Outlook 联系人所需的所有相关属性。本文展示了如何使用该类创建、保存和读取 Outlook 联系人。 MapiContact 类。
创建并保存 Outlook 联系人
创建联系人并保存到磁盘:
- 实例化一个新的对象 MapiContact 类。
- 输入联系人属性信息。
- 添加照片数据(如果有)。
- 将联系人保存为 MSG 或 VCard 格式。
以下代码片段展示如何创建并保存 Outlook 联系人。
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
MapiContact contact = new MapiContact();
contact.NameInfo = new MapiContactNamePropertySet("Bertha", "A.", "Buell");
contact.ProfessionalInfo = new MapiContactProfessionalPropertySet("Awthentikz", "Social work assistant");
contact.PersonalInfo.PersonalHomePage = "B2BTies.com";
contact.PhysicalAddresses.WorkAddress.Address = "Im Astenfeld 59 8580 EDELSCHROTT";
contact.ElectronicAddresses.Email1 = new MapiContactElectronicAddress("Experwas", "SMTP", "BerthaABuell@armyspy.com");
contact.Telephones = new MapiContactTelephonePropertySet("06605045265");
contact.PersonalInfo.Children = new string[] { "child1", "child2", "child3" };
contact.Categories = new string[] { "category1", "category2", "category3" };
contact.Mileage = "Some test mileage";
contact.Billing = "Test billing information";
contact.OtherFields.Journal = true;
contact.OtherFields.Private = true;
contact.OtherFields.ReminderTime = new DateTime(2014, 1, 1, 0, 0, 55);
contact.OtherFields.ReminderTopic = "Test topic";
contact.OtherFields.UserField1 = "ContactUserField1";
contact.OtherFields.UserField2 = "ContactUserField2";
contact.OtherFields.UserField3 = "ContactUserField3";
contact.OtherFields.UserField4 = "ContactUserField4";
// Add a photo
using (FileStream fs = File.OpenRead(dataDir + "Desert.jpg"))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
contact.Photo = new MapiContactPhoto(buffer,
MapiContactPhotoImageFormat.Jpeg);
}
// Save the Contact in MSG format
contact.Save(dataDir + "MapiContact_out.msg",ContactSaveFormat.Msg);
// Save the Contact in VCF format
contact.Save(dataDir + "MapiContact_out.vcf", ContactSaveFormat.VCard);
将 MAPI 分发列表保存为 VCF 文件
下面的代码示例演示了如何将分发列表保存为多联系人 VCF 文件:
// convert the `msg` object to a `MapiDistributionList` object
var dlist = (MapiDistributionList)msg.ToMapiMessageItem();
//save the distribution list
var options = new MapiDistributionListSaveOptions(ContactSaveFormat.VCard);
dlist.Save("distribution_list.vcf", options);
将多联系人 VCF 文件转换为 MapiDistributionList
Aspose.Email 支持将多联系人 VCF 文件转换为 MapiDistributionList 对象,便于直接在应用程序中管理和导入多个联系人。此功能可通过以下静态方法访问: MapiDistributionList 类:
- static MapiDistributionList FromVCF(string filePath)
- static MapiDistributionList FromVCF(Stream stream)
下面的代码示例演示了如何使用此功能:
// Convert a multi-contact VCF file to a MapiDistributionList
MapiDistributionList dlist = MapiDistributionList.FromVCF(fileName);
以 VCF 格式保存联系人
要将联系人保存为版本 3 的 VCF 格式,请使用 VCardVersion 枚举以设置 VCardSaveOptions.Version 属性。以下示例代码演示了如何使用 VCardVersion 枚举以保存联系人 VCF 版本 3 格式:
var options = new MapiDistributionListSaveOptions(ContactSaveFormat.VCard);
options.Version = VCardVersion.V30;
dlist.Save("distribution_list.vcf", options);
读取 MAPI 联系人
该 MapiContact 该类可用于加载 Outlook MSG 和 VCard 格式的联系人。以下代码片段展示了如何将保存为 MSG 和 VCF 的 Outlook 联系人加载到 MapiContact.
从 MSG 文件加载联系人
以下代码片段展示了如何从 MSG 加载联系人。
MapiMessage msg = MapiMessage.FromFile(dataDir + "Contact.msg");
MapiContact contact = (MapiContact)msg.ToMapiMessageItem();
Console.WriteLine(contact.NameInfo.DisplayName);
从 VCard 加载联系人
以下代码片段展示了如何将 vCard 文件加载到 MapiContact 使用 FromVCard 方法。要将 vCard 加载到 VCardContact 而不是,请参见 使用 vCard(VCF)文件.
MapiContact contact = MapiContact.FromVCard(dataDir + "Contact.vcf");
使用自定义选项从 vCard 加载 MAPI 联系人
为在将 vCard(.vcf)文件转换为 MAPI 联系人时提供更大灵活性,Aspose.Email for .NET 提供了一个重载 MapiContact.FromVCard(string filePath, VCardLoadOptions options) 接受以下参数的方法 VCardLoadOptions 对象。它提供了对 vCard 文件解释方式的增强控制——尤其在处理不同的 vCard 格式、编码或高级解析场景时。
以下代码示例演示了如何将 .vcf 联系人文件加载到一个 MapiContact 使用接受以下参数的重载创建对象 VCardLoadOptions。加载的联系人随后可用于 PST 文件、MSG 导出或其他 Outlook 兼容格式。
var mapiContact = MapiContact.FromVCard("contact.vcf", new VCardLoadOptions());
Console.WriteLine(mapiContact.NameInfo.DisplayName);
使用指定编码从 VCard 加载联系人
vCard 文件的编码通过 PreferredEncoding 属性的 VCardLoadOptions 类。以下代码片段展示了如何使用指定的编码从 VCard 加载联系人。
var loadOptions = new VCardLoadOptions { PreferredEncoding = Encoding.UTF8 };
var contactReadFromFile = MapiContact.FromVCard(dataDir + @"Contact.vcf", loadOptions);
保存带扩展字段的 VCard 文件
该 UseExtensions 该属性允许您控制在保存 vCard 文件时是否可以使用扩展字段。设置为 true(默认)时,允许使用扩展,兼容自定义字段和额外的联系人信息。
以下代码示例展示了在保存联系人时如何禁用扩展字段:
var contact = MapiContact.FromVCard("contact.vcf");
var options = new VCardSaveOptions
{
UseExtensions = false
};
contact.Save("contact_out.vcf", options);
直接处理 vCard 文件
使用与格式无关的方式读取和写入 vCard(VCF)文件 VCardContact 类读取和写入 vCard 文件——加载单个或多个联系人,选择文本编码,使用特定的 vCard 版本保存,以及异步加载——在专门的文章中有所介绍: 使用 vCard(VCF)文件.
将联系信息渲染为 MHTML
可以使用 Aspose.Email API 将 Outlook 联系人转换为 MHTML。此示例展示如何将 VCard 加载到 MapiContact 然后在以下帮助下转换为 MHTML MailMessage API。
//Load VCF Contact and convert to MailMessage for rendering to MHTML
var contact = MapiContact.FromVCard("Contact.vcf");
MemoryStream ms = new MemoryStream();
contact.Save(ms, ContactSaveFormat.Msg);
ms.Position = 0;
MapiMessage msg = MapiMessage.Load(ms, new MsgLoadOptions());
MailConversionOptions op = new MailConversionOptions();
MailMessage eml = msg.ToMailMessage(op);
//Prepare the MHT format options
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.CheckBodyContentEncoding = true;
mhtSaveOptions.PreserveOriginalBoundaries = true;
MhtFormatOptions formatOp = MhtFormatOptions.WriteHeader | MhtFormatOptions.RenderVCardInfo;
mhtSaveOptions.RenderedContactFields = ContactFieldsSet.NameInfo | ContactFieldsSet.PersonalInfo | ContactFieldsSet.Telephones | ContactFieldsSet.Events;
mhtSaveOptions.MhtFormatOptions = formatOp;
eml.Save("ContactMhtml_out.mhtml", mhtSaveOptions);
另见
- 使用 vCard(VCF)文件 — 直接使用
VCardContact类。 - 在 PST 文件中管理联系人 —— 在 PST 中存储和读取联系人。