Skip to content
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using Aspose.Email;

class Program
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,108 @@
using System;
using System.IO;
using Aspose.Email;
using Aspose.Email.Mime;
using Aspose.Email.Mapi;

class Program
{
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)
{
Expand Down
Loading