DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Custom Domains for HTTP/2 on Heroku
  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • Comparing Axios, Fetch, and Angular HttpClient for Data Fetching in JavaScript
  • 5 Simple Steps To Get Your Test Suite Running in Heroku CI

Trending

  • Harnessing the Power of Distributed Databases on Kubernetes
  • Hot Class Reload in Java: A Webpack HMR-Like Experience for Java Developers
  • Three Habits of Highly Effective Observability Teams
  • AI-Driven Intent-Based Networking: The Future of Network Management Using AI

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

By 
David Zondray user avatar
David Zondray
·
Mar. 11, 15 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free
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[i].UniqueId, "c:\\temp\\" + list[i].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[i].UniqueId);
    Msg.Save(list[i].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);
Fetch (FTP client) app

Opinions expressed by DZone contributors are their own.

Related

  • Custom Domains for HTTP/2 on Heroku
  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • Comparing Axios, Fetch, and Angular HttpClient for Data Fetching in JavaScript
  • 5 Simple Steps To Get Your Test Suite Running in Heroku CI

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: