MailClient.DeleteFolder Method


Removes a mail folder on IMAP4/MS Exchange server.

[Visual Basic]
Public Sub DeleteFolder( _
    folder As Imap4Folder _
)

Public Async Function DeleteFolderAsync( _
    folder As Imap4Folder _
) As Task
[C#]
public void DeleteFolder(
    Imap4Folder folder
);

public async Task DeleteFolderAsync(
    Imap4Folder folder
);
[C++]
public: Void DeleteFolder(
    Imap4Folder^ folder
);
[JScript]
public function DeleteFolder( 
    folder: Imap4Folder
);

Parameters

folder
the folder instance to delete. Notice, many servers don't allow to delete the folder if the folder contains email message or sub-folder.

Example

[Visual Basic, C#, C++] The following example demonstrates how to create, subscribe, unsubscribe and delete "TestFolder" with EAGetMail POP3 & IMAP Component. To get the full samples of EAGetMail, please refer to Samples section.

[VB - Create/delete/subscribe/unsubscribe folder]
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports EAGetMail

Public Class TestClass
    ' if you want to search sub folder, use parentfolder\subfolder as folderPath
    ' for example: inbox\mysubfolder
    Function FindFolder(ByVal folderPath As String, ByRef folders As Imap4Folder()) As Imap4Folder
        For i As Integer = 0 To folders.Length - 1
            Dim folder As Imap4Folder = folders(i)
            ' Folder was found.
            If String.Compare(folder.LocalPath, folderPath, True) = 0 Then
                Return folder
            End If

            folder = FindFolder(folderPath, folder.SubFolders)
            If Not (folder Is Nothing) Then
                Return folder
            End If
        Next

        ' No folder found
        Return Nothing
    End Function

    Public Sub CreateDeleteFolder(server As String, user As String, password As String, useSsl As Boolean)
        Try
            ' ExchangeEWS Or ExchangeWebDAV protocol also supports folder operation.

            ' Most modern email server require SSL/TLS connection, 
            ' set useSsl to true Is recommended.
            Dim oServer As New MailServer(server, user, password, useSsl,
                        ServerAuthType.AuthLogin, ServerProtocol.Imap4)

            ' IMAP4 port Is 143,  IMAP4 SSL port Is 993.
            ' EWS/WebDAV, please ignore Port property.
            oServer.Port = If(useSsl, 993, 143)

            Console.WriteLine("Connecting server ...")
            Dim oClient As New MailClient("TryIt")
            oClient.Connect(oServer)

            ' Create a test folder
            Dim folder As Imap4Folder = oClient.CreateFolder(Nothing, "TestFolder")

            ' Only IMAP4 protocol supports SubscribeFolder
            If oServer.Protocol = ServerProtocol.Imap4 Then
                If Not folder.Subscribed Then
                    oClient.SubscribeFolder(folder)
                End If
            End If

            ' List existed root folders on server
            Dim folders() As Imap4Folder = oClient.GetFolders()
            For i As Integer = 0 To folders.Length - 1
                Dim fd As Imap4Folder = folders(i)
                Console.WriteLine("folder: {0}", fd.LocalPath)
            Next

            ' Only IMAP4 protocol supports UnsubscribeFolder
            If oServer.Protocol = ServerProtocol.Imap4 Then
                oClient.UnsubscribeFolder(folder)
            End If

            ' Delete test folder
            oClient.DeleteFolder(folder)

            oClient.Logout()
            Console.WriteLine("Completed!")
        Catch ep As Exception
            Console.WriteLine(ep.Message)
        End Try

    End Sub

End Class


[C# - Create/delete/subscribe/unsubscribe folder] using System; using System.IO; using System.Globalization; using System.Text; using EAGetMail; class TestClass { // if you want to search sub folder, use parentfolder\subfolder as folderPath // for example: inbox\mysubfolder static Imap4Folder FindFolder(string folderPath, Imap4Folder[] folders) { int count = folders.Length; for (int i = 0; i < count; i++) { Imap4Folder folder = folders[i]; if (string.Compare(folder.LocalPath, folderPath, true) == 0) { return folder; } folder = FindFolder(folderPath, folder.SubFolders); if (folder != null) { return folder; } } // No folder found return null; } public void CreateDeleteFolder(string server, string user, string password, bool useSsl) { try { // ExchangeEWS Or ExchangeWebDAV protocol also supports folder operation. // Most modern email servers require SSL/TLS connection, // set useSsl to true is recommended. MailServer oServer = new MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Imap4); // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. oServer.Port = (useSsl) ? 993 : 143; Console.WriteLine("Connecting server ..."); MailClient oClient = new MailClient("TryIt"); oClient.Connect(oServer); // Create a test folder. Imap4Folder folder = oClient.CreateFolder(null, "TestFolder"); // Only IMAP4 protocol supports SubscribeFolder if (oServer.Protocol == ServerProtocol.Imap4) { if (!folder.Subscribed) { oClient.SubscribeFolder(folder); } } // List existed root folders on server Imap4Folder[] folders = oClient.GetFolders(); for (int i = 0; i < folders.Length; i++) { Imap4Folder fd = folders[i]; Console.WriteLine("folder: {0}", fd.LocalPath); } // Only IMAP4 protocol supports UnsubscribeFolder if (oServer.Protocol == ServerProtocol.Imap4) { oClient.UnsubscribeFolder(folder); } // Delete test folder oClient.DeleteFolder(folder); oClient.Logout(); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } } }
[C++/CLI - Create/delete/subscribe/unsubscribe folder] using namespace System; using namespace System::Globalization; using namespace System::IO; using namespace EAGetMail; //add EAGetMail namespace // if you want to search sub folder, use parentfolder\\subfolder as folderPath // for example: inbox\\mysubfolder Imap4Folder^ FindFolder(String^ folderPath, array<Imap4Folder^> ^folders) { for (int i = 0; i < folders->Length; i++) { Imap4Folder^ folder = folders[i]; // Folder was found. if (String::Compare(folder->LocalPath, folderPath, true) == 0) return folder; folder = FindFolder(folderPath, folder->SubFolders); if (folder != nullptr) return folder; } // No folder found return nullptr; } void CreateDeleteFolder(String ^server, String ^user, String ^password, bool useSsl) { try { // ExchangeEWS or ExchangeWebDAV protocol also supports folder operation. // Most modern email servers require SSL/TLS connection, // set useSsl to true is recommended. MailServer ^oServer = gcnew MailServer(server, user, password, useSsl, ServerAuthType::AuthLogin, ServerProtocol::Imap4); // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. oServer->Port = (useSsl) ? 993 : 143; Console::WriteLine("Connecting server ..."); MailClient ^oClient = gcnew MailClient("TryIt"); oClient->Connect(oServer); // Create a test folder Imap4Folder ^ folder = oClient->CreateFolder(nullptr, "TestFolder"); // Only IMAP4 protocol supports SubscribeFolder if (oServer->Protocol == ServerProtocol::Imap4) { if (!folder->Subscribed) oClient->SubscribeFolder(folder); } // List existed root folders on server. array<Imap4Folder^> ^folders = oClient->GetFolders(); for (int i = 0; i < folders->Length; i++) { Imap4Folder ^fd = folders[i]; Console::WriteLine("folder: {0}", fd->FullPath); } // Only IMAP4 protocol supports UnsubscribeFolder if (oServer->Protocol == ServerProtocol::Imap4) { oClient->UnsubscribeFolder(folder); } // Delete test folder oClient->DeleteFolder(folder); oClient->Logout(); Console::WriteLine("Completed!"); } catch (Exception ^ep) { Console::WriteLine("Error: {0}", ep->Message); } }

See Also

MailClient.RenameFolder
MailClient.CreateFolder
MailClient.SubscribeFolder
MailClient.UnsubscribeFolder