Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 30, 2022 04:46
Move Email to a Folder in Microsoft Exchange Server using C#
try
{
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Call ListMessages method to list messages info from Inbox
ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri);
// Loop through the collection to get Message URI
foreach (ExchangeMessageInfo msgInfo in msgCollection)
{
if (msgInfo.From.Address.Contains("jhon.vick"))
{
String strMessageURI = msgInfo.UniqueUri;
// Copy to particular folder
string newMessageUri = client.CopyItem(strMessageURI, client.MailboxInfo.DeletedItemsUri);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Create messge
MailMessage message = new MailMessage("from@domain.com", "to@domain.com", "EMAILNET-34997 - " + Guid.NewGuid().ToString(), "EMAILNET-34997 Exchange: Copy a message and get reference to the new Copy item");
// Get message URI
string messageUri = client.AppendMessage(message);
// Copy message
string newMessageUri = client.CopyItem(messageUri, client.MailboxInfo.OutboxUri);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment