Home > Code > C# > How to Fetch Email Messages from IMAP Server & Save it as EML, MSG inside .NET Apps

How to Fetch Email Messages from IMAP Server & Save it as EML, MSG inside .NET Apps

by sher azam   on Mar 11, 2015   Category: C#   |  Views: 1537    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can fetch email messages from IMAP server and save it to disk in different formats. The first example shows how to fetch email messages from a server and save them, in EML format, to disk. The following steps are required to save the messages to disk:

• Create an instance of the ImapClient class.
• Specify hostname, username and password in the ImapClient constructor.
• Select the folder using SelectFolder() method.
• Call the ListMessages() method to get the ImapMessageInfoCollection object.
• Iterate through the ImapMessageInfoCollection collection, call the SaveMessage() method and provide the output path and file name.

To save emails in MSG format, the ImapClient.FetchMessage() method needs to be called. It returns the message in an instance of the MailMessage class. The MailMessage.Save() method can then be called to save the message to MSG. Aspose.Email provides a 2-member overloaded variant of ListMessages to retrieve a specified number of messages based on a query. The IMAP protocol supports listing messages recursively from a mailbox folder. This helps list messages from subfolders of a folder as well.

The code samples below show how to fetch email messages from a server and save them, in EML format, to disk.



//[C# Code Sample]

// Select the inbox folder
client.SelectFolder(ImapFolderInfo.InBox);

// Get the message info collection
ImapMessageInfoCollection list = client.ListMessages();

// Download each message
for (int i = 0; i < list.Count; i++)
{
//Save the EML file locally
client.SaveMessage(list.UniqueId, "c:\\temp\\" + list.UniqueId + ".eml");
}

//[VB.NET Code Sample]

' Select the inbox folder
client.SelectFolder(ImapFolderInfo.InBox)

' Get the message info collection
Dim list As ImapMessageInfoCollection = client.ListMessages()

' Download each message
Dim i As Integer = 0
Do While i < list.Count
'Save the EML file locally
client.SaveMessage(list(i).UniqueId, "c:\temp\" & list(i).UniqueId & ".eml")
i += 1
Loop

//Saving Messages from IMAP Server in MSG Format

//[C# Code Sample]

// Select the inbox folder
client.SelectFolder(ImapFolderInfo.InBox);

// Get the message info collection
ImapMessageInfoCollection list = client.ListMessages();

// Download each message
for (int i = 0; i < list.Count; i++)
{
// Save the message in MSG format
MailMessage msg = client.FetchMessage(list.UniqueId);
Msg.Save(list.UniqueId + ".msg", MailMessageSaveType.OutlookMessageFormat);
}

//[VB.NET Code Sample]

' Select the inbox folder
client.SelectFolder(ImapFolderInfo.InBox)

' Get the message info collection
Dim list As ImapMessageInfoCollection = client.ListMessages()

' Download each message
Dim i As Integer = 0
Do While i < list.Count
' Save the message in MSG format
Dim msg As MailMessage = client.FetchMessage(list(i).UniqueId)
Msg.Save(list(i).UniqueId & ".msg", MailMessageSaveType.OutlookMessageFormat)
i += 1
Loop

//List Messages with Maximum Number of Messages

//[C# Code Sample]

imapClient.SelectFolder("Inbox");
ImapQueryBuilder builder = new ImapQueryBuilder();
MailQuery query =
builder.Or(
builder.Or(
builder.Or(
builder.Or(
builder.Subject.Contains(" (1) "),
builder.Subject.Contains(" (2) ")),
builder.Subject.Contains(" (3) ")),
builder.Subject.Contains(" (4) ")),
builder.Subject.Contains(" (5) "));
ImapMessageInfoCollection messageInfoCol4 = imapClient.ListMessages(query, 4);
Console.WriteLine((messageInfoCol4.Count == 4) ? "Success" : "Failure");

//[VB.NET Code Sample]

ImapClient client = new ImapClient();
client.Host = "domain.com";
client.Username = "username";
client.Password = "password";

client.SelectFolder("InBox");
ImapMessageInfoCollection msgsColl = client.ListMessages(true);
Console.WriteLine("Total Messages: " + msgsColl.Count);



Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts