public static void Run() { try { // ExStart:EnumeratMessagesWithPaginginEWS // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "UserName", "Password"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.GetMailboxInfo().InboxUri); int itemsPerPage = 5; List <PageInfo> pages = new List <PageInfo>(); PageInfo pagedMessageInfoCol = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); while (!pagedMessageInfoCol.LastPage) { pagedMessageInfoCol = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); } pagedMessageInfoCol = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); while (!pagedMessageInfoCol.LastPage) { client.ListMessages(client.MailboxInfo.InboxUri); } } catch (Exception ex) { Console.Write(ex.Message); } finally { } // ExEnd:EnumeratMessagesWithPaginginEWS }
public static void Run() { // ExStart:EnumeratMessagesWithPaginginEWS // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "*****@*****.**", "LoveAir1993"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.GetMailboxInfo().InboxUri); try { int itemsPerPage = 5; List <ExchangeMessageInfoCollection> pages = new List <ExchangeMessageInfoCollection>(); ExchangeMessageInfoCollection pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); while (!pagedMessageInfoCol.LastPage) { pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1); pages.Add(pagedMessageInfoCol); } pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage); while (!pagedMessageInfoCol.LastPage) { client.ListMessages(client.MailboxInfo.InboxUri, pagedMessageInfoCol, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1); } } finally { } // ExEnd:EnumeratMessagesWithPaginginEWS }
public static void Run() { // ExStart:CreateREAndFWMessages string dataDir = RunExamples.GetDataDir_Exchange(); const string mailboxUri = "https://exchange.domain.com/ews/Exchange.asmx"; const string domain = @""; const string username = @"username"; const string password = @"password"; NetworkCredential credential = new NetworkCredential(username, password, domain); IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential); try { MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**", "TestMailRefw - " + Guid.NewGuid().ToString(), "TestMailRefw Implement ability to create RE and FW messages from source MSG file"); client.Send(message); ExchangeMessageInfoCollection messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri); if (messageInfoCol.Count == 1) { Console.WriteLine("1 message in Inbox"); } else { Console.WriteLine("Error! No message in Inbox"); } MailMessage message1 = new MailMessage("*****@*****.**", "*****@*****.**", "TestMailRefw - " + Guid.NewGuid().ToString(), "TestMailRefw Implement ability to create RE and FW messages from source MSG file"); client.Send(message1); messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri); if (messageInfoCol.Count == 2) { Console.WriteLine("2 messages in Inbox"); } else { Console.WriteLine("Error! No new message in Inbox"); } MailMessage message2 = new MailMessage("*****@*****.**", "*****@*****.**", "TestMailRefw - " + Guid.NewGuid().ToString(), "TestMailRefw Implement ability to create RE and FW messages from source MSG file"); message2.Attachments.Add(Attachment.CreateAttachmentFromString("Test attachment 1", "Attachment Name 1")); message2.Attachments.Add(Attachment.CreateAttachmentFromString("Test attachment 2", "Attachment Name 2")); // Reply, Replay All and forward Message client.Reply(message2, messageInfoCol[0]); client.ReplyAll(message2, messageInfoCol[0]); client.Forward(message2, messageInfoCol[0]); } catch (Exception ex) { Console.WriteLine("Error in program" + ex.Message); } // ExEnd:CreateREAndFWMessages }
public static void Run() { // ExStart:ExchangeImpersonationUsingEWS // 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.DeleteMessage(messageInfo.UniqueUri); } string subj1 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User1"); client1.AppendMessage(folder, new MailMessage("*****@*****.**", "*****@*****.**", subj1, "")); foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder)) { client2.DeleteMessage(messageInfo.UniqueUri); } string subj2 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User2"); client2.AppendMessage(folder, new MailMessage("*****@*****.**", "*****@*****.**", subj2, "")); ExchangeMessageInfoCollection messInfoColl = client1.ListMessages(folder); //Assert.AreEqual(1, messInfoColl.Count); //Assert.AreEqual(subj1, messInfoColl[0].Subject); client1.ImpersonateUser(ItemChoice.PrimarySmtpAddress, "*****@*****.**"); 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.DeleteMessage(messageInfo.UniqueUri); } foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder)) { client2.DeleteMessage(messageInfo.UniqueUri); } } catch { } } } // ExEnd:ExchangeImpersonationUsingEWS }
public static void Run() { // ExStart:FilterTasksFromServer // Set mailboxURI, Username, password, domain information string mailboxUri = "https://ex2010/ews/exchange.asmx"; string username = "******"; string password = "******"; string domain = "ex2010.local"; NetworkCredential credentials = new NetworkCredential(username, password, domain); IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials); ExchangeQueryBuilder queryBuilder = null; MailQuery query = null; ExchangeTask fetchedTask = null; ExchangeMessageInfoCollection messageInfoCol = null; client.TimezoneId = "Central Europe Standard Time"; Array values = Enum.GetValues(typeof(ExchangeTaskStatus)); //Now retrieve the tasks with specific statuses foreach (ExchangeTaskStatus status in values) { queryBuilder = new ExchangeQueryBuilder(); queryBuilder.TaskStatus.Equals(status); query = queryBuilder.GetQuery(); messageInfoCol = client.ListMessages(client.MailboxInfo.TasksUri, query); fetchedTask = client.FetchTask(messageInfoCol[0].UniqueUri); } //retrieve all other than specified foreach (ExchangeTaskStatus status in values) { queryBuilder = new ExchangeQueryBuilder(); queryBuilder.TaskStatus.NotEquals(status); query = queryBuilder.GetQuery(); messageInfoCol = client.ListMessages(client.MailboxInfo.TasksUri, query); } //specifying multiple criterion ExchangeTaskStatus[] selectedStatuses = new ExchangeTaskStatus[] { ExchangeTaskStatus.Completed, ExchangeTaskStatus.InProgress }; queryBuilder = new ExchangeQueryBuilder(); queryBuilder.TaskStatus.In(selectedStatuses); query = queryBuilder.GetQuery(); messageInfoCol = client.ListMessages(client.MailboxInfo.TasksUri, query); queryBuilder = new ExchangeQueryBuilder(); queryBuilder.TaskStatus.NotIn(selectedStatuses); query = queryBuilder.GetQuery(); messageInfoCol = client.ListMessages(client.MailboxInfo.TasksUri, query); //ExEnd:FilterTasksFromServer }
public static void Run() { // Create and initialize credentials var credentials = new NetworkCredential("username", "12345"); // Create instance of ExchangeClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Get all tasks info collection from exchange ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri); // Parse all the tasks info in the list foreach (ExchangeMessageInfo info in tasks) { // Fetch task from exchange using current task info ExchangeTask task = client.FetchTask(info.UniqueUri); // Update the task status to NotStarted task.Status = ExchangeTaskStatus.NotStarted; // Set the task due date task.DueDate = new DateTime(2013, 2, 26); // Set task priority task.Priority = MailPriority.Low; // Update task on exchange client.UpdateTask(task); } Console.WriteLine(Environment.NewLine + "Task updated on Exchange Server successfully."); }
public static void Run() { // ExStart:MoveMessageFromOneFolderToAnotherusingEWS // Create instance of IEWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); // List all messages from Inbox folder Console.WriteLine("Listing all messages from Inbox...."); ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(mailboxInfo.InboxUri); foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Move message to "Processed" folder, after processing certain messages based on some criteria if (msgInfo.Subject != null && msgInfo.Subject.ToLower().Contains("process this message") == true) { client.MoveItem(mailboxInfo.DeletedItemsUri, msgInfo.UniqueUri); // EWS Console.WriteLine("Message moved...." + msgInfo.Subject); } else { // Do something else } } // ExEnd:MoveMessageFromOneFolderToAnotherusingEWS }
public static void Run() { try { // ExStart:SaveMessagesToMemoryStream string datadir = RunExamples.GetDataDir_Exchange(); // 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) { string strMessageURI = msgInfo.UniqueUri; // Now save the message in memory stream MemoryStream stream = new MemoryStream(); client.SaveMessage(strMessageURI, datadir + stream); } // ExEnd:SaveMessagesToMemoryStream } catch (Exception ex) { Console.Write(ex.Message); } }
public static void Run() { // ExStart:DeleteMessagesFromusingEWS // Create instance of IEWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); // List all messages from Inbox folder Console.WriteLine("Listing all messages from Inbox...."); ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(mailboxInfo.InboxUri); foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Delete message based on some criteria if (msgInfo.Subject != null && msgInfo.Subject.ToLower().Contains("delete") == true) { client.DeleteItem(msgInfo.UniqueUri, DeletionOptions.DeletePermanently); // EWS Console.WriteLine("Message deleted...." + msgInfo.Subject); } else { // Do something else } } // ExEnd:DeleteMessagesFromusingEWS }
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); } }
public static void Run() { try { // ExStart:GetExchangeMessageInfoFromMessageURI IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "*****@*****.**", "pwd", "domain"); List <string> ids = new List <string>(); List <MailMessage> messages = new List <MailMessage>(); for (int i = 0; i < 5; i++) { MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**", "EMAILNET-35033 - " + Guid.NewGuid().ToString(), "EMAILNET-35033 Messages saved from Sent Items folder doesn't contain 'To' field"); messages.Add(message); string uri = client.AppendMessage(message); ids.Add(uri); } ExchangeMessageInfoCollection messageInfoCol = client.ListMessages(ids); foreach (ExchangeMessageInfo messageInfo in messageInfoCol) { // Do something ... Console.WriteLine(messageInfo.UniqueUri); } // ExEnd:GetExchangeMessageInfoFromMessageURI } catch (Exception ex) { Console.WriteLine(ex.Message); } }
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); //if custom folder exists if (subfolderInfo != null) { messages = client.ListMessages(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); } } else { Console.WriteLine("No folder with this name found."); } // ExEnd:AccessCustomFolderUsingExchangeWebServiceClient }
public static void Run() { try { // ExStart:ListingMessagesUsingEWS // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "UserName", "Password"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri); // Loop through the collection to display the basic information foreach (ExchangeMessageInfo msgInfo in msgCollection) { Console.WriteLine("Subject: " + msgInfo.Subject); Console.WriteLine("From: " + msgInfo.From.ToString()); Console.WriteLine("To: " + msgInfo.To.ToString()); Console.WriteLine("Message ID: " + msgInfo.MessageId); Console.WriteLine("Unique URI: " + msgInfo.UniqueUri); } // ExEnd:ListingMessagesUsingEWS } catch (Exception ex) { Console.Write(ex.Message); } }
public static void Run() { // ExStart:SynchronizeFolderItems IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); MailMessage message1 = new MailMessage("*****@*****.**", "*****@*****.**", "EMAILNET-34738 - " + Guid.NewGuid().ToString(), "EMAILNET-34738 Sync Folder Items"); client.Send(message1); MailMessage message2 = new MailMessage("*****@*****.**", "*****@*****.**", "EMAILNET-34738 - " + Guid.NewGuid().ToString(), "EMAILNET-34738 Sync Folder Items"); client.Send(message2); ExchangeMessageInfoCollection messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri); SyncFolderResult result = client.SyncFolder(client.MailboxInfo.InboxUri, null); Console.WriteLine(result.NewItems.Count); Console.WriteLine(result.ChangedItems.Count); Console.WriteLine(result.ReadFlagChanged.Count); Console.WriteLine(result.DeletedItems.Length); // ExEnd:SynchronizeFolderItems }
public static void Run() { // Connect to EWS const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx"; const string username = "******"; const string password = "******"; const string domain = "domain"; try { IEWSClient client = EWSClient.GetEWSClient(mailboxUri, username, password, domain); // ExStart:CaseSensitiveEmailsFiltering // Query building by means of ExchangeQueryBuilder class ExchangeQueryBuilder builder = new ExchangeQueryBuilder(); builder.Subject.Contains("Newsletter", true); builder.InternalDate.On(DateTime.Now); MailQuery query = builder.GetQuery(); // ExEnd:CaseSensitiveEmailsFiltering // Get list of messages ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query, false); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); // Disconnect from EWS client.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Run() { ///<summary> /// This example shows how to list messages from Exchange Server with Paging support /// Introduced in Aspose.Email for .NET 6.4.0 /// ExchangeMessagePageInfo ListMessagesByPage(string folder, int itemsPerPage); /// ExchangeMessagePageInfo ListMessagesByPage(string folder, int itemsPerPage, int offset); /// ExchangeMessagePageInfo ListMessagesByPage(string folder, int itemsPerPage, int pageOffset, ExchangeListMessagesOptions options); /// ExchangeMessagePageInfo ListMessagesByPage(string folder, PageInfo pageInfo); /// ExchangeMessagePageInfo ListMessagesByPage(string folder, PageInfo pageInfo, ExchangeListMessagesOptions options); ///</summary> // ExStart: PagingSupportForListingMessages using (IEWSClient client = EWSClient.GetEWSClient("exchange.domain.com", "username", "password")) { try { // Create some test messages to be added to server for retrieval later int messagesNum = 12; int itemsPerPage = 5; MailMessage message = null; for (int i = 0; i < messagesNum; i++) { message = new MailMessage( "*****@*****.**", "*****@*****.**", "EMAILNET-35157_1 - " + Guid.NewGuid().ToString(), "EMAILNET-35157 Move paging parameters to separate class"); client.AppendMessage(client.MailboxInfo.InboxUri, message); } // Verfiy that the messages have been added to the server ExchangeMessageInfoCollection totalMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri); Console.WriteLine(totalMessageInfoCol.Count); ////////////////// RETREIVING THE MESSAGES USING PAGING SUPPORT //////////////////////////////////// List <ExchangeMessagePageInfo> pages = new List <ExchangeMessagePageInfo>(); ExchangeMessagePageInfo pageInfo = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); // Total Pages Count Console.WriteLine(pageInfo.TotalCount); pages.Add(pageInfo); while (!pageInfo.LastPage) { pageInfo = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage, pageInfo.PageOffset + 1); pages.Add(pageInfo); } int retrievedItems = 0; foreach (ExchangeMessagePageInfo pageCol in pages) { retrievedItems += pageCol.Items.Count; } // Verify total message count using paging Console.WriteLine(retrievedItems); } finally { } } // ExEnd: PagingSupportForListingMessages }
public static void Run() { // ExStart:AddingHeadersToEWSRequests using (IEWSClient client = EWSClient.GetEWSClient("exchange.domain.com/ews/Exchange.asmx", "username", "password", "")) { client.AddHeader("X-AnchorMailbox", "*****@*****.**"); ExchangeMessageInfoCollection messageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri); } // ExEnd:AddingHeadersToEWSRequests }
public static void Run() { // Connect to EWS const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx"; const string username = "******"; const string password = "******"; const string domain = "domain"; try { IEWSClient client = EWSClient.GetEWSClient(mailboxUri, username, password, domain); MailQueryBuilder builder = new MailQueryBuilder(); // ExStart:CombineQueriesWithAND // Emails from specific host, get all emails that arrived before today and all emails that arrived since 7 days ago builder.From.Contains("SpecificHost.com"); builder.InternalDate.Before(DateTime.Now); builder.InternalDate.Since(DateTime.Now.AddDays(-7)); // ExEnd:CombineQueriesWithAND // Build the query and Get list of messages MailQuery query = builder.GetQuery(); ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); builder = new MailQueryBuilder(); // ExStart:CombiningQueriesWithOR builder.Or(builder.Subject.Contains("test"), builder.From.Contains("*****@*****.**")); // ExEnd:CombiningQueriesWithOR // Build the query and Get list of messages query = builder.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Run() { try { IEWSClient client = GetExchangeEWSClient(); Console.WriteLine(client.ListMessages("Inbox").Count); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Run() { //ExStart: GetMessageTypeFromExchangeMessageInfo const string mailboxUri = "https://exchange/ews/exchange.asmx"; const string domain = @""; const string username = @"*****@*****.**"; const string password = @"password"; NetworkCredential credentials = new NetworkCredential(username, password, domain); // ExStart:CopyConversations IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials); ExchangeMessageInfoCollection list = client.ListMessages(client.MailboxInfo.DeletedItemsUri); Console.WriteLine(list[0].MessageInfoType.ToString()); //ExEnd: GetMessageTypeFromExchangeMessageInfo }
public static void Run() { // ExStart:ExchangeServerUsingEWS // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Get folder URI string strFolderURI = string.Empty; strFolderURI = client.MailboxInfo.InboxUri; strFolderURI = client.MailboxInfo.DeletedItemsUri; strFolderURI = client.MailboxInfo.DraftsUri; strFolderURI = client.MailboxInfo.SentItemsUri; // Get list of messages from the specified folder ExchangeMessageInfoCollection msgCollection = client.ListMessages(strFolderURI); // ExEnd:ExchangeServerUsingEWS }
public static void Run() { // ExStart:PreFetchMessageSizeUsingIEWSClient // 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 display the basic information foreach (ExchangeMessageInfo msgInfo in msgCollection) { Console.WriteLine("Subject: " + msgInfo.Subject); Console.WriteLine("From: " + msgInfo.From.ToString()); Console.WriteLine("To: " + msgInfo.To.ToString()); Console.WriteLine("Message Size: " + msgInfo.Size); Console.WriteLine("=================================="); } // ExEnd:PreFetchMessageSizeUsingIEWSClient }
public static void Run() { //ExStart: MoveItemsToInPlaceArchive const string mailboxUri = "<HOST>"; const string domain = @""; const string username = @"<USERNAME>"; const string password = @"<PASSWORD>"; NetworkCredential credentials = new NetworkCredential(username, password, domain); IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials); ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri); foreach (ExchangeMessageInfo msgInfo in msgCollection) { Console.WriteLine("Subject:" + msgInfo.Subject); client.ArchiveItem(client.MailboxInfo.InboxUri, msgInfo.UniqueUri); } client.Dispose(); //ExEnd: MoveItemsToInPlaceArchive }
public static void Run() { // ExStart:SaveMessagesUsingExchangeWebServices string dataDir = RunExamples.GetDataDir_Exchange(); // Create instance of IEWSClient 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 save the message in disk client.SaveMessage(strMessageURI, dataDir + msgInfo.MessageId + "out.eml"); } // ExEnd:SaveMessagesUsingExchangeWebServices }
public static void Run() { // ExStart:ProgrammingSamplesUsingEWS // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "*****@*****.**", "LoveAir1993"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri); // Loop through the collection to display the basic information foreach (ExchangeMessageInfo msgInfo in msgCollection) { Console.WriteLine("Subject: " + msgInfo.Subject); Console.WriteLine("From: " + msgInfo.From.ToString()); Console.WriteLine("To: " + msgInfo.To.ToString()); Console.WriteLine("Message ID: " + msgInfo.MessageId); Console.WriteLine("Unique URI: " + msgInfo.UniqueUri); Console.WriteLine("=================================="); } // ExEnd:ProgrammingSamplesUsingEWS }
// 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) { } }
// 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) { } }
public static void Run() { // ExStart:DeleteTaskOnExchange // Create instance of ExchangeClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Get all tasks info collection from exchange ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri); // Parse all the tasks info in the list foreach (ExchangeMessageInfo info in tasks) { // Fetch task from exchange using current task info ExchangeTask task = client.FetchTask(info.UniqueUri); // Check if the current task fulfills the search criteria if (task.Subject.Equals("test")) { //Delete task from exchange client.DeleteItem(task.UniqueUri, DeletionOptions.DeletePermanently); } } // ExEnd:DeleteTaskOnExchange }
public static void Run() { // Connect to EWS const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx"; const string username = "******"; const string password = "******"; const string domain = "domain"; try { IEWSClient client = EWSClient.GetEWSClient(mailboxUri, username, password, domain); // ExStart:GetEmailsWithTodayDate // Emails that arrived today MailQueryBuilder builder = new MailQueryBuilder(); builder.InternalDate.On(DateTime.Now); // ExEnd:GetEmailsWithTodayDate // Build the query and Get list of messages MailQuery query = builder.GetQuery(); ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); builder = new MailQueryBuilder(); // ExStart:GetEmailsOverDateRange // Emails that arrived in last 7 days builder.InternalDate.Before(DateTime.Now); builder.InternalDate.Since(DateTime.Now.AddDays(-7)); // ExEnd:GetEmailsOverDateRange // Build the query and Get list of messages query = builder.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); builder = new MailQueryBuilder(); // ExStart:GetSpecificSenderEmails // Get emails from specific sender builder.From.Contains("[email protected]"); // ExEnd:GetSpecificSenderEmails // Build the query and Get list of messages query = builder.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); builder = new MailQueryBuilder(); // ExStart:GetSpecificDomainEmails // Get emails from specific domain builder.From.Contains("SpecificHost.com"); // ExEnd:GetSpecificDomainEmails // Build the query and Get list of messages query = builder.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); builder = new MailQueryBuilder(); // ExStart:GetSpecificRecipientEmails // Get emails sent to specific recipient builder.To.Contains("recipient"); // ExEnd:GetSpecificRecipientEmails // Build the query and Get list of messages query = builder.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); // ExStart:GetSpecificMessageIdEmail // Get email with specific MessageId ExchangeQueryBuilder builder1 = new ExchangeQueryBuilder(); builder1.MessageId.Equals("MessageID"); // ExEnd:GetSpecificMessageIdEmail // Build the query and Get list of messages query = builder1.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); // ExStart:GetMailDeliveryNotifications // Get Mail Delivery Notifications builder1 = new ExchangeQueryBuilder(); builder1.ContentClass.Equals(ContentClassType.MDN.ToString()); // ExEnd:GetMailDeliveryNotifications //ExStart: FilterMessagesByMessageSize builder1 = new ExchangeQueryBuilder(); builder1.ItemSize.Greater(80000); //ExEnd: FilterMessagesByMessageSize // Build the query and Get list of messages query = builder1.GetQuery(); messages = client.ListMessages(client.MailboxInfo.InboxUri, query); Console.WriteLine("EWS: " + messages.Count + " message(s) found."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }