Aspose.Email for .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 模拟
Exchange 冒充允许某人冒充另一个账户,并使用被冒充账户的权限执行任务和操作,而不是使用自己的权限。委派让用户代表其他用户操作,冒充则让他们以其他用户的身份操作。Aspose.Email 支持 Exchange 冒充。该 EWSClient class 提供了 ImpersonateUser 和 ResetImpersonation 方法以实现此功能。
执行此任务:
- 为用户 1 初始化 ExchangeWebServiceClient。
- 为用户 2 初始化 ExchangeWebServiceClient。
- 向账号追加测试消息。
- 启用模拟。
- 重置模拟。
下面的代码片段展示了如何使用 EWSClient 用于实现模拟功能的类。
// 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 { }
}
}
使用 EWS 的自动发现功能
Aspose.Email API 让您使用 EWS 客户端发现 Exchange 服务器的设置。
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 服务器的操作
Aspose.Email API 允许将 PST 文件恢复到 Exchange 服务器。但是,如果由于 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}");
}