Adding MapiDistributionList to PST
Contents
[
Hide
]
Create New PST, Add Sub-folders and Messages showed how to create a PST file and add a subfolder to it. With Aspose.Email you can add a MapiDistributionList to the Contacts subfolder of a PST file that you have created or loaded.
In order to set the EntryId for a MapiDistributionListMember, the base64String needs to be converted using Apache Commons Codec.
Load MapidistributionList from file
The code below loads a MAPI distribution list from a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
MapiMessage msg = MapiMessage.fromFile(dataDir + "DistList.msg"); | |
MapiDistributionList dlist = (MapiDistributionList) msg.toMapiMessageItem(); |
Create a New MapiDistributionList and Add it to the Contacts Subfolder
Below are the steps to add a MapiDistributionList to a PST:
- Create a new PST.
- Add the Contacts folder to PST.
- Create sample contacts.
- Create a distribution list on the base of the created contacts.
- Add the distribution list to PST.
The code snippet below shows how to create a MapiDistributionList and then add it to the Contacts folder of a newly created PST file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
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 | |
PersonalStorage pst = PersonalStorage.create(dataDir + "pstFileName1_out.pst", FileFormatVersion.Unicode); | |
// Add the contact folder to the PST | |
FolderInfo contactFolder = pst.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); | |
byte[] decodedBytes = Base64.decodeBase64(strEntryId1); | |
member1.setEntryId(decodedBytes); | |
MapiDistributionListMember member2 = new MapiDistributionListMember(displayName2, email2); | |
member2.setEntryIdType(MapiDistributionListEntryIdType.Contact); | |
decodedBytes = Base64.decodeBase64(strEntryId2); | |
member2.setEntryId(decodedBytes); | |
MapiDistributionListMemberCollection members = new MapiDistributionListMemberCollection(); | |
members.addItem(member1); | |
members.addItem(member2); | |
MapiDistributionList distributionList = new MapiDistributionList("Contact list", members); | |
distributionList.setBody("Distribution List Body!"); | |
distributionList.setSubject("Distribution List Subject!"); | |
// Add distribution list to PST | |
contactFolder.addMapiMessageItem(distributionList); |
Create a One-off Distribution List
For this distribution list, no separate contacts are required.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
PersonalStorage pst = PersonalStorage.create(dataDir + "pstFileName2_out.pst", FileFormatVersion.Unicode); | |
// Add the contact folder to the PST | |
FolderInfo contactFolder = pst.createPredefinedFolder("Contacts", StandardIpmFolder.Contacts); | |
MapiDistributionListMemberCollection oneOffmembers = new MapiDistributionListMemberCollection(); | |
oneOffmembers.addItem(new MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com")); | |
oneOffmembers.addItem(new MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com")); | |
MapiDistributionList oneOffMembersList = new MapiDistributionList("Simple list", oneOffmembers); | |
contactFolder.addMapiMessageItem(oneOffMembersList); |