Trabajando con Contactos en Exchange Server

Obteniendo Contactos con EWS

Aspose.Email proporciona la clase EWSClient para conectarse a Microsoft Exchange Server utilizando Exchange Web Services. Los fragmentos de código que siguen utilizan Exchange Web Services para leer todos los contactos. El siguiente fragmento de código te muestra cómo obtener contactos con EWS.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// List all the contacts
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri);
foreach (MapiContact contact in contacts)
{
// Display name and email address
Console.WriteLine("Name: " + contact.NameInfo.DisplayName + ", Email Address: " + contact.ElectronicAddresses.Email1);
}

Resolver Contactos usando el Nombre del Contacto

El siguiente fragmento de código te muestra cómo obtener contactos con EWS.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// List all the contacts
Contact[] contacts = client.ResolveContacts("Changed Name", ExchangeListContactsOptions.FetchPhoto);
foreach (MapiContact contact in contacts)
{
// Display name and email address
Console.WriteLine("Name: " + contact.NameInfo.DisplayName + ", Email Address: " + contact.ElectronicAddresses.Email1);
}

Determinar el Formato de Notas del Contacto

El formato de notas de Aspose.Email.Mail.Contact.NotesFormat especifica el tipo de formato de texto de las notas de contacto definido por el enumerador Aspose.Email.TextFormat.

Recuperar Contacto usando Id

Un contacto en particular puede ser recuperado del servidor usando su id de contacto como se muestra en el siguiente ejemplo de código.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
Contact fetchedContact = client.GetContact(id);

Agregar Contactos

La clase EWSClient puede utilizar el método CreateContact() para agregar información de contacto a un Exchange Server. El método CreateContact() recibe un objeto Contact como parámetro de entrada.

Para agregar contactos a un Exchange Server:

  1. Inicializa el EWSClient con dirección y credenciales.
  2. Inicializa el objeto Contact con las propiedades deseadas.
  3. Llama al método CreateContact para agregar el contacto al Exchange Server.

Aspose.Email proporciona la clase EWSClient para conectarse a Microsoft Exchange Server utilizando Exchange Web Services. Los fragmentos de código te muestran cómo utilizar Exchange Web Services para agregar contactos a un Exchange Server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Set mailboxURI, Username, password, domain information
string mailboxUri = "https://ex2010/ews/exchange.asmx";
string username = "test.exchange";
string password = "pwd";
string domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
//Create New Contact
Contact contact = new Contact();
//Set general info
contact.Gender = Gender.Male;
contact.DisplayName = "Frank Lin";
contact.CompanyName = "ABC Co.";
contact.JobTitle = "Executive Manager";
//Add Phone numbers
contact.PhoneNumbers.Add(new PhoneNumber { Number = "123456789", Category = PhoneNumberCategory.Home });
//contact's associated persons
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Catherine", Category = AssociatedPersonCategory.Spouse });
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Bob", Category = AssociatedPersonCategory.Child });
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Merry", Category = AssociatedPersonCategory.Sister });
//URLs
contact.Urls.Add(new Url { Href = "www.blog.com", Category = UrlCategory.Blog });
contact.Urls.Add(new Url { Href = "www.homepage.com", Category = UrlCategory.HomePage });
//Set contact's Email address
contact.EmailAddresses.Add(new EmailAddress { Address = "Frank.Lin@Abc.com", DisplayName = "Frank Lin", Category = EmailAddressCategory.Email1 });
try
{
client.CreateContact(contact);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Actualizar Contactos

La información del contacto puede ser actualizada usando Microsoft Outlook. Aspose.Email también puede actualizar la información del contacto en Exchange Server usando el servicio web de Exchange (EWS). El método IEWSClient UpdateContact puede actualizar la información del contacto en Exchange Server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
// List all the contacts and Loop through all contacts
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri);
Contact contact = contacts[0];
Console.WriteLine("Name: " + contact.DisplayName);
contact.DisplayName = "David Ch";
client.UpdateContact(contact);

Eliminar Contactos

La clase EWSClient permite que el método DeleteContact acceda y elimine contactos de la carpeta de contactos de un Exchange Server. Este método toma el ID del contacto o MapiContact como parámetro de entrada.

Para eliminar contactos de un Exchange Server:

  1. Inicializa el ExchangeWebServiceClient con dirección y credenciales.
  2. Elimina un contacto usando su ID.
  3. Elimina un contacto llamando al método DeleteContact con MapiContact como parámetro de entrada.

Los siguientes fragmentos de código te muestran cómo eliminar contactos de un servidor de intercambio utilizando el servicio web de intercambio.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
string strContactToDelete = "John Teddy";
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri);
foreach (Contact contact in contacts)
{
if (contact.DisplayName.Equals(strContactToDelete))
client.DeleteItem(contact.Id.EWSId, DeletionOptions.DeletePermanently);
}
client.Dispose();

Trabajando con Propiedades Extendidas de Contactos en Exchange Server

 IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");

//Las propiedades extendidas requeridas deben ser añadidas para poder crearlas o leerlas desde el Exchange Server

string[] extraFields = new string[] {{ "Campo de Usuario 1", "Campo de Usuario 2", "Campo de Usuario 3", "Campo de Usuario 4" }};

foreach (string extraField in extraFields)
    client.ContactExtendedPropertiesDefinition.Add(extraField);

//Crear un contacto de prueba en el Exchange Server

Contact contact = new Contact();

contact.DisplayName = "EMAILNET-38433 - " + Guid.NewGuid().ToString();

foreach (string extraField in extraFields)
    contact.ExtendedProperties.Add(extraField, extraField);

string contactId = client.CreateContact(contact);

//recuperar el contacto de nuevo del servidor después de algún tiempo

Thread.Sleep(5000);

contact = client.GetContact(contactId);

//Parsear las propiedades extendidas del contacto

foreach (string extraField in extraFields)
    if (contact.ExtendedProperties.ContainsKey(extraField))
        Console.WriteLine(contact.ExtendedProperties[extraField].ToString());