From c6bea13983e2adefd835612e14856886ee825f28 Mon Sep 17 00:00:00 2001 From: agent-aspose-email-examples Date: Fri, 3 Apr 2026 00:21:44 -0400 Subject: [PATCH 1/6] Add sample implement-message-filtering-through-webdav-to-retrieve-only-emails-that-satisfy-defined-search-criteria-and-properties.cs --- ...-defined-search-criteria-and-properties.cs | 93 +++++++++++-------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/working-with-exchange-webdav-client/implement-message-filtering-through-webdav-to-retrieve-only-emails-that-satisfy-defined-search-criteria-and-properties.cs b/working-with-exchange-webdav-client/implement-message-filtering-through-webdav-to-retrieve-only-emails-that-satisfy-defined-search-criteria-and-properties.cs index 48147329..9c933835 100644 --- a/working-with-exchange-webdav-client/implement-message-filtering-through-webdav-to-retrieve-only-emails-that-satisfy-defined-search-criteria-and-properties.cs +++ b/working-with-exchange-webdav-client/implement-message-filtering-through-webdav-to-retrieve-only-emails-that-satisfy-defined-search-criteria-and-properties.cs @@ -1,66 +1,77 @@ using Aspose.Email.Clients.Exchange; using System; -using System.Net; +using System.IO; using Aspose.Email; using Aspose.Email.Clients.Exchange.Dav; -using Aspose.Email.Tools.Search; -namespace AsposeEmailWebDavFilterExample +class Program { - class Program + static void Main() { - static void Main() + try { - try - { - // Placeholder connection settings – replace with real values. - string serverUri = "https://exchange.example.com/EWS/Exchange.asmx"; - string username = "username"; - string password = "password"; + // Placeholder connection settings + string mailboxUri = "https://exchange.example.com/ews/Exchange.asmx"; + string username = "username"; + string password = "password"; - // Guard against executing with placeholder credentials. - if (serverUri.Contains("example.com") || username == "username" || password == "password") - { - Console.Error.WriteLine("Placeholder credentials detected. Skipping execution."); - return; - } + // Skip execution if placeholder credentials are detected + if (mailboxUri.Contains("example.com") || username == "username") + { + Console.Error.WriteLine("Placeholder credentials detected. Skipping execution."); + return; + } - // Create and connect the Exchange WebDAV client. + // Create and use the Exchange client + using (ExchangeClient client = new ExchangeClient(mailboxUri, username, password)) + { try { - using (ExchangeClient client = new ExchangeClient(serverUri, username, password)) + // Retrieve messages from the Inbox folder that match the query. + string query = "HasAttachment = True AND IsRead = False"; + ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query); + foreach (var msgInfo in messages) { - // Build a query to retrieve only unread messages that have attachments. - // The query language follows the Exchange MailQuery syntax. - MailQuery query = new MailQuery("HasAttachment = True AND IsRead = False"); + // Fetch the full message + using (MailMessage message = client.FetchMessage(msgInfo.UniqueUri)) + { + Console.WriteLine($"Subject: {message.Subject}"); - // List messages from the Inbox that match the query (recursive = false). - ExchangeMessageInfoCollection messages = client.ListMessages( - client.MailboxInfo.InboxUri, - query, - false); + // Prepare output directory and file path + string outputDir = "Output"; + string outputPath = Path.Combine(outputDir, "FirstMessage.eml"); - // Process the filtered messages. - foreach (ExchangeMessageInfo info in messages) - { - Console.WriteLine("Subject: " + info.Subject); - Console.WriteLine("Has Attachments: " + info.HasAttachments); - Console.WriteLine("Received: " + info.InternalDate); - Console.WriteLine(new string('-', 40)); + // Ensure the output directory exists + if (!Directory.Exists(outputDir)) + { + Directory.CreateDirectory(outputDir); + } + + // Save the message to a file with error handling + try + { + message.Save(outputPath); + Console.WriteLine($"Message saved to {outputPath}"); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to save message: {ex.Message}"); + } } + + // Process only the first message + break; } } catch (Exception ex) { - Console.Error.WriteLine("Error during client operation: " + ex.Message); - return; + Console.Error.WriteLine($"Error during Exchange operations: {ex.Message}"); } } - catch (Exception ex) - { - Console.Error.WriteLine("Unhandled exception: " + ex.Message); - return; - } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Unhandled exception: {ex.Message}"); } } } From de914a6741eb05fbbae22cbcd3c36bc3dfacb9e4 Mon Sep 17 00:00:00 2001 From: agent-aspose-email-examples Date: Fri, 3 Apr 2026 00:21:45 -0400 Subject: [PATCH 2/6] Add sample assign-a-high-priority-to-a-msg-email-programmatically-by-setting-mailmessage-priority-to-mailpriority-high.cs --- ...ilmessage-priority-to-mailpriority-high.cs | 38 ++----------------- 1 file changed, 4 insertions(+), 34 deletions(-) diff --git a/working-with-mime-messages/assign-a-high-priority-to-a-msg-email-programmatically-by-setting-mailmessage-priority-to-mailpriority-high.cs b/working-with-mime-messages/assign-a-high-priority-to-a-msg-email-programmatically-by-setting-mailmessage-priority-to-mailpriority-high.cs index 96b41030..2bbb8876 100644 --- a/working-with-mime-messages/assign-a-high-priority-to-a-msg-email-programmatically-by-setting-mailmessage-priority-to-mailpriority-high.cs +++ b/working-with-mime-messages/assign-a-high-priority-to-a-msg-email-programmatically-by-setting-mailmessage-priority-to-mailpriority-high.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using Aspose.Email; class Program @@ -8,44 +7,15 @@ static void Main() { try { - string emlPath = "Message.eml"; - string msgPath = "outTest_out.msg"; - - // Ensure the input EML file exists; create a minimal placeholder if missing. - if (!File.Exists(emlPath)) - { - try - { - string placeholder = "From: test@example.com\r\nTo: test@example.com\r\nSubject: Test\r\n\r\nBody of the email."; - File.WriteAllText(emlPath, placeholder); - } - catch (Exception ex) - { - Console.Error.WriteLine($"Failed to create placeholder EML file: {ex.Message}"); - return; - } - } - - // Load the email, set high priority, and save as MSG. - try - { - using (MailMessage mailMessage = MailMessage.Load(emlPath)) - { - mailMessage.Priority = MailPriority.High; - - MsgSaveOptions saveOptions = new MsgSaveOptions(MailMessageSaveType.OutlookMessageFormatUnicode); - mailMessage.Save(msgPath, saveOptions); - } - } - catch (Exception ex) + using (MailMessage mailMessage = new MailMessage()) { - Console.Error.WriteLine($"Error processing email files: {ex.Message}"); - return; + mailMessage.Priority = MailPriority.High; + Console.WriteLine($"Priority set to: {mailMessage.Priority}"); } } catch (Exception ex) { - Console.Error.WriteLine($"Unexpected error: {ex.Message}"); + Console.Error.WriteLine(ex.Message); } } } From 55c294523cdc236fb8004f16960b8312756972c2 Mon Sep 17 00:00:00 2001 From: agent-aspose-email-examples Date: Fri, 3 Apr 2026 00:21:45 -0400 Subject: [PATCH 3/6] Add sample convert-tnef-attachment-files-into-msg-format-preserving-attachment-metadata-and-content-integrity-for-further-processing.cs --- ...ontent-integrity-for-further-processing.cs | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/working-with-mime-messages/convert-tnef-attachment-files-into-msg-format-preserving-attachment-metadata-and-content-integrity-for-further-processing.cs b/working-with-mime-messages/convert-tnef-attachment-files-into-msg-format-preserving-attachment-metadata-and-content-integrity-for-further-processing.cs index 1ac9e895..96564cc9 100644 --- a/working-with-mime-messages/convert-tnef-attachment-files-into-msg-format-preserving-attachment-metadata-and-content-integrity-for-further-processing.cs +++ b/working-with-mime-messages/convert-tnef-attachment-files-into-msg-format-preserving-attachment-metadata-and-content-integrity-for-further-processing.cs @@ -5,34 +5,49 @@ class Program { - static void Main() + static void Main(string[] args) { try { - // Define input TNEF file and output MSG file paths - string inputPath = "input.tnef"; - string outputPath = "output.msg"; + // Define input TNEF file (e.g., winmail.dat) and output MSG file paths + string inputTnefPath = "input.winmail.dat"; + string outputMsgPath = "output.msg"; // Verify that the input TNEF file exists - if (!File.Exists(inputPath)) + if (!File.Exists(inputTnefPath)) { - Console.Error.WriteLine($"Error: File not found – {inputPath}"); + Console.Error.WriteLine($"Error: Input TNEF file not found – {inputTnefPath}"); return; } - // Load the TNEF file into a MapiMessage and save it as MSG - try + // Ensure the output directory exists + string outputDirectory = Path.GetDirectoryName(outputMsgPath); + if (!string.IsNullOrEmpty(outputDirectory) && !Directory.Exists(outputDirectory)) { - using (MapiMessage message = MapiMessage.LoadFromTnef(inputPath)) + try { - // Save as MSG using the default MSG save options - message.Save(outputPath, SaveOptions.DefaultMsg); + Directory.CreateDirectory(outputDirectory); + } + catch (Exception dirEx) + { + Console.Error.WriteLine($"Error: Unable to create output directory – {outputDirectory}. {dirEx.Message}"); + return; } } - catch (Exception ex) + + // Load the TNEF file into a MapiMessage + using (MapiMessage message = MapiMessage.LoadFromTnef(inputTnefPath)) { - Console.Error.WriteLine($"Error processing TNEF file: {ex.Message}"); - return; + // Save the message as MSG, preserving all attachments and metadata + try + { + message.Save(outputMsgPath); + Console.WriteLine($"Successfully converted TNEF to MSG: {outputMsgPath}"); + } + catch (Exception saveEx) + { + Console.Error.WriteLine($"Error: Failed to save MSG file – {saveEx.Message}"); + } } } catch (Exception ex) From d7ba9468d8ea1d8685dd1b0962da58e97d85cb9e Mon Sep 17 00:00:00 2001 From: agent-aspose-email-examples Date: Fri, 3 Apr 2026 00:21:45 -0400 Subject: [PATCH 4/6] Add sample extract-an-embedded-image-from-an-email-via-linkedresource-contentstream-and-write-it-to-a-msg-file.cs --- ...ontentstream-and-write-it-to-a-msg-file.cs | 113 +++++++++++++----- 1 file changed, 81 insertions(+), 32 deletions(-) diff --git a/working-with-mime-messages/extract-an-embedded-image-from-an-email-via-linkedresource-contentstream-and-write-it-to-a-msg-file.cs b/working-with-mime-messages/extract-an-embedded-image-from-an-email-via-linkedresource-contentstream-and-write-it-to-a-msg-file.cs index 702ef2b6..afb72ded 100644 --- a/working-with-mime-messages/extract-an-embedded-image-from-an-email-via-linkedresource-contentstream-and-write-it-to-a-msg-file.cs +++ b/working-with-mime-messages/extract-an-embedded-image-from-an-email-via-linkedresource-contentstream-and-write-it-to-a-msg-file.cs @@ -1,7 +1,7 @@ using System; using System.IO; using Aspose.Email; -using Aspose.Email.Mime; +using Aspose.Email.Mapi; class Program { @@ -9,51 +9,100 @@ static void Main() { try { - string inputPath = "sample.eml"; - if (!File.Exists(inputPath)) + // Input EML file containing embedded images + string inputEmlPath = "input.eml"; + // Output MSG file + string outputMsgPath = "output.msg"; + // Path to save the extracted image + string extractedImagePath = "extracted_image.jpg"; + + // Guard input file existence + if (!File.Exists(inputEmlPath)) { - Console.Error.WriteLine($"Error: File not found – {inputPath}"); + try + { + using (MailMessage placeholder = new MailMessage( + "sender@example.com", + "recipient@example.com", + "Placeholder Subject", + "Placeholder body.")) + { + placeholder.Save(inputEmlPath, SaveOptions.DefaultEml); + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Error creating placeholder message: {ex.Message}"); + return; + } + + Console.Error.WriteLine($"Input file not found: {inputEmlPath}"); return; } - string outputPath = "extracted_image.msg"; + // Ensure output directory exists + string outputDirectory = Path.GetDirectoryName(outputMsgPath); + if (!string.IsNullOrEmpty(outputDirectory) && !Directory.Exists(outputDirectory)) + { + try + { + Directory.CreateDirectory(outputDirectory); + } + catch (Exception dirEx) + { + Console.Error.WriteLine($"Failed to create output directory: {dirEx.Message}"); + return; + } + } - try + // Load the email message + using (MailMessage mailMessage = MailMessage.Load(inputEmlPath)) { - using (MailMessage originalMessage = MailMessage.Load(inputPath)) + // Extract the first linked resource (embedded image) if any + if (mailMessage.LinkedResources.Count > 0) { - if (originalMessage.LinkedResources.Count == 0) + // Assuming the first linked resource is the image we want + LinkedResource linkedResource = mailMessage.LinkedResources[0]; + using (Stream contentStream = linkedResource.ContentStream) { - Console.Error.WriteLine("No linked resources found in the email."); - return; + if (contentStream != null) + { + try + { + using (FileStream fileStream = new FileStream(extractedImagePath, FileMode.Create, FileAccess.Write)) + { + contentStream.CopyTo(fileStream); + } + Console.WriteLine($"Extracted image saved to: {extractedImagePath}"); + } + catch (Exception ioEx) + { + Console.Error.WriteLine($"Failed to write extracted image: {ioEx.Message}"); + // Continue without aborting; the MSG will still be saved + } + } } + } + else + { + Console.WriteLine("No linked resources (embedded images) found in the email."); + } - LinkedResource linked = originalMessage.LinkedResources[0]; - - using (MemoryStream imageStream = new MemoryStream()) + // Convert the MailMessage to a MapiMessage (preserves embedded resources) + using (MapiMessage mapiMessage = MapiMessage.FromMailMessage(mailMessage)) + { + // Save as MSG file + try { - linked.ContentStream.CopyTo(imageStream); - imageStream.Position = 0; - - using (MailMessage extractedMessage = new MailMessage()) - { - extractedMessage.From = "extracted@example.com"; - extractedMessage.To = "extracted@example.com"; - extractedMessage.Subject = "Extracted Image"; - - Attachment attachment = new Attachment(imageStream, linked.ContentType); - extractedMessage.Attachments.Add(attachment); - - extractedMessage.Save(outputPath, SaveOptions.DefaultMsgUnicode); - } + mapiMessage.Save(outputMsgPath); + Console.WriteLine($"MSG file saved to: {outputMsgPath}"); + } + catch (Exception saveEx) + { + Console.Error.WriteLine($"Failed to save MSG file: {saveEx.Message}"); } } } - catch (Exception ex) - { - Console.Error.WriteLine($"Error processing email: {ex.Message}"); - return; - } } catch (Exception ex) { From 44605666a07519b6133cd1c44ac18eca280c3f76 Mon Sep 17 00:00:00 2001 From: agent-aspose-email-examples Date: Fri, 3 Apr 2026 00:21:45 -0400 Subject: [PATCH 5/6] Add sample parse-an-msg-file-and-retrieve-its-subject-sender-and-recipient-values-from-the-message-headers.cs --- ...cipient-values-from-the-message-headers.cs | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/working-with-mime-messages/parse-an-msg-file-and-retrieve-its-subject-sender-and-recipient-values-from-the-message-headers.cs b/working-with-mime-messages/parse-an-msg-file-and-retrieve-its-subject-sender-and-recipient-values-from-the-message-headers.cs index 180087c6..2f55efc4 100644 --- a/working-with-mime-messages/parse-an-msg-file-and-retrieve-its-subject-sender-and-recipient-values-from-the-message-headers.cs +++ b/working-with-mime-messages/parse-an-msg-file-and-retrieve-its-subject-sender-and-recipient-values-from-the-message-headers.cs @@ -1,7 +1,7 @@ using System; using System.IO; using Aspose.Email; -using Aspose.Email.Mapi; +using System.Collections.Specialized; class Program { @@ -9,46 +9,52 @@ static void Main(string[] args) { try { - // Path to the MSG file - string msgPath = "message.msg"; + string msgPath = "sample.msg"; - // Verify that the file exists before attempting to load it if (!File.Exists(msgPath)) { + try + { + using (MailMessage placeholder = new MailMessage( + "sender@example.com", + "recipient@example.com", + "Placeholder Subject", + "Placeholder body.")) + { + placeholder.Save(msgPath, new MsgSaveOptions(MailMessageSaveType.OutlookMessageFormat)); + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Error creating placeholder MSG: {ex.Message}"); + return; + } + Console.Error.WriteLine($"File not found: {msgPath}"); return; } - // Load the MSG file inside a using block to ensure proper disposal - using (MapiMessage msg = MapiMessage.Load(msgPath)) + using (MailMessage mail = MailMessage.Load(msgPath)) { - // Retrieve the subject - string subject = msg.Subject ?? string.Empty; - Console.WriteLine("Subject: " + subject); + // Retrieve specific header values + string subject = mail.Headers["Subject"]; + string from = mail.Headers["From"]; + string to = mail.Headers["To"]; - // Retrieve the sender information - string senderName = msg.SenderName ?? string.Empty; - string senderEmail = msg.SenderEmailAddress ?? string.Empty; - string sender = string.IsNullOrEmpty(senderEmail) ? senderName : $"{senderName} <{senderEmail}>"; - Console.WriteLine("From: " + sender); + Console.WriteLine($"Subject: {subject}"); + Console.WriteLine($"From: {from}"); + Console.WriteLine($"To: {to}"); - // Retrieve recipient information from the Recipients collection - if (msg.Recipients != null) + // Iterate all headers + foreach (string key in mail.Headers.Keys) { - foreach (MapiRecipient recipient in msg.Recipients) - { - string recipientName = recipient.DisplayName ?? string.Empty; - string recipientEmail = recipient.EmailAddress ?? string.Empty; - string formattedRecipient = string.IsNullOrEmpty(recipientEmail) ? recipientName : $"{recipientName} <{recipientEmail}>"; - Console.WriteLine("To: " + formattedRecipient); - } + Console.WriteLine($"{key}: {mail.Headers[key]}"); } } } catch (Exception ex) { - // Output any unexpected errors - Console.Error.WriteLine(ex.Message); + Console.Error.WriteLine($"Error: {ex.Message}"); } } } From f7c064f03389317e6fdcb613a5e606b7d7061181 Mon Sep 17 00:00:00 2001 From: agent-aspose-email-examples Date: Fri, 3 Apr 2026 00:21:45 -0400 Subject: [PATCH 6/6] Add sample set-the-msg-email-message-s-priority-flag-to-high-before-initiating-the-send-operation.cs --- ...gh-before-initiating-the-send-operation.cs | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/working-with-mime-messages/set-the-msg-email-message-s-priority-flag-to-high-before-initiating-the-send-operation.cs b/working-with-mime-messages/set-the-msg-email-message-s-priority-flag-to-high-before-initiating-the-send-operation.cs index a7ce93fb..84fa78a0 100644 --- a/working-with-mime-messages/set-the-msg-email-message-s-priority-flag-to-high-before-initiating-the-send-operation.cs +++ b/working-with-mime-messages/set-the-msg-email-message-s-priority-flag-to-high-before-initiating-the-send-operation.cs @@ -1,6 +1,6 @@ using System; using Aspose.Email; -using Aspose.Email.Clients.Smtp; +using Aspose.Email.Clients.Exchange.Dav; class Program { @@ -8,30 +8,40 @@ static void Main() { try { - // Create a mail message and set its priority to High - using (MailMessage message = new MailMessage("sender@example.com", "recipient@example.com", "Test Subject", "Test Body")) + // Placeholder credentials detection – skip actual send when placeholders are used + string mailboxUri = "https://exchange.example.com/ews/Exchange.asmx"; + string username = "username"; + string password = "password"; + + if (mailboxUri.Contains("example.com") || username == "username") { - message.Priority = MailPriority.High; + Console.Error.WriteLine("Placeholder credentials detected. Skipping send operation."); + return; + } - // Initialize the SMTP client (preserve the variable name 'client') - using (SmtpClient client = new SmtpClient("smtp.example.com", 587, "username", "password")) + // Create and configure the Exchange client + using (ExchangeClient client = new ExchangeClient(mailboxUri, username, password)) + { + // Create the email message + using (MailMessage message = new MailMessage()) { - try - { - client.Send(message); - Console.WriteLine("Message sent successfully."); - } - catch (Exception ex) - { - Console.Error.WriteLine($"Error sending message: {ex.Message}"); - return; - } + message.From = new MailAddress("sender@example.com", "Sender"); + message.To.Add(new MailAddress("recipient@example.com", "Recipient")); + message.Subject = "Test Message with High Priority"; + message.Body = "This is a test email with high priority."; + + // Set the priority flag to High + message.Priority = MailPriority.High; + + // Send the message + client.Send(message); + Console.WriteLine("Message sent successfully."); } } } catch (Exception ex) { - Console.Error.WriteLine($"Unexpected error: {ex.Message}"); + Console.Error.WriteLine($"Error: {ex.Message}"); } } }