ทำงานกับรายชื่อการกระจาย Outlook

A distribution list groups several recipients under one name, so a message can be addressed to the whole group at once. Outlook keeps such lists among the contacts, and Aspose.Email for C++ represents them with the MapiDistributionList คลาส บทความนี้ทำงานกับรายการแยก - รายการที่บันทึกเป็นไฟล์ MSG หรือ VCF ของตนเอง รายการที่เก็บในไฟล์ PST จะอธิบายใน การจัดการรายการจัดส่งในไฟล์ PST.

แต่ละรายการของรายการเป็น MapiDistributionListMember, เก็บไว้ใน MapiDistributionListMemberCollection. สมาชิกที่สร้างจากชื่อและที่อยู่เป็นสมาชิก ครั้งเดียว : มันมีรายละเอียดของตนเองแทนการชี้ไปที่ผู้ติดต่อในคลัง ซึ่งเป็นสิ่งที่ไฟล์แยกต้องการ

ตัวอย่างด้านล่างใช้ส่วนหัวและเนมสเปซเหล่านี้:

#include <Mapi/MapiMessage.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiDistributionList.h>
#include <Mapi/MapiDistributionListMember.h>
#include <Mapi/MapiDistributionListMemberCollection.h>
#include <Mapi/MapiDistributionListSaveOptions.h>
#include <Mapi/ContactSaveFormat.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;

Create a Distribution List and Save It

นี้ MapiDistributionList คอนสตรัคเตอร์รับชื่อของรายการและสมาชิกของมัน บันทึก เขียนรายการในรูปแบบที่เลือกผ่าน MapiDistributionListSaveOptions: ContactSaveFormat Msg สร้างรายการ Outlook VCard ไฟล์ VCF ที่สมุดที่อยู่อื่น ๆ สามารถนำเข้าได้

// members that exist only inside this list
SharedPtr<MapiDistributionListMemberCollection> oneOffMembers = MakeObject<MapiDistributionListMemberCollection>();
oneOffMembers->Add(MakeObject<MapiDistributionListMember>(u"John R. Patrick", u"JohnRPatrick@armyspy.com"));
oneOffMembers->Add(MakeObject<MapiDistributionListMember>(u"Tilly Bates", u"TillyBates@armyspy.com"));

SharedPtr<MapiDistributionList> list = MakeObject<MapiDistributionList>(u"Simple list", oneOffMembers);
list->set_Body(u"Distribution List Body");
list->set_Subject(u"Sample Distribution List");

// the same list in both formats
list->Save(u"dlist.msg", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::Msg));
list->Save(u"dlist.vcf", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::VCard));

Read a Distribution List from an MSG File

ไฟล์ MSG ถูกเปิดด้วย MapiMessage::Load ไม่ว่ามันจะบรรจุอะไร SupportedType รายงาน MapiItemType::DistList สำหรับรายการกระจาย, และ ToMapiMessageItem ส่งคืนรายการที่ถูกแคสต์เป็น MapiDistributionList; ของมัน Members คอลเล็กชันรายการว่าใครเป็นสมาชิก

SharedPtr<MapiMessage> msg = MapiMessage::Load(u"dlist.msg");

if (msg->get_SupportedType() == MapiItemType::DistList)
{
    SharedPtr<MapiDistributionList> loaded = DynamicCast<MapiDistributionList>(msg->ToMapiMessageItem());
    Console::WriteLine(u"List: " + loaded->get_DisplayName());

    for (SharedPtr<MapiDistributionListMember> member : loaded->get_Members())
    {
        Console::WriteLine(u"   " + member->get_DisplayName() + u" <" + member->get_EmailAddress() + u">");
    }
}

ไฟล์ VCF ที่เก็บหลายติดต่อสามารถแปลงเป็นรายการกระจายได้เช่นกัน - ดู รายการกระจายและไฟล์ VCF หลายติดต่อ.

Change the Members of a Saved List

เพื่อเปลี่ยนรายการที่บันทึกไว้, อ่านสมาชิกของมัน, สร้างคอลเล็กชันสมาชิกใหม่จากพวกมัน, สร้าง MapiDistributionList ด้วยชื่อ, เรื่องและเนื้อหาเดิม, แล้วบันทึกทับไฟล์เก่า ตัวอย่างลบสมาชิกหนึ่ง เพิ่มอีกคนหนึ่งและอ่านไฟล์ใหม่เพื่อแสดงผลลัพธ์

// a standalone list to start from
{
    SharedPtr<MapiDistributionListMemberCollection> members = MakeObject<MapiDistributionListMemberCollection>();
    members->Add(MakeObject<MapiDistributionListMember>(u"John R. Patrick", u"JohnRPatrick@armyspy.com"));
    members->Add(MakeObject<MapiDistributionListMember>(u"Tilly Bates", u"TillyBates@armyspy.com"));
    SharedPtr<MapiDistributionList> list = MakeObject<MapiDistributionList>(u"Simple list", members);
    list->set_Subject(u"Simple list");
    list->Save(u"team.msg", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::Msg));
}

SharedPtr<MapiDistributionList> stored = DynamicCast<MapiDistributionList>(MapiMessage::Load(u"team.msg")->ToMapiMessageItem());

// the new member list: everyone except John, plus Edward
SharedPtr<MapiDistributionListMemberCollection> members = MakeObject<MapiDistributionListMemberCollection>();
for (SharedPtr<MapiDistributionListMember> member : stored->get_Members())
{
    if (member->get_EmailAddress() != u"JohnRPatrick@armyspy.com")
    {
        members->Add(MakeObject<MapiDistributionListMember>(member->get_DisplayName(), member->get_EmailAddress()));
    }
}
members->Add(MakeObject<MapiDistributionListMember>(u"Edward R. Manuel", u"EdwardRManuel@armyspy.com"));

SharedPtr<MapiDistributionList> updated = MakeObject<MapiDistributionList>(stored->get_DisplayName(), members);
updated->set_Subject(stored->get_Subject());
updated->set_Body(stored->get_Body());
updated->Save(u"team.msg", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::Msg));

// read the file again
SharedPtr<MapiDistributionList> reloaded = DynamicCast<MapiDistributionList>(MapiMessage::Load(u"team.msg")->ToMapiMessageItem());
for (SharedPtr<MapiDistributionListMember> member : reloaded->get_Members())
{
    Console::WriteLine(u"   " + member->get_DisplayName() + u" <" + member->get_EmailAddress() + u">");
}

ดูเพิ่มเติม