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

Có thể tạo một danh sách Phân phối bằng Aspose.Email API, đây là một tập hợp của nhiều liên hệ. Danh sách phân phối có thể được lưu vào đĩa ở định dạng Outlook MSG và có thể được xem/điều chỉnh bằng cách mở nó trong MS Outlook.

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

Đoạn mã sau đây cho bạn thấy cách tạo và lưu một danh sách phân phối.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

String displayName1 = "Sebastian Wright";
String email1 = "SebastianWright@dayrep.com";

String displayName2 = "Wichert Kroos";
String email2 = "WichertKroos@teleworm.us";

String strEntryId1;
String strEntryId2;

// Create distribution list from contacts
try (PersonalStorage personalStorage = PersonalStorage.create(dataDir + "CreateDistributionListInPST_out.pst", FileFormatVersion.Unicode)) {
    // Add the contact folder to pst
    FolderInfo contactFolder = personalStorage.createPredefinedFolder("Contacts", StandardIpmFolder.Contacts);

    // Create contacts
    strEntryId1 = contactFolder.addMapiMessageItem(new MapiContact(displayName1, email1));
    strEntryId2 = contactFolder.addMapiMessageItem(new MapiContact(displayName2, email2));

    // Create distribution list on the base of the created contacts
    MapiDistributionListMember member1 = new MapiDistributionListMember(displayName1, email1);
    member1.setEntryIdType(MapiDistributionListEntryIdType.Contact);
    member1.setEntryId(Base64.getDecoder().decode(strEntryId1));

    MapiDistributionListMember member2 = new MapiDistributionListMember(displayName2, email2);
    member2.setEntryIdType(MapiDistributionListEntryIdType.Contact);
    member2.setEntryId(Base64.getDecoder().decode(strEntryId2));

    MapiDistributionListMemberCollection members = new MapiDistributionListMemberCollection();
    members.add(member1);
    members.add(member2);

    MapiDistributionList distributionList = new MapiDistributionList("Contact list", members);
    distributionList.setBody("Distribution List Body");
    distributionList.setSubject("Sample Distribution List using Aspose.Email");

    // Add distribution list to PST
    contactFolder.addMapiMessageItem(distributionList);
}

// Create one-off distribution list members (for which no separate contacts were created)
try (PersonalStorage personalStorage = PersonalStorage.create(dataDir + "CreateDistributionListInPST_OneOffmembers_out.pst", FileFormatVersion.Unicode)) {
    // Add the contact folder to pst
    FolderInfo contactFolder = personalStorage.createPredefinedFolder("Contacts", StandardIpmFolder.Contacts);

    MapiDistributionListMemberCollection oneOffmembers = new MapiDistributionListMemberCollection();
    oneOffmembers.add(new MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com"));
    oneOffmembers.add(new MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com"));

    MapiDistributionList oneOffMembersList = new MapiDistributionList("Simple list", oneOffmembers);
    contactFolder.addMapiMessageItem(oneOffMembersList);
}

Lưu Mapi Distribution List vào một Tệp VCF Đa Liên hệ Đơn

Cái void save(String fileName, MapiDistributionListSaveOptions options) phương thức cho phép bạn lưu Mapi Distribution List vào một tên tệp đã chỉ định bằng cách sử dụng các tùy chọn lưu được cung cấp. Bạn có thể cung cấp tên tệp và một thể hiện của MapiDistributionListSaveOptions lớp như các tham số. The MapiDistributionListSaveOptions lớp chứa các tùy chọn để lưu Mapi Distribution List. Trong trường hợp này, bạn có thể chỉ định định dạng lưu là VCard (ContactSaveFormat.VCard) để lưu danh sách phân phối dưới dạng tệp VCF đa liên hệ.

Đoạn mã sau đây minh họa cách lưu một danh sách phân phối vào tệp VCF đa liên hệ:

MapiDistributionList dlist = (MapiDistributionList)msg.toMapiMessageItem();
MapiDistributionListSaveOptions options = new MapiDistributionListSaveOptions(ContactSaveFormat.VCard);
dlist.save("distribution_list.vcf", options);

Đọc Danh sách Phân phối từ PST

Đoạn mã sau đây cho bạn thấy cách đọc một danh sách phân phối từ tệp PST.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String fileName = "outlook/message.msg";

MapiMessage message = MapiMessage.fromFile(fileName);
MapiDistributionList dlist = (MapiDistributionList)message.toMapiMessageItem();