MailBee.NET Objects 7.1

Imap.GetFolderSize Method 

Downloads the size of each message in the currently selected folder and returns the total size occupied by all messages in this folder.

public long GetFolderSize();

Return Value

On success, return the total size occupied by all messages in this folder; otherwise, -1.

Remarks

If the application needs to calculate the total size of all messages in all folders of the mail account, it should use QUOTA extension (GetAccountQuota method) and fall back to multiple GetFolderSize calls only if QUOTA is not supported. However, iterating through all the folders and downloading messages' sizes may take considerable amount of time. Also, it's not possible to determine sizes of non-selectable folders and sizes of folders themselves (without their contained messages) using non-QUOTA approach. Thus, the storage size obtained from GetAccountQuota is usually larger (by 1% or so) than the size determined via a series of SelectFolder and GetFolderSize calls.

Note   GetFolderSize method considers only messages belonging to the folder itself. If the folder contains any sub-folders, the sizes of messages in these subfolders will not be included into the returned value.

Exceptions

Exception Type Condition
MailBeeException An error occurred and ThrowExceptions is true.

Example

This sample attempts to gets the size of the currently used account storage using QUOTA extension. If QUOTA is not supported by the server, the sample selects all selectable folders in the account one by another, and calls GetFolderSize for each folder.

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

class Sample
{
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        // Connect to the server and log in the account.
        imp.Connect("mail.company.com");
        imp.Login("jdoe@company.com", "secret");

        long accountStorageUsedSize = -1;

        if (imp.GetExtension("QUOTA") != null)
        {
            // This may still return -1 if the QUOTA is supported
            // in general but storage quota in bytes is not.
            accountStorageUsedSize = imp.GetAccountQuota().CurrentStorageSize;
        }

        if (accountStorageUsedSize < 0)
        {
            // Fall back to a series of SelectFolder/GetFolderSize calls.
            accountStorageUsedSize = 0;
            FolderCollection folders = imp.DownloadFolders();
            
            foreach (Folder fold in folders)
            {
                // For each selectable folder, select it and
                // determine the total size of all messages.
                if ((fold.Flags & FolderFlags.Noselect) == 0)
                {
                    imp.SelectFolder(fold.Name);
                    accountStorageUsedSize += imp.GetFolderSize();
                }
            }
        }

        Console.WriteLine("Currently used account storage size is " +
            accountStorageUsedSize + " bytes");

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Module Sample
    Sub Main(ByVal args As String())
        Dim imp As New Imap

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

        Dim accountStorageUsedSize As Long = -1

        If Not imp.GetExtension("QUOTA") Is Nothing Then
            ' This may still return -1 if the QUOTA is supported
            ' in general but storage quota in bytes is not.
            accountStorageUsedSize = imp.GetAccountQuota().CurrentStorageSize
        End If

        If accountStorageUsedSize < 0 Then
            ' Fall back to a series of SelectFolder/GetFolderSize calls.
            accountStorageUsedSize = 0
            Dim folders As FolderCollection = imp.DownloadFolders()

            For Each fold As Folder In folders
                ' For each selectable folder, select it and
                ' determine the total size of all messages.
                If (fold.Flags And FolderFlags.Noselect) = 0 Then
                    imp.SelectFolder(fold.Name)
                    accountStorageUsedSize += imp.GetFolderSize()
                End If
            Next
        End If

        Console.WriteLine("Currently used account storage size is " & _
            accountStorageUsedSize & " bytes")

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace