
Email distribution lists make it possible to send emails to a group of people without writing individual email addresses. You can create a number of lists based on types of people such as official, social, etc. MS Outlook also allows you to make the distribution lists and often you may need to create such lists programmatically. So let’s see how to create and read MS Outlook distribution lists programmatically in Java.
- Java API to Create MS Outlook Distribution Lists
- Create a Distribution List in MS Outlook
- Read an MS Outlook Distribution List
Java API to Create MS Outlook Distribution Lists
Aspose.Email for Java is an amazing API to work with emails. It lets you create, send, and process emails and work with various email clients including MS Outlook. We will use this API to create and read the Outlook distribution lists in this article. You can either download the API or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-email</artifactId>
<version>22.3</version>
<classifier>jdk16</classifier>
</dependency>
Create a Distribution List in MS Outlook in Java
MS Outlook uses PST format to store the information about the distribution lists. Let’s see how to create a distribution list in MS Outlook programmatically using Aspose.Email for Java.
- First, create string objects to store details of the members.
- Then, create a PST file using the PersonalStorage.create() method to store the distribution list.
- After that, make a new folder in PST using PersonalStorage.createPredefinedFolder() method and set its name.
- Then, create a MapiDistributionListMember object for each member and initialize it.
- Create a new MapiDistributionListMemberCollection object and add members to it.
- Assign the collection to a MapiDistributionList object.
- Finally, add a distribution list to the folder of PST using the FolderInfo.addMapiMessageItem() method.
The following code sample shows how to create an MS Outlook distribution list in Java.
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 + "list.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 a collection to keep members | |
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)); | |
// Add members to collection | |
MapiDistributionListMemberCollection members = new MapiDistributionListMemberCollection(); | |
members.add(member1); | |
members.add(member2); | |
// Create list | |
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); | |
} |
Read an MS Outlook Distribution List in Java
You can also read an Outlook distribution list and fetch the contacts’ information. The following steps show how to perform this operation.
- Load the distribution list from the PST file using MapiMessage.load() method.
- Fetch the list using MapiMessage.toMapiMessageItem() method and cast it to MapiDistributionList.
- Read the contacts from the MapiDistributionList object.
The following code sample shows how to read an MS Outlook distribution list in Java.
// Load PST file | |
MapiMessage message = MapiMessage.load("list.pst"); | |
// Fetch distribution list | |
MapiDistributionList dlist = (MapiDistributionList)message.toMapiMessageItem(); | |
// Get members collection | |
MapiDistributionListMemberCollection members = dlist.getMembers(); | |
// Read each MapiDistributionListMember from collection | |
MapiDistributionListMember member1 = members.get(0); |
Get a Free API License
You can use Aspose.Email for Java without evaluation limitations by getting a free temporary license.
Conclusion
A distribution list in MS Outlook lets you send an email to a group of people. In this article, you have learned how to create MS Outlook distribution lists in Java. Furthermore, you have seen how to read the Outlook distribution lists programmatically. Besides, you can explore more about the Java email API using the documentation. Also, you can post your questions or queries on our forum.