การเชื่อมต่อกับ Exchange Server

เพื่อเชื่อมต่อกับ Exchange server 2007, 2010 และ 2013 โดยใช้ Exchange Web Service, Aspose.Email มี IEWSClient อินเทอร์เฟซที่ทำงานตาม EWSClient คลาส. เมธอด EWSClient.getEWSClient เมธอดสร้างอินสแตนซ์และคืนค่า IEWSClient ออบเจกต์ที่ใช้ต่อไปเพื่อทำการดำเนินการที่เกี่ยวกับกล่องเมล Exchange และโฟลเดอร์อื่น ๆ บทความนี้แสดงวิธีสร้างอินสแตนซ์ของออบเจกต์ IEWSClient.

เชื่อมต่อกับ Exchange Server ด้วย EWS

โค้ดส่วนต่อไปนี้จะแสดงวิธีเชื่อมต่อโดยใช้ 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;
}

เชื่อมต่อกับ Exchange Server ด้วย IMAP

Microsoft Exchange Server รองรับโปรโตคอล IMAP สำหรับการเข้าถึงรายการในกล่องเมล ใช้ของ Aspose.Email’s ImapClient คลาสเพื่อเชื่อมต่อกับ Exchange Server ด้วยโปรโตคอล IMAP ข้อมูลเพิ่มเติมเกี่ยวกับ ImapClient คลาส ก่อนแรกให้แน่ใจว่า IMAP service เปิดใช้งานสำหรับ Exchange Server ของคุณ:

  1. เปิด Control Panel.
  2. ไปที่ Administrator Tools แล้วเลือก Services.
  3. ตรวจสอบสถานะของบริการ Microsoft Exchange IMAP4.
  4. หากยังไม่ได้ทำงาน ให้เปิดใช้งาน/เริ่มต้นมัน.

โค้ดส่วนต่อไปนี้จะแสดงวิธีเชื่อมต่อและแสดงรายการข้อความจากโฟลเดอร์ Inbox ของ Microsoft exchange server โดยใช้โปรโตคอล 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();

โค้ดส่วนต่อไปนี้จะแสดงวิธีใช้ 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();
}

หลังจากเชื่อมต่อกับ Exchange server ด้วย IMAP และรับ IMapMessageInfoCollection, โค้ดส่วนต่อไปนี้จะแสดงวิธีใช้ MessageInfo ลำดับเลขของออบเจกต์เพื่อบันทึกข้อความเฉพาะ.

// 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());
}