MailBee. NET Objects Tutorials

Logging of conversation with mail server into a file

To monitor MailBee.NET activities into a file, the developer should use Logger class. Pop3.Log property provides access to the instance of this class for Pop3 objects ( Imap and Smtp classes also support this property). To monitor the progress of the POP3 session, you can use the following code:

C#

Pop3 pop = new Pop3();
pop.Log.Enabled = true;
pop.Log.Filename = @"C:\log.txt";
pop.Log.MaxSize = 4096;
pop.Log.Clear();

VB.NET

Dim pop As New Pop3() 
pop.Log.Enabled = True
pop.Log.Filename = "C:\log.txt"
pop.Log.MaxSize = 4096
pop.Log.Clear()

The log file provides a lot of useful and detailed information on how MailBee.NET communicates with mail servers. The log file keeps track of any connection and protocol errors as well.

Logger class also supports logging into a memory buffer instead of a file ( MemoryLog property).

To prevent unlimited growth of the log file/memory buffer, the maximum size of the log file can be restricted to a certain value (see MaxSize property). When the log size is going to exceed MaxSize value, the log file is renamed to OldFilename name and then cleared (or just cleared if OldFilename property is not set). The developer can also use Clear method to clear the log file/buffer manually.

Logger class also supports properties affecting the formatting of the log file/buffer, its codepage, etc.

If multiple instances of Logger class running in concurrent threads write into the same log file (for instance, you're checking mail in multiple mailboxes simultaneously), you should synchronize access to the log file using Logger.SyncRoot property:

C#

Pop3 pop1 = new Pop3();
Pop3 pop2 = new Pop3();
Pop3 pop3 = new Pop3();
object sync = new object();
pop1.Log.SyncRoot = sync;
pop2.Log.SyncRoot = sync;
pop3.Log.SyncRoot = sync;

VB.NET

Dim pop1 As New Pop3() 
Dim pop2 As New Pop3() 
Dim pop3 As New Pop3() 
Dim sync As New Object() 
pop1.Log.SyncRoot = sync
pop2.Log.SyncRoot = sync
pop3.Log.SyncRoot = sync