Create Folder and Manage Folder using IMAP4/Exchange Web Service (EWS)/WebDAV in Visual C++

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

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.

[Visual C++ 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.

#include "stdafx.h"
#include <windows.h>

#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;

int _tmain(int argc, _TCHAR* argv[])
{
    const int MailServerPop3 = 0;
    const int MailServerImap4 = 1;
    const int MailServerEWS = 2;
    const int MailServerDAV = 3;

    // Initialize COM environment
    ::CoInitialize( NULL );

    try
    {
        IMailServerPtr oServer = NULL;
        oServer.CreateInstance(__uuidof(EAGetMailObjLib::MailServer));

        oServer->Server = _T("imap.emailarchitect.net");
        oServer->User = _T("test@emailarchitect.net");
        oServer->Password = _T("testpassword");
        oServer->Protocol = MailServerImap4;

        // Enable SSL Connection,  most modern email servers require SSL/TLS by default
        oServer->SSLConnection = VARIANT_TRUE;
        // Set 993 SSL IMAP4 port
        oServer->Port = 993;

        // If your IMAP doesn't deploy SSL connection
        // Please use
        // oServer->SSLConnection = VARIANT_FALSE;
        // oServer->Port = 143;

        IMailClientPtr oClient = NULL;
        oClient.CreateInstance(__uuidof(EAGetMailObjLib::MailClient));
        oClient->LicenseCode = _T("TryIt");
        oClient->Connect(oServer);

        IImap4FolderPtr oFolder = oClient->CreateFolder(NULL, _T("Test Folder"));

        IFolderCollectionPtr folders = oClient->GetFolderList();
        for(long i = 0; i < folders->Count; i++)
        {
            IImap4FolderPtr fd = folders->GetItem(i);
            _tprintf( _T("folder: %s\r\n"), (TCHAR*)fd->FullPath);
        }

        oClient->DeleteFolder(oFolder);
        oClient->Logout();
    }
    catch (_com_error &ep)
    {
        _tprintf(_T("Error: %s"), (const TCHAR*)ep.Description());
    }
    return 0;
}

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/203), IMAP4 protocol does not expose public folders to mail client. If you want to access public folders on MS Exchange 2007/2010/2013/2016 or later version, please use MailClient.GetEWSPublicFolderList 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.