اتصال به سرور Exchange

برای اتصال به سرورهای Exchange 2007، 2010 و 2013 با استفاده از Exchange Web Service، Aspose.Email فراهم می‌کند IEWSClient رابطی که پیاده‌سازی می‌کند EWSClient کلاس. EWSClient.getEWSClient متد یک IEWSClient شیئی که سپس برای انجام عملیات مرتبط با صندوق‌نامه Exchange و پوشه‌های دیگر استفاده می‌شود. این مقاله نشان می‌دهد چگونه اشیاء از IEWSClient.

اتصال به سرور Exchange با استفاده از 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 با استفاده از IMAP

Microsoft Exchange Server از پروتکل IMAP برای دسترسی به آیتم‌های یک صندوق‌نامه پشتیبانی می‌کند. از Aspose.Email’s ImapClient کلاس برای اتصال به سرور Exchange با استفاده از پروتکل IMAP. برای اطلاعات بیشتر دربارهٔ ImapClient کلاس. ابتدا اطمینان حاصل کنید که سرویس‌های IMAP برای سرور Exchange شما فعال هستند:

  1. پنل کنترل را باز کنید.
  2. به ابزارهای مدیر (Administrator Tools) رفته، سپس Services.
  3. وضعیت سرویس Microsoft Exchange IMAP4 را بررسی کنید.
  4. اگر قبلاً در حال اجرا نیست، آن را فعال/شروع کنید.

قطعه کد زیر نشان می‌دهد چگونه با استفاده از پروتکل IMAP به سرور Microsoft Exchange متصل شوید و پیام‌های پوشه Inbox را فهرست کنید.

// 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 با استفاده از 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());
}