Receive Email with IMAP in C# and VB.NET

GemBox.Email provides two methods for receiving mail messages with IMAP client: GetMessage and SaveMessage. GetMessage parses the server response and returns a MailMessage instance, while SaveMessage saves a received message to a stream or a file without parsing it.

The following example shows how to receive a message using both methods with the GemBox.Email library.

Screenshot

Receive Message Screenshot

See the full code below.

1using GemBox.Email;
2using GemBox.Email.Imap;
3using GemBox.Email.Mime;
4using GemBox.Email.Security;
5using System;
6using System.Collections.Generic;
7using System.Globalization;
8using System.Net.Security;
9using System.Security.Cryptography.X509Certificates;
10
11class Sample
12{
13    [STAThread]
14    static void Main(string[] args)
15    {
16        // If using Professional version, put your serial key below.
17        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
18
19        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
20        {
21            imap.Connect();
22            Console.WriteLine("Connected.");
23
24            imap.Authenticate("<USERNAME>", "<PASSWORD>");
25            Console.WriteLine("Authenticated.");
26
27            // Select INBOX folder
28            imap.SelectInbox();
29
30            // Download message with sequence number 1 (first one) and list its properties
31            MailMessage message = imap.GetMessage(1);
32            Console.WriteLine("Listing message 1...");
33            Console.WriteLine();
34
35            Console.WriteLine("------------------ HEADERS ---------------");
36            Console.WriteLine("From   : " + message.From.ToString());
37            Console.WriteLine("To     : " + message.To.ToString());
38            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture));
39            Console.WriteLine("Subject: " + message.Subject);
40            Console.WriteLine("------------------- BODY -----------------");
41            Console.WriteLine(message.BodyText);
42            Console.WriteLine("------------------- END ------------------");
43
44            // Save second message on server to file without parsing it
45            imap.SaveMessage(2, "Message2.eml");
46
47            Console.WriteLine();
48            Console.WriteLine("Message 2 saved.");
49        }
50    }
51}
1Imports System.Globalization
2Imports System.Net.Security
3Imports System.Security.Cryptography.X509Certificates
4Imports GemBox.Email
5Imports GemBox.Email.Imap
6Imports GemBox.Email.Mime
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")
16
17            imap.Connect()
18            Console.WriteLine("Connected.")
19
20            imap.Authenticate("<USERNAME>", "<PASSWORD>")
21            Console.WriteLine("Authenticated.")
22
23            ' Select INBOX folder
24            imap.SelectInbox()
25
26            ' Download message with sequence number 1 (first one) and list its properties
27            Dim message As MailMessage = imap.GetMessage(1)
28            Console.WriteLine("Listing message 1...")
29            Console.WriteLine()
30
31            Console.WriteLine("------------------ HEADERS ---------------")
32            Console.WriteLine("From   : " + message.From.ToString())
33            Console.WriteLine("To     : " + message.To.ToString())
34            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture))
35            Console.WriteLine("Subject: " + message.Subject)
36            Console.WriteLine("------------------- BODY -----------------")
37            Console.WriteLine(message.BodyText)
38            Console.WriteLine("------------------- END ------------------")
39
40            ' Save second message on server to file without parsing it
41            imap.SaveMessage(2, "Message2.eml")
42
43            Console.WriteLine()
44            Console.WriteLine("Message 2 saved.")
45
46        End Using
47
48    End Sub
49
50End Module

Check next sample or find out more about GemBox.Email and GemBox Software.