Utility Features in Aspose.Email for .NET

Working with Unified Messaging

Aspose.Email can retrieve unified messaging information from Exchange Server 2010. Unified messaging such as getting configuration information, initiating an outbound call, retrieving phone call information by call ID and disconnecting a phone call by ID is supported at present. The following code sample shows how to retrieve unified messaging configuration information from Microsoft Exchange Server 2010.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential);
UnifiedMessagingConfiguration umConf = client.GetUMConfiguration();

Getting Mail Tips

Microsoft Exchange Server added several new features with Exchange Server 2010 and 2013. One of them lets users get mail tips when composing an email message. These tips are very useful as they provide information before the email is sent. For example, if an email address is wrong in the recipient list, a tip is displayed to let you know that the email address is invalid. Mail tips also let you see out of office replies before sending an email: Exchange Server (2010 & 2013) sends the mail tip when the email is being composed if one or more of the recipients have set out of office replies. Microsoft Exchange Server 2010 Service Pack 1 is required for all the features demonstrated in this article. The following code snippet shows you how to use the EWSClient class which uses Exchange Web Services, available in Microsoft Exchange Server 2007 and later versions.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// 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 impersonation allows someone to impersonate another account and perform tasks and operations using the impersonated account permissions instead of their own. Where delegation lets users act on behalf of other users, Impersonation allows them to act as other users. Aspose.Email supports Exchange Impersonation. The EWSClient class provides the ImpersonateUser and ResetImpersonation methods to facilitate this feature.

To perform this task:

  1. Initialize the ExchangeWebServiceClient for user 1.
  2. Initialize the ExchangeWebServiceClient for user 2.
  3. Append test messages to the accounts.
  4. Enable Impersonation.
  5. Reset Impersonation.

The following code snippet shows you how to use the EWSClient class to implement the Impersonation feature.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// 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 Feature using EWS

Aspose.Email API lets you discover about the Exchange Server settings using the EWS Client. 

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);

Abort PST Restore to Exchange Server Operation

Aspose.Email API lets you restore a PST file to Exchange Server. However, if the operation is taking long due to large size PST file, it may be required to specify criterion for aborting the operation. This can be achieved using the API as shown in the following sample code.

Note: The example needs the following class to be added as well.


 public class CustomAbortRestoreException : Exception { }

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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}");
}