public static void Run() { // ExStart:AccessCustomFolderUsingExchangeWebServiceClient // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Create ExchangeMailboxInfo, ExchangeMessageInfoCollection instance ExchangeMailboxInfo mailbox = client.GetMailboxInfo(); ExchangeMessageInfoCollection messages = null; ExchangeFolderInfo subfolderInfo = new ExchangeFolderInfo(); // Check if specified custom folder exisits and Get all the messages info from the target Uri client.FolderExists(mailbox.InboxUri, "TestInbox", out subfolderInfo); messages = client.FindMessages(subfolderInfo.Uri); // Parse all the messages info collection foreach (ExchangeMessageInfo info in messages) { string strMessageURI = info.UniqueUri; // now get the message details using FetchMessage() MailMessage msg = client.FetchMessage(strMessageURI); Console.WriteLine("Subject: " + msg.Subject); } // ExEnd:AccessCustomFolderUsingExchangeWebServiceClient }
public static void Run() { string dataDir = RunExamples.GetDataDir_Exchange(); try { // ExStart:SaveMessagesInMSGFormatExchangeEWS // 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); int count = 0; // Loop through the collection to get Message URI foreach (ExchangeMessageInfo msgInfo in msgCollection) { string strMessageURI = msgInfo.UniqueUri; // Now get the message details using FetchMessage() and Save message as Msg MailMessage message = client.FetchMessage(strMessageURI); message.Save(dataDir + (count++) + "_out.msg", SaveOptions.DefaultMsgUnicode); } // ExEnd:SaveMessagesInMSGFormatExchangeEWS } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Run() { try { // ExStart:FetchMessageUsingEWS // Create instance of ExchangeWebServiceClient 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) { String strMessageURI = msgInfo.UniqueUri; // Now get the message details using FetchMessage() MailMessage msg = client.FetchMessage(strMessageURI); foreach (Attachment att in msg.Attachments) { Console.WriteLine("Attachment Name: " + att.Name); } } // ExEnd:FetchMessageUsingEWS } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static void ListMessagesFromSubFolder(ExchangeFolderInfo publicFolder, IEWSClient client) { Console.WriteLine("Folder Name: " + publicFolder.DisplayName); ExchangeMessageInfoCollection msgInfoCollection = client.ListMessagesFromPublicFolder(publicFolder); foreach (ExchangeMessageInfo messageInfo in msgInfoCollection) { MailMessage msg = client.FetchMessage(messageInfo.UniqueUri); Console.WriteLine(msg.Subject); msg.Save(dataDir + msg.Subject + ".msg", SaveOptions.DefaultMsgUnicode); } // Call this method recursively for any subfolders if (publicFolder.ChildFolderCount > 0) { ExchangeFolderInfoCollection subfolders = client.ListSubFolders(publicFolder); foreach (ExchangeFolderInfo subfolder in subfolders) { ListMessagesFromSubFolder(subfolder, client); } } }
// Recursive method to get messages from folders and sub-folders private static void ListMessagesInFolder(IEWSClient client, ExchangeFolderInfo folderInfo, string rootFolder) { // Create the folder in disk (same name as on IMAP server) string currentFolder = rootFolder + "\\" + folderInfo.DisplayName; Directory.CreateDirectory(currentFolder); // List messages ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(folderInfo.Uri); Console.WriteLine("Listing messages...."); int i = 0; foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Get subject and other properties of the message Console.WriteLine("Subject: " + msgInfo.Subject); // Get rid of characters like ? and :, which should not be included in a file name string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " "); MailMessage msg = client.FetchMessage(msgInfo.UniqueUri); msg.Save(dataDir + "\\" + fileName + "-" + i + ".msg"); i++; } Console.WriteLine("============================\n"); try { // If this folder has sub-folders, call this method recursively to get messages ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderInfo.Uri); foreach (ExchangeFolderInfo subfolderInfo in folderInfoCollection) { ListMessagesInFolder(client, subfolderInfo, currentFolder); } } catch (Exception) { } }