Get common email fields (Subject, Text) with IMAP
In this article we’ll show how to download emails using Mail.dll IMAP client and access some common email properties like: Subject, Date, Html, Text and Attachments.
Remember about adding a reference to Mail.dll and importing namespaces.
First you need to download email from IMAP server using Imap class. Then parse it using MailBuilder class. After that you’ll receive IMail interface.
This interface can be used to access all standard and non-standard headers.
Subject, From, To, Date, email data like: Text and Html versions of the email, and others can be accessed as simple properties. This sample also shows how to access Attachments collection.
// C# using System; using System.Collections.Generic; using Limilabs.Client.IMAP; using Limilabs.Mail; using Limilabs.Mail.MIME; using Limilabs.Mail.Headers; class Program { static void Main(string[] args) { using(Imap imap = new Imap()) { imap.Connect("imap.example.com"); // use ConnectSSL for SSL imap.UseBestLogin("user", "password"); imap.SelectInbox(); List<long> uids = imap.Search(Flag.Unseen); foreach (long uid in uids) { var eml = imap.GetMessageByUID(uid); IMail email = new MailBuilder() .CreateFromEml(eml); // Subject Console.WriteLine(email.Subject); // From foreach (MailBox m in email.From) { Console.WriteLine(m.Address); Console.WriteLine(m.Name); } // Date Console.WriteLine(email.Date); // Text body of the message Console.WriteLine(email.Text); // Html body of the message Console.WriteLine(email.Html); // Custom header Console.WriteLine(email.Document.Root.Headers["x-spam"]); // Save all attachments to disk foreach (MimeData mime in email.Attachments) { mime.Save(@"c:\" + mime.SafeFileName); } } imap.Close(); } } };
' VB.NET Imports Limilabs.Client.IMAP Imports Limilabs.Mail Imports Limilabs.Mail.MIME Imports Limilabs.Mail.Headers Imports System Imports System.Collections.Generic Public Module Module1 Public Sub Main(ByVal args As String()) Using imap As New Imap() imap.Connect("pop3.example.com") ' use ConnectSSL for SSL imap.UseBestLogin("user", "password") imap.SelectInbox() Dim uids As List(Of Long) = imap.Search(Flag.Unseen) For Each uid As Long In uids Dim eml = imap.GetMessageByUID(uid) Dim email As IMail = New MailBuilder() _ .CreateFromEml(eml) ' Subject Console.WriteLine(email.Subject) ' From For Each m As MailBox In email.From Console.WriteLine(m.Address) Console.WriteLine(m.Name) Next ' Date Console.WriteLine(email.[Date]) ' Text body of the message Console.WriteLine(email.Text) ' Html body of the message Console.WriteLine(email.Html) ' Custom header Console.WriteLine(email.Document.Root.Headers("x-spam-value")) ' Save all attachments to disk For Each mime As MimeData In email.Attachments mime.Save("c:\" + mime.SafeFileName) Next Next imap.Close() End Using End Sub End Module
To, Cc and Bcc headers are a bit tricky as they can contain address groups. You can learn more about how to work with them in how to access To, Cc, Bcc fields article.