Làm việc với Danh sách Phân phối Outlook

Một danh sách phân phối nhóm nhiều người nhận dưới một tên, để một tin nhắn có thể gửi đến toàn bộ nhóm cùng một lúc. Outlook giữ các danh sách này trong danh bạ, và Aspose.Email cho C++ đại diện chúng bằng MapiDistributionList lớp. Bài viết này làm việc với các danh sách độc lập - các danh sách được lưu dưới dạng tệp MSG hoặc VCF riêng. Các danh sách lưu trong tệp PST được đề cập trong Quản lý Danh sách Phân phối trong Tệp PST.

Mỗi mục nhập của danh sách là một MapiDistributionListMember, được giữ trong một MapiDistributionListMemberCollection. Một thành viên được tạo từ tên và địa chỉ là một thành viên độc lập: nó mang chi tiết riêng thay vì trỏ tới một liên hệ trong kho, đây là điều một tệp độc lập cần.

Các mẫu dưới đây sử dụng các tiêu đề và không gian tên này:

#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;

Tạo Danh sách Phân phối và Lưu lại

Cái MapiDistributionList hàm khởi tạo nhận tên danh sách và các thành viên của nó. Lưu ghi danh sách ở định dạng được chọn qua MapiDistributionListSaveOptions: ContactSaveFormat Msg sản xuất một mục Outlook, VCard một tệp VCF mà các sổ địa chỉ khác có thể nhập.

// 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));

Đọc Danh sách Phân phối từ Tệp MSG

Một tệp MSG được mở bằng MapiMessage::Load bất cứ gì nó chứa. SupportedType báo cáo MapiItemType::DistList cho một danh sách phân phối, và ToMapiMessageItem trả về mục được ép kiểu thành MapiDistributionList; của nó Members tập hợp liệt kê ai thuộc về nó.

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">");
    }
}

Một tệp VCF chứa nhiều liên hệ cũng có thể được chuyển thành danh sách phân phối - xem Danh sách Phân phối và Tệp VCF Đa Liên hệ.

Thay đổi Thành viên của Danh sách Đã lưu

Để thay đổi một danh sách đã lưu, đọc các thành viên của nó, xây dựng bộ sưu tập thành viên mới từ chúng, tạo một MapiDistributionList với tên, tiêu đề và nội dung gốc, và lưu lên tệp cũ. Mẫu loại bỏ một thành viên, thêm một thành viên khác và đọc lại tệp để hiển thị kết quả.

// 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">");
}

Xem Thêm