Lavorare con i contatti su Exchange Server
Ottenere i contatti con EWS
Aspose.Email fornisce il EWSClient classe per connettersi a Microsoft Exchange Server usando Exchange Web Services. I successivi frammenti di codice usano Exchange Web Services per leggere tutti i contatti. Il frammento di codice seguente mostra come ottenere i contatti con EWS.
// 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.getMailboxInfo().getContactsUri());
for (Contact contact : contacts) {
MapiContact mapiContact = Contact.to_MapiContact(contact);
// Display name and email address
System.out.println("Name: " + mapiContact.getNameInfo().getDisplayName() + "+ Email Address: " + mapiContact.getElectronicAddresses().getEmail1());
}
Risoluzione dei contatti usando il nome del contatto
Il seguente frammento di codice mostra come usare get contacts con EWS
// 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);
for (Contact c : contacts) {
MapiContact contact = Contact.to_MapiContact(c);
// Display name and email address
System.out.println("Name: " + contact.getNameInfo().getDisplayName() + "+ Email Address: " + contact.getElectronicAddresses().getEmail1());
}
Determinazione del formato delle note del contatto
NotesFormat specifica il tipo di formato del testo delle note dei contatti definito dall’enumeratore TextFormat.
Recuperare contatto usando ID
Un contatto specifico può essere recuperato dal server usando il suo ID contatto come mostrato nel seguente esempio di codice.
Contact fetchedContact = client.getContact(id);
Aggiunta di contatti
Il EWSClient classe createContact() Il metodo può essere usato per aggiungere informazioni di contatto a un Exchange Server. Il createContact() il metodo accetta un Contact oggetto come parametro di input.
Per aggiungere contatti a un Exchange Server:
- Inizializza l’EWSClient con indirizzo e credenziali.
- Inizializza l’oggetto Contact con le proprietà desiderate.
- Chiama il metodo CreateContact per aggiungere il contatto al Exchange Server.
Aspose.Email fornisce il EWSClient classe per connettersi a Microsoft Exchange Server usando Exchange Web Services. Il frammento di codice mostra come utilizzare Exchange Web Services per aggiungere contatti a un Exchange Server.
// 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.setGender(Gender.Male);
contact.setDisplayName("Frank Lin");
contact.setCompanyName("ABC Co.");
contact.setJobTitle("Executive Manager");
PhoneNumber tmp0 = new PhoneNumber();
tmp0.setNumber("123456789");
tmp0.setCategory(PhoneNumberCategory.getHome());
// Add Phone numbers
contact.getPhoneNumbers().add(tmp0);
AssociatedPerson tmp1 = new AssociatedPerson();
tmp1.setName("Catherine");
tmp1.setCategory(AssociatedPersonCategory.getSpouse());
// contact's associated persons
contact.getAssociatedPersons().add(tmp1);
AssociatedPerson tmp2 = new AssociatedPerson();
tmp2.setName("Bob");
tmp2.setCategory(AssociatedPersonCategory.getChild());
contact.getAssociatedPersons().add(tmp2);
AssociatedPerson tmp3 = new AssociatedPerson();
tmp3.setName("Merry");
tmp3.setCategory(AssociatedPersonCategory.getSister());
contact.getAssociatedPersons().add(tmp3);
Url tmp4 = new Url();
tmp4.setHref("www.blog.com");
tmp4.setCategory(UrlCategory.getBlog());
// URLs
contact.getUrls().add(tmp4);
Url tmp5 = new Url();
tmp5.setHref("www.homepage.com");
tmp5.setCategory(UrlCategory.getHomePage());
contact.getUrls().add(tmp5);
EmailAddress tmp6 = new EmailAddress();
tmp6.setAddress("Frank.Lin@Abc.com");
tmp6.setDisplayName("Frank Lin");
tmp6.setCategory(EmailAddressCategory.getEmail1());
// Set contact's Email address
contact.getEmailAddresses().add(tmp6);
try {
client.createContact(contact);
} catch (java.lang.RuntimeException ex) {
System.out.println(ex.getMessage());
}
Aggiornamento dei contatti
Le informazioni di contatto possono essere aggiornate usando Microsoft Outlook. Aspose.Email può anche aggiornare le informazioni di contatto su Exchange Server usando Exchange Web Service (EWS). Il di IEWSClient updateContact() il metodo può aggiornare le informazioni del contatto su Exchange Server.
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
// List all the contacts and Loop through all contacts
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
Contact contact = contacts[0];
System.out.println("Name: " + contact.getDisplayName());
contact.setDisplayName("David Ch");
client.updateContact(contact);
Eliminazione dei contatti
Il EWSClient classe fornisce il deleteItem per accedere ed eliminare contatti dalla cartella Contatti di un Exchange Server. Questo metodo accetta l’ID del contatto come parametro di input.
Per eliminare contatti da un Exchange Server:
- Inizializza l’ExchangeWebServiceClient con l’indirizzo e le credenziali.
- Elimina un contatto usando il suo ID.
Il seguente frammento di codice mostra come eliminare i contatti da un server Exchange usando Exchange Web Service.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
String strContactToDelete = "John Teddy";
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
for (Contact contact : contacts) {
if (contact.getDisplayName().equals(strContactToDelete))
client.deleteItem(contact.getId().getEWSId(), DeletionOptions.getDeletePermanently());
}
client.dispose();