คุณลักษณะยูทิลิตี้ใน Aspose.Email สำหรับ .NET

การทำงานกับการสื่อสารแบบรวม

Aspose.Email สามารถเรียกคืนข้อมูลการสื่อสารแบบรวมจาก Exchange Server 2010. การสื่อสารแบบรวมเช่น การรับข้อมูลการกำหนดค่า, เริ่มการโทรออก, ดึงข้อมูลการโทรตาม ID การโทรและตัดการโทรตาม ID ได้รับการสนับสนุนในปัจจุบัน. ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการดึงข้อมูลการกำหนดค่าการสื่อสารแบบรวมจาก Microsoft Exchange Server 2010.

IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential);
UnifiedMessagingConfiguration umConf = client.GetUMConfiguration();

รับเคล็ดลับอีเมล

Microsoft Exchange Server ได้เพิ่มคุณลักษณะใหม่หลายอย่างใน Exchange Server 2010 และ 2013 หนึ่งในนั้นทำให้ผู้ใช้สามารถรับเคล็ดลับการส่งเมลขณะเขียนข้อความอีเมลได้ เคล็ดลับเหล่านี้มีประโยชน์อย่างมากเพราะให้ข้อมูลก่อนที่อีเมลจะถูกส่ง ตัวอย่างเช่น หากที่อยู่อีเมลในรายการผู้รับผิดพลาด จะมีเคล็ดลับแสดงให้คุณทราบว่าที่อยู่นั้นไม่ถูกต้อง เคล็ดลับการส่งเมลยังทำให้คุณเห็นการตอบกลับนอกสำนักก่อนส่งอีเมล: Exchange Server (2010 & 2013) จะส่งเคล็ดลับการส่งเมลเมื่อกำลังเขียนข้อความอีเมล หากผู้รับหนึ่งคนหรือหลายคนตั้งค่าการตอบกลับนอกสำนัก Microsoft Exchange Server 2010 Service Pack 1 เป็นสิ่งจำเป็นสำหรับคุณลักษณะทั้งหมดที่แสดงในบทความนี้ ส่วนโค้ดตัวอย่างต่อไปนี้แสดงวิธีใช้ EWSClient คลาสที่ใช้ Exchange Web Services, มีให้ใน Microsoft Exchange Server 2007 และรุ่นต่อ ๆ ไป.

// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
Console.WriteLine("Connected to Exchange server...");
// Provide mail tips options
MailAddressCollection addrColl = new MailAddressCollection();
addrColl.Add(new MailAddress("test.exchange@ex2010.local", true));
addrColl.Add(new MailAddress("invalid.recipient@ex2010.local", true));
GetMailTipsOptions options = new GetMailTipsOptions("administrator@ex2010.local", addrColl, MailTipsType.All);

// Get Mail Tips
MailTips[] tips = client.GetMailTips(options);

// Display information about each Mail Tip
foreach (MailTips tip in tips)
{
    // Display Out of office message, if present
    if (tip.OutOfOffice != null)
    {
        Console.WriteLine("Out of office: " + tip.OutOfOffice.ReplyBody.Message);
    }

    // Display the invalid email address in recipient, if present
    if (tip.InvalidRecipient == true)
    {
        Console.WriteLine("The recipient address is invalid: " + tip.RecipientAddress);
    }
}

การแสดงตนของ Exchange

การแอบทำหน้าที่ (Impersonation) ของ Exchange อนุญาตให้บุคคลหนึ่งแอบทำหน้าที่เป็นบัญชีอื่นและดำเนินการต่าง ๆ โดยใช้สิทธิ์ของบัญชีที่ถูกแอบทำหน้าที่แทนของตนเอง ในขณะที่การมอบหมาย (delegation) ให้ผู้ใช้ทำงานในนามของผู้ใช้อื่น การแอบทำหน้าที่ทำให้พวกเขาสามารถทำงานเหมือนผู้ใช้คนอื่น Aspose.Email รองรับการแอบทำหน้าที่ของ Exchange. The EWSClient class ให้ ImpersonateUser และ ResetImpersonation เมธอดเพื่ออำนวยความสะดวกให้กับคุณลักษณะนี้.

เพื่อทำงานนี้:

  1. เริ่มต้น ExchangeWebServiceClient สำหรับผู้ใช้ 1.
  2. เริ่มต้น ExchangeWebServiceClient สำหรับผู้ใช้ 2.
  3. เพิ่มข้อความทดสอบไปยังบัญชี.
  4. เปิดใช้งานการแสดงตน.
  5. รีเซ็ตการแสดงตน.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการใช้ EWSClient คลาสสำหรับทำฟีเจอร์การแสดงตน (Impersonation).

// Create instance's of EWSClient class by giving credentials
IEWSClient client1 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser1", "pwd", "domain");
IEWSClient client2 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser2", "pwd", "domain");
{
    string folder = "Drafts";
    try
    {
        foreach (ExchangeMessageInfo messageInfo in client1.ListMessages(folder))
            client1.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently);
        string subj1 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User1");
        client1.AppendMessage(folder, new MailMessage("User1@exchange.conholdate.local", "To@aspsoe.com", subj1, ""));

        foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder))
            client2.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently);
        string subj2 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User2");
        client2.AppendMessage(folder, new MailMessage("User2@exchange.conholdate.local", "To@aspose.com", subj2, ""));

        ExchangeMessageInfoCollection messInfoColl = client1.ListMessages(folder);
        //Assert.AreEqual(1, messInfoColl.Count);
        //Assert.AreEqual(subj1, messInfoColl[0].Subject);

        client1.ImpersonateUser(ItemChoice.PrimarySmtpAddress, "User2@exchange.conholdate.local");
        ExchangeMessageInfoCollection messInfoColl1 = client1.ListMessages(folder);
        //Assert.AreEqual(1, messInfoColl1.Count);
        //Assert.AreEqual(subj2, messInfoColl1[0].Subject);

        client1.ResetImpersonation();
        ExchangeMessageInfoCollection messInfoColl2 = client1.ListMessages(folder);
        //Assert.AreEqual(1, messInfoColl2.Count);
        //Assert.AreEqual(subj1, messInfoColl2[0].Subject);
    }
    finally
    {
        try
        {
            foreach (ExchangeMessageInfo messageInfo in client1.ListMessages(folder))
                client1.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently);
            foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder))
                client2.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently);
        }
        catch { }
    }
}

ฟีเจอร์ Auto Discover ด้วย EWS

Aspose.Email API ช่วยให้คุณค้นพบการตั้งค่า Exchange Server ด้วยการใช้ EWS Client 

string email = "asposeemail.test3@aspose.com";
string password = "Aspose@2017";
AutodiscoverService svc = new AutodiscoverService();
svc.Credentials = new NetworkCredential(email, password);

IDictionary<UserSettingName, object> userSettings = svc.GetUserSettings(email, UserSettingName.ExternalEwsUrl).Settings;
string ewsUrl = (string)userSettings[UserSettingName.ExternalEwsUrl];
Console.WriteLine("Auto discovered EWS Url: "  + ewsUrl);

ยกเลิกการกู้คืน PST ไปยัง Exchange Server

Aspose.Email API ช่วยให้คุณกู้คืนไฟล์ PST ไปยัง Exchange Server. อย่างไรก็ตาม หากการดำเนินการใช้เวลานานเนื่องจากไฟล์ PST มีขนาดใหญ่ อาจจำเป็นต้องระบุเกณฑ์สำหรับการยกเลิกการดำเนินการ. สามารถทำได้โดยใช้ API ตามที่แสดงในตัวอย่างโค้ดต่อไปนี้.

หมายเหตุ: ตัวอย่างต้องการให้เพิ่มคลาสต่อไปนี้ด้วย.


 public class CustomAbortRestoreException : Exception { }
using (IEWSClient client = EWSClient.GetEWSClient("https://exchange.office365.com/ews/exchange.asmx", "username", "password"))
{
    DateTime startTime = DateTime.Now;
    TimeSpan maxRestoreTime = TimeSpan.FromSeconds(15);
    int processedItems = 0;

    BeforeItemCallback callback = delegate
    {
        if (DateTime.Now >= startTime.Add(maxRestoreTime))
        {
            throw new CustomAbortRestoreException();
        }

        processedItems++;
    };

    try
    {
        //create a test pst and add some test messages to it
        var pst = PersonalStorage.Create(new MemoryStream(), FileFormatVersion.Unicode);
        var folder = pst.RootFolder.AddSubFolder("My test folder");
        for (int i = 0; i < 20; i++)
        {
            var message = new MapiMessage("from@gmail.com", "to@gmail.com", "subj", new string('a', 10000));
            folder.AddMessage(message);
        }

        //now restore the PST with callback
        client.Restore(pst, new Aspose.Email.Clients.Exchange.WebService.RestoreSettings
        {
            BeforeItemCallback = callback
        });
        Console.WriteLine("Success!");
    }
    catch (CustomAbortRestoreException)
    {
        Console.WriteLine($"Timeout! {processedItems}");
    }