Trabalhando com Listas de Distribuição
Contents
[
Hide
]
É possível criar uma lista de distribuição usando a API Aspose.Email, que é uma coleção de múltiplos contatos. Uma lista de distribuição pode ser salva no disco no formato MSG do Outlook e pode ser visualizada/ manipulada ao ser aberta no MS Outlook.
Criando e Salvando uma Lista de Distribuição
O seguinte trecho de código mostra como criar e salvar uma lista de distribuição.
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-.NET | |
string dataDir = RunExamples.GetDataDir_Outlook(); | |
string displayName1 = "Sebastian Wright"; | |
string email1 = "SebastianWright@dayrep.com"; | |
string displayName2 = "Wichert Kroos"; | |
string email2 = "WichertKroos@teleworm.us"; | |
string strEntryId1; | |
string strEntryId2; | |
string path = dataDir + "CreateDistributionListInPST_out.pst"; | |
if (File.Exists(path)) | |
{ | |
File.Delete(path); | |
} | |
// Create distribution list from contacts | |
using (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.EntryIdType = MapiDistributionListEntryIdType.Contact; | |
member1.EntryId = Convert.FromBase64String(strEntryId1); | |
MapiDistributionListMember member2 = new MapiDistributionListMember(displayName2, email2); | |
member2.EntryIdType = MapiDistributionListEntryIdType.Contact; | |
member2.EntryId = Convert.FromBase64String(strEntryId2); | |
MapiDistributionListMemberCollection members = new MapiDistributionListMemberCollection(); | |
members.Add(member1); | |
members.Add(member2); | |
MapiDistributionList distributionList = new MapiDistributionList("Contact list", members); | |
distributionList.Body = "Distribution List Body"; | |
distributionList.Subject = "Sample Distribution List using Aspose.Email"; | |
// Add distribution list to PST | |
contactFolder.AddMapiMessageItem(distributionList); | |
} | |
string path1 = dataDir + "CreateDistributionListInPST_OneOffmembers_out.pst"; | |
if (File.Exists(path1)) | |
{ | |
File.Delete(path1); | |
} | |
// Create one-off distribution list members (for which no separate contacts were created) | |
using (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); | |
} |
Lendo uma Lista de Distribuição de um PST
O seguinte trecho de código mostra como ler uma lista de distribuição de um PST.
using Aspose.Email.Storage.Pst;
using Aspose.Email.Mapi;
// Load the PST file
using (var pst = PersonalStorage.FromFile("your.pst"))
{
// Get the Contacts folder
var folder = pst.GetPredefinedFolder(StandardIpmFolder.Contacts);
if (folder != null)
{
foreach (var msgInfo in folder.EnumerateMessages())
{
// Check if the message has the "IPM.DistList" message class
if (msgInfo.MessageClass == "IPM.DistList")
{
// Extract the distribution list
var distList = (MapiDistributionList)pst.ExtractMessage(msgInfo).ToMapiMessageItem();
// Now, you can work with the distribution list
// (e.g., access its members, display its properties, or make modifications)
}
}
}
}
Atualizando uma Lista de Distribuição em PST
O seguinte trecho de código mostra como atualizar uma lista de distribuição em PST.
using Aspose.Email.Mapi;
using Aspose.Email.Storage.Pst;
// Load PST file
using (PersonalStorage pst = PersonalStorage.FromFile("my.pst"))
{
// Get Contacts folder
var folder = pst.GetPredefinedFolder(StandardIpmFolder.Contacts);
// Add a new member to each distribution list in PST
foreach (var msg in folder.EnumerateMessages())
{
// Check if the message has the "IPM.DistList" message class
if (msg.MessageClass == "IPM.DistList")
{
var distList = pst.ExtractMessage(msg).ToMapiMessageItem();
// Create a new member to add
var member = new MapiDistributionListMember("Edward R. Manuel", "EdwardRManuel@example.com");
distList.Members.Add(member);
// Update Distribution List in PST
folder.UpdateMessage(msg.EntryIdString, distList);
}
}
}