Receive Email with POP3 in C# and VB.NET

GemBox.Email provides two methods for receiving mail messages with POP3 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.Mime;
3using GemBox.Email.Pop;
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 (PopClient pop = new PopClient("<ADDRESS> (e.g. pop.gmail.com)"))
20        {
21            pop.Connect();
22            Console.WriteLine("Connected.");
23
24            pop.Authenticate("<USERNAME>", "<PASSWORD>");
25            Console.WriteLine("Authenticated.");
26
27            // Download message with sequence number 1 (first one) and list its properties
28            MailMessage message = pop.GetMessage(1);
29            Console.WriteLine("Listing message 1...");
30            Console.WriteLine();
31
32            Console.WriteLine("------------------ HEADERS ---------------");
33            Console.WriteLine("From   : " + message.From.ToString());
34            Console.WriteLine("To     : " + message.To.ToString());
35            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture));
36            Console.WriteLine("Subject: " + message.Subject);
37            Console.WriteLine("------------------- BODY -----------------");
38            Console.WriteLine(message.BodyText);
39            Console.WriteLine("------------------- END ------------------");
40
41            // Save second message on server to file without parsing it
42            pop.SaveMessage(2, "Message2.eml");
43
44            Console.WriteLine();
45            Console.WriteLine("Message 2 saved.");
46        }
47    }
48}
1Imports System.Globalization
2Imports System.Net.Security
3Imports System.Security.Cryptography.X509Certificates
4Imports GemBox.Email
5Imports GemBox.Email.Mime
6Imports GemBox.Email.Pop
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 pop As New PopClient("<ADDRESS> (e.g. pop.gmail.com)")
16
17            pop.Connect()
18            Console.WriteLine("Connected.")
19
20            pop.Authenticate("<USERNAME>", "<PASSWORD>")
21            Console.WriteLine("Authenticated.")
22
23            ' Download message with sequence number 1 (first one) and list its properties
24            Dim message As MailMessage = pop.GetMessage(1)
25            Console.WriteLine("Listing message 1...")
26            Console.WriteLine()
27
28            Console.WriteLine("------------------ HEADERS ---------------")
29            Console.WriteLine("From   : " + message.From.ToString())
30            Console.WriteLine("To     : " + message.To.ToString())
31            Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture))
32            Console.WriteLine("Subject: " + message.Subject)
33            Console.WriteLine("------------------- BODY -----------------")
34            Console.WriteLine(message.BodyText)
35            Console.WriteLine("------------------- END ------------------")
36
37            ' Save second message on server to file without parsing it
38            pop.SaveMessage(2, "Message2.eml")
39
40            Console.WriteLine()
41            Console.WriteLine("Message 2 saved.")
42
43        End Using
44
45    End Sub
46
47End Module

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