Export Email Contents to a Local File


Saving email messages to a local file is useful for archiving messages, processing them offline, or passing them to other systems without requiring access to the mail server. The POP and IMAP components can write a retrieved message directly to a file by setting the LocalFile property before retrieving it.

By default, only the body of the message is written to the file. To save the complete message (including its headers), enable the components' IncludeHeaders option. The resulting file contains the message in its original MIME format, and can be saved with an .eml extension, which most email clients can open directly.

Saving Messages with the POP Component

When using the POP component, set the MessageNumber property to the number of the message to retrieve, set LocalFile to the path of the file, and set the IncludeHeaders property to true before calling the Retrieve method.

POP pop = new POP();
pop.MailServer = "mail.example.com";
pop.User = "USER";
pop.Password = "PASSWORD";
pop.Connect();
// Save each message in the mailbox to a separate .eml file.
for (int i = 1; i <= pop.MessageCount; i++) {
  pop.MessageNumber = i;
  pop.LocalFile = "C:\\mail\\message" + i + ".eml";
  pop.IncludeHeaders = true;
  pop.Overwrite = true; // Replace the file if it already exists.
  pop.Retrieve();
}
pop.Disconnect();

Saving Messages with the IMAP Component

When using the IMAP component, select the mailbox, set the MessageSet property to the message to retrieve, and set LocalFile to the path of the file. The headers are included in the file by enabling the IncludeHeaders configuration setting, and the message is retrieved using the RetrieveMessageText method.

IMAP imap = new IMAP();
imap.MailServer = "mail.example.com";
imap.User = "USER";
imap.Password = "PASSWORD";
imap.Connect();
imap.Mailbox = "INBOX";
imap.SelectMailbox();
// Save the first message in the mailbox, including its headers.
imap.MessageSet = "1";
imap.LocalFile = "C:\\mail\\message1.eml";
imap.Config("IncludeHeaders=true");
imap.Overwrite = true; // Replace the file if it already exists.
imap.RetrieveMessageText();
imap.Disconnect();

Notes

  • If the file specified by LocalFile already exists, the component throws an error unless the Overwrite property is set to true.
  • When LocalFile is empty, the retrieved message is instead available in the MessageText property (and through the Transfer event), so it can be processed in memory or written to a file by the application.
  • To read the contents of a saved message later (such as its headers, body, and attachments), the file can be parsed with the MIME component.
  • Connecting to most mail servers requires TLS, which is configured through the components' SSLStartMode property, and many providers (such as Gmail and Microsoft 365) require OAuth authentication. For more information, please see our Using OAuth with the SMTP, IMAP, and POP Components article.

Note that in older versions of IPWorks, the IMAP component retrieved message text using the FetchMessageText method, which has been replaced by the RetrieveMessageText method.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.