Create Folder and Manage Folder using IMAP4/Exchange Web Service (EWS)/WebDAV in VB.NET

In previous section, I introduced how to parse non-delivery report (NDR) in VB.NET. In this section, I will introduce how to create folders and manage folders with IMAP4/Exchange Web Service (EWS)/WebDAV protocol in VB.NET.

Create Folder and Delete Folder

Because IMAP4/Exchange Web Service (EWS)/WebDAV protocol supports folder access, so we can retrieve email from other mailbox rather than default “INBOX”, I have introduced it in other sections. In this section, I will introduce how to use EAGetMail to create folder and delete folder with IMAP4/Exchange Web Service/WebDAV protocol. Notice: POP3 protocol doesn’t support this feature.

[VB.NET Example - Create Folder and Delete Folder]

The following example codes demonstrate how to create folder and delete folder.

Note

To get the full sample projects, please refer to Samples section.

Imports System.Globalization
Imports System.Text
Imports System.IO
Imports EAGetMail 'imports EAGetMail namespace

Module Module1

    Sub Main()

        Try
            ' Create a folder named "inbox" under current directory
            ' to save the email retrieved.
            Dim localInbox As String = String.Format("{0}\inbox", Directory.GetCurrentDirectory())

            ' If the folder is not existed, create it.
            If Not Directory.Exists(localInbox) Then
                Directory.CreateDirectory(localInbox)
            End If

            Dim oServer As New MailServer("imap.emailarchitect.net",
                        "test@emailarchitect.net",
                        "testpassword",
                        ServerProtocol.Imap4)

            ' Enable SSL/TLS connection, most modern email server require SSL/TLS connection by default.
            oServer.SSLConnection = True
            oServer.Port = 993

            ' If your server doesn't support SSL/TLS connection, please use the following codes
            ' oServer.SSLConnection = False
            ' oServer.Port = 143

            Console.WriteLine("Connecting server ...")

            Dim oClient As New MailClient("TryIt")
            oClient.Connect(oServer)

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

            ' list existed folders
            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.FullPath)
            Next

            ' delete folder
            oClient.DeleteFolder(folder)
            oClient.Logout()

            Console.WriteLine("Completed!")

        Catch ep As Exception
            Console.WriteLine(ep.Message)
        End Try

    End Sub
End Module

Retrieve/Copy/Move/Upload Email to Folder

To retrieve emails from a specified folder, please use SelectFolder method; To move email from a folder to another folder, please use Move method; To copy email from a folder to another folder, please use Copy method; To upload an email file from local disk to server folder, please use Append method.

Note

Please refer to ImapFull project for more detail.

Access Exchange Shared Mailbox

A MS Exchange shared mailbox is a mailbox that multiple users can use to read and send email messages. To access shared mailbox, please use MailServer.Alias property.

Access Exchange 2007/2010/2013/2016/2019 Public Mailbox

Since Exchange 2007 or later version (2010/2013/2016/2019), IMAP4 protocol does not expose public folders to mail client. If you want to access public folders on MS Exchange 2007/2010/2013/2016/2019 or later version, please use MailClient.QueryEWSPublicFolders method.

Next Section

Total sample projects in EAGetMail Mail Component installation package.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.