התחברות ל‑Exchange Server

כדי להתחבר ל‑Exchange servers 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 של ImapClient מחלקה להתחברות ל‑Exchange Server באמצעות פרוטוקול IMAP. למידע נוסף על ה‑ ImapClient מחלקה. ראשית, ודא ששירותי IMAP מופעלים ב‑Exchange Server שלך:

  1. פתח את לוח הבקרה.
  2. גש לכלי הניהול, ואז לשירותים.
  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());
}