Menghubungkan ke Server Exchange

Untuk terhubung ke server Exchange 2007, 2010, dan 2013 menggunakan Exchange Web Service, Aspose.Email menyediakan IEWSClient antarmuka yang mengimplementasikan EWSClient kelas. The EWSClient.getEWSClient metode menginstansiasi dan mengembalikan sebuah IEWSClient objek yang selanjutnya digunakan untuk melakukan operasi terkait kotak surat Exchange dan folder lain. Artikel ini menunjukkan cara menginstansiasi objek dari IEWSClient.

Menghubungkan ke Server Exchange menggunakan EWS

Potongan kode berikut menunjukkan cara terhubung menggunakan Exchange Web Service (EWS).

private static IEWSClient getExchangeEWSClient() {
    final String mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
    final String domain = "";
    final String username = "username@onmicrosoft.com";
    final String password = "password";
    NetworkCredential credentials = new NetworkCredential(username, password, domain);
    IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
    return client;
}

Menghubungkan ke Server Exchange menggunakan IMAP

Microsoft Exchange Server mendukung protokol IMAP untuk mengakses item dalam kotak surat. Gunakan Aspose.Email’s ImapClient kelas untuk terhubung ke Exchange Server menggunakan protokol IMAP. Untuk informasi lebih lanjut tentang ImapClient kelas. Pertama, pastikan layanan IMAP diaktifkan untuk Server Exchange Anda:

  1. Buka Panel Kontrol.
  2. Buka Alat Administrator, lalu Layanan.
  3. Periksa status layanan Microsoft Exchange IMAP4.
  4. Jika belum berjalan, aktifkan/nyalakan.

Potongan kode berikut menunjukkan cara terhubung dan mencantumkan pesan dari folder Kotak Masuk server Microsoft Exchange menggunakan protokol IMAP.

// Connect to Exchange Server using ImapClient class
ImapClient imapClient = new ImapClient("ex07sp1", "Administrator", "Evaluation1");
imapClient.setSecurityOptions(SecurityOptions.Auto);

// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);

// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
    System.out.println(msgInfo.getSubject());
}
// Disconnect from the server
imapClient.dispose();

Potongan kode berikut menunjukkan cara menggunakan SSL.

public static void run() {
    // Connect to Exchange Server using ImapClient class
    ImapClient imapClient = new ImapClient("ex07sp1", 993, "Administrator", "Evaluation1");
    imapClient.setSecurityOptions(SecurityOptions.SSLExplicit);

    // Select the Inbox folder
    imapClient.selectFolder(ImapFolderInfo.IN_BOX);

    // Get the list of messages
    ImapMessageInfoCollection msgCollection = imapClient.listMessages();
    for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
        System.out.println(msgInfo.getSubject());
    }
    // Disconnect from the server
    imapClient.dispose();
}

Setelah terhubung ke server Exchange menggunakan IMAP dan mendapatkan IMapMessageInfoCollection, Potongan kode berikut menunjukkan cara menggunakan MessageInfo nomor urut objek untuk menyimpan pesan tertentu.

// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);
// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
    // Fetch the message from inbox using its SequenceNumber from msgInfo
    MailMessage message = imapClient.fetchMessage(msgInfo.getSequenceNumber());

    // Save the message to disc now
    message.save(dataDir + msgInfo.getSequenceNumber() + "_out.msg", SaveOptions.getDefaultMsgUnicode());
}