MailBee.NET Objects 3.1

Resources Class

Provides localization and text customization framework for MailBee library.

For a list of all members of this type, see Resources Members.

System.Object
   MailBee.Resources

public class Resources

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

The developer can derive a class from Resources class and override some or all properties for the purposes of localization or other tasks which require modification of any messages returned by MailBee (this includes log file messages, error messages, etc).

CAUTION   For this class, it's not guaranteed to preserve compatibility with future versions of MailBee library. In particular, properties returning error message strings may be replaced.

Example

This sample changes the standard log message "Messages (startIndex={0}, count={1}) marked as deleted." (defined by Log_Pop3DeletedMessagesStartIndex0Count1 property) to "{1} message(s) starting at {0} flagged as deleted.". Log_Pop3WillDeleteMessagesStartIndex0Count1 property is overridden as well.

To show the effect of this change, the logging is on, and messages in the POP3 mailbox are flagged as deleted twice (so the log messages are added using the standard resources and then using the modified resources). However, no message is actually deleted since ResetDeletes method is called each time.

To improve readability of the log, all log messages which are not necessary for understanding this sample are filtered out in LogNewEntry event handler.

[C#]
using System;
using MailBee;
using MailBee.Pop3Mail;

class Sample
{
    // We set it to true only for the time while DeleteMessages is executing.
    // Alternatively, we could just enable logging only for the time of 
    // DeleteMessages execution and have it disabled at other times.
    private static bool    m_addEntry;

    // LogNewEntry event handler
    private static void OnLogNewEntry(object sender, LogNewEntryEventArgs e)
    {
        // Do not add log records which either do not apply to 
        // DeleteMessages or contain data transferred between the client 
        // and the server. This way, we filter all log messages which do 
        // not directly relate to this sample.
        if (!m_addEntry || e.NewEntry.MessageType != LogMessageType.Info)
        {
            e.NewEntry.AddThisEntry = false;
        }
    }

    // The actual code
    static void Main(string[] args)
    {
        Pop3 pop = new Pop3();

        // Subscribe to the LogNewEntry to filter out unnecessary log records.
        pop.LogNewEntry += new LogNewEntryEventHandler(OnLogNewEntry);

        // Connect to the POP3 server and log in the user account.
        pop.Connect("mail.domain.com");
        pop.Login("jdoe", "secret");

        // Enable logging after already connected to the server.
        // Alternatively, we could move this code above pop.Connect 
        // and set m_addEntry=false there.
        pop.Log.Enabled = true;
        pop.Log.Filename = @"C:\pop3_log.txt";
        pop.Log.Clear();

        // Will add the records produced by DeleteMessages, to the log.
        m_addEntry = true;

        // Flag the messages as deleted using Resources class.
        pop.Log.WriteLine("The following 2 log records are added using standard resources");
        pop.DeleteMessages(1, 3);

        // We do not want to overburden the log with ResetDeletes results.
        m_addEntry = false;
        pop.ResetDeletes();

        // Flag the messages as deleted using MyResources class.
        Resources.Instance = new MyResources();
        m_addEntry = true;
        pop.Log.WriteLine(null);
        pop.Log.WriteLine("The following 2 log records are added using MyResources");
        pop.DeleteMessages(1, 3);

        // We do not want to overburden the log with ResetDeletes and Disconnect results.
        m_addEntry = false;
        pop.ResetDeletes();

        pop.Disconnect();
    }
}

// Derive our resources class from Resource and override the properties 
// which return the string values used to log DeleteMessages method progress.
class MyResources : Resources
{
    public override string Log_Pop3DeletedMessagesStartIndex0Count1
    {
        get { return "{1} message(s) starting at {0} flagged as deleted."; }
    }

    public override string Log_Pop3WillDeleteMessagesStartIndex0Count1
    {
        get { return "Will flag {1} message(s) starting at {0} as deleted."; }
    }
}

// After running this code, C:\pop3_log.txt file should look like below.
[00:41:02.54] [USER] The following 2 log records are added using standard resources
[00:41:02.55] [INFO] Will mark messages (startIndex=1, count=3) as deleted.
[00:41:02.56] [INFO] Messages (startIndex=1, count=3) marked as deleted.
[00:41:02.56] [USER] 
[00:41:02.57] [USER] The following 2 log records are added using MyResources
[00:41:02.57] [INFO] Will flag 3 message(s) starting at 1 as deleted.
[00:41:02.57] [INFO] 3 message(s) starting at 1 flagged as deleted.
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Pop3Mail

Class Sample
    ' We set it to true only for the time while DeleteMessages is executing.
    ' Alternatively, we could just enable logging only for the time of 
    ' DeleteMessages execution and have it disabled at other times.
    Private Shared m_addEntry As Boolean

    ' LogNewEntry event handler
    Private Shared Sub OnLogNewEntry(ByVal sender As Object, ByVal e As LogNewEntryEventArgs)

        ' Do not add log records which either do not apply to 
        ' DeleteMessages or contain data transferred between the client 
        ' and the server. This way, we filter all log messages which do 
        ' not directly relate to this sample.
        If (Not m_addEntry) Or (e.NewEntry.MessageType <> LogMessageType.Info) Then
            e.NewEntry.AddThisEntry = False
        End If
    End Sub

    ' The actual code
    Shared Sub Main(ByVal args As String())
        Dim pop As New Pop3

        ' Subscribe to the LogNewEntry to filter out unnecessary log records.
        AddHandler pop.LogNewEntry, AddressOf OnLogNewEntry

        ' Connect to the POP3 server and log in the user account.
        pop.Connect("mail.domain.com")
        pop.Login("jdoe", "secret")

        ' Enable logging after already connected to the server.
        ' Alternatively, we could move this code above pop.Connect 
        ' and set m_addEntry=false there.
        pop.Log.Enabled = True
        pop.Log.Filename = "C:\pop3_log.txt"
        pop.Log.Clear()

        ' Will add the records produced by DeleteMessages, to the log.
        m_addEntry = True

        ' Flag the messages as deleted using Resources class.
        pop.Log.WriteLine("The following 2 log records are added using standard resources")
        pop.DeleteMessages(1, 3)

        ' We do not want to overburden the log with ResetDeletes results.
        m_addEntry = False
        pop.ResetDeletes()

        ' Flag the messages as deleted using MyResources class.
        MailBee.Resources.Instance = New MyResources
        m_addEntry = True
        pop.Log.WriteLine(Nothing)
        pop.Log.WriteLine("The following 2 log records are added using MyResources")
        pop.DeleteMessages(1, 3)

        ' We do not want to overburden the log with ResetDeletes and Disconnect results.
        m_addEntry = False
        pop.ResetDeletes()

        pop.Disconnect()
    End Sub
End Class

' Derive our resources class from Resource and override the properties 
' which return the string values used to log DeleteMessages method progress.
Class MyResources
    Inherits MailBee.Resources
    Public Overrides ReadOnly Property Log_Pop3DeletedMessagesStartIndex0Count1() As String
        Get
            Return "{1} message(s) starting at {0} flagged as deleted."
        End Get
    End Property

    Public Overrides ReadOnly Property Log_Pop3WillDeleteMessagesStartIndex0Count1() As String
        Get
            Return "Will flag {1} message(s) starting at {0} as deleted."
        End Get
    End Property
End Class

' After running this code, C:\pop3_log.txt file should look like below.
[00:41:02.54] [USER] The following 2 log records are added using standard resources
[00:41:02.55] [INFO] Will mark messages (startIndex=1, count=3) as deleted.
[00:41:02.56] [INFO] Messages (startIndex=1, count=3) marked as deleted.
[00:41:02.56] [USER] 
[00:41:02.57] [USER] The following 2 log records are added using MyResources
[00:41:02.57] [INFO] Will flag 3 message(s) starting at 1 as deleted.
[00:41:02.57] [INFO] 3 message(s) starting at 1 flagged as deleted.

Requirements

Namespace: MailBee

Assembly: MailBee.NET (in MailBee.NET.dll)

See Also

Resources Members | MailBee Namespace