MailClient.SelectFolder Method


Selects an IMAP4/Exchange folder to operate.

[Visual Basic 6.0]
Public Sub SelectFolder( _
    folder As Imap4Folder _
)
[Visual C++]
public: Void SelectFolder(
    IImap4Folder* folder
);

Parameters

folder
the folder instance to select.

Remarks

By default, the "INBOX" will be seleted automatically when the client connects to the IMAP4/Exchange server, then you can get, copy, delete the emails in selected folder. To manipulate emails in specific folder, please use SelectFolder method to select the folder at first. POP3 protocol doesn't support this method.

Example

[Visual Basic 6.0, VBScript, Visual C++, Delphi] The following sample demonstrates how to retrieve email from "Deleted Items" folder. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic 6.0]
Function FindFolder(ByVal FolderPath As String, ByRef folders As FolderCollection)
    Dim i As Long
    For i = 0 To folders.Count - 1 
        Dim oFolder As Imap4Folder
        Set oFolder = folders.Item(i)
        If StrComp(oFolder.LocalPath, FolderPath, 1) = 0 Then
            Set FindFolder = oFolder
            Exit Function
        End If

        Set oFolder = FindFolder(FolderPath, oFolder.SubFolderList)
        If Not (oFolder Is Nothing) Then
            Set FindFolder = oFolder
            Exit Function
        End If
    Next

    Set FindFolder = Nothing
End Function

Public Sub RetrieveMail(ByVal Server As String, _
ByVal User As String, _
ByVal Password As String, _
ByVal SSLConnection As Boolean)
    Const MailServerPop3 = 0
    Const MailServerImap4 = 1
    Const MailServerEWS = 2
    Const MailServerDAV = 3
    Const MailServerMsGraph = 4

On Error GoTo ErrorHandle
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = Server
    oServer.User = User
    oServer.Password = Password
    oServer.SSLConnection = SSLConnection
    oServer.Protocol = MailServerImap4

    'To retrieve email with Exchange Web Service, please change
    'MailServerImap4 to MailServerEWS to MailServer.Protocol

    'To retrieve email with Exchange WebDAV, please change
    'MailServerImap4 to MailServerDAV to MailServer.Protocol

    'Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
    'or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4
    'to download email from Exchange Server, you can use Exchange Web Service(Exchange 2007/2010 or
    'later version) or WebDAV(Exchange 2000/2003) protocol.

    'For Exchange Web Service/WebDAV, please ignore 
    'Port property. But for Exchange Web Service, please set SSLConnection to True

    If oServer.SSLConnection = True Then
        oServer.Port = 993
    Else
        oServer.Port = 143
    End If
    
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"
    
    oClient.Connect oServer

    Dim folders as EAGetMailObjLib.FolderCollection
    Set folders = oClient.GetFolderList()

    Dim oFolder As EAGetMailObjLib.Imap4Folder
    Set oFolder = FindFolder("Deleted Items", folders)

    If oFolder Is Nothing Then
        MsgBox "Folder not found!"
        Exit Sub
    End If

    oClient.SelectFolder oFolder
    MsgBox oClient.SelectedFolder & " is selected!"
    
    ' retrieve emails in selected folder
    Dim infos As EAGetMailObjLib.MailInfoCollection
    Set infos = oClient.GetMailInfoList()
        
    Dim i
    For i = 0 To infos.Count - 1
        Dim info As EAGetMailObjLib.MailInfo
        Set info = infos.Item(i)
        
        Dim oMail As EAGetMailObjLib.Mail
        Set oMail = oClient.GetMail(info)
        'Save mail to local
        oMail.SaveAs "d:\tempfolder\" & i & ".eml", True

        ' Delete email from server
        oClient.Delete info
    Next
    
    '' Delete method just mark the email as deleted,
    ' Quit method expunge the emails from server permanently.
    oClient.Quit
    
    Exit Sub
ErrorHandle:
    MsgBox Err.Description
End Sub


[VBScript] Function FindFolder(ByVal FolderPath, ByRef folders) Dim i For i = 0 To folders.Count - 1 Dim oFolder Set oFolder = folders.Item(i) If StrComp(oFolder.LocalPath, FolderPath, 1) = 0 Then Set FindFolder = oFolder Exit Function End If Set oFolder = FindFolder(FolderPath, oFolder.SubFolderList) If Not (oFolder Is Nothing) Then Set FindFolder = oFolder Exit Function End If Next Set FindFolder = Nothing End Function Sub RetrieveMail(ByVal Server, _ ByVal User, _ ByVal Password, _ ByVal SSLConnection) Const MailServerPop3 = 0 Const MailServerImap4 = 1 Const MailServerEWS = 2 Const MailServerDAV = 3 Const MailServerMsGraph = 4 Dim oServer Set oServer = CreateObject("EAGetMailObj.MailServer") oServer.Server = Server oServer.User = User oServer.Password = Password oServer.SSLConnection = SSLConnection oServer.Protocol = MailServerImap4 'To retrieve email with Exchange Web Service, please change 'MailServerImap4 to MailServerEWS to MailServer.Protocol 'To retrieve email with Exchange WebDAV, please change 'MailServerImap4 to MailServerDAV to MailServer.Protocol 'Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 'or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 'to download email from Exchange Server, you can use Exchange Web Service(Exchange 2007/2010 or 'later version) or WebDAV(Exchange 2000/2003) protocol. 'For Exchange Web Service/WebDAV, please ignore 'Port property. But for Exchange Web Service, please set SSLConnection to True If oServer.SSLConnection = True Then oServer.Port = 993 Else oServer.Port = 143 End If Dim oClient Set oClient = CreateObject("EAGetMailObj.MailClient") oClient.LicenseCode = "TryIt" oClient.Connect oServer Dim folders Set folders = oClient.GetFolderList() Dim oFolder Set oFolder = FindFolder("Deleted Items", folders) If oFolder Is Nothing Then MsgBox "Folder not found!" Exit Sub End If oClient.SelectFolder oFolder ' retrieve emails in selected folder Dim infos Set infos = oClient.GetMailInfoList() Dim i For i = 0 To infos.Count - 1 Dim info Set info = infos.Item(i) Dim oMail Set oMail = oClient.GetMail(info) 'Save mail to local oMail.SaveAs "d:\tempfolder\" & i & ".eml", True ' Delete email from server oClient.Delete info Next '' Delete method just mark the email as deleted, ' Quit method expunge the emails from server permanently. oClient.Quit End Sub
[Visual C++] #include "stdafx.h" #include <windows.h> #include "eagetmailobj.tlh" using namespace EAGetMailObjLib; IImap4FolderPtr FindFolder(LPCTSTR folderPath, IFolderCollectionPtr& folders) { for(long i = 0; i < folders->Count; i++) { IImap4FolderPtr oFolder = folders->GetItem(i); if(_tcsicmp((TCHAR*)oFolder->LocalPath, folderPath) == 0) { return oFolder; } IFolderCollectionPtr subFolders = oFolder->SubFolderList; oFolder = FindFolder(folderPath, subFolders); if(oFolder != NULL) { return oFolder; } } return NULL; } void RetrieveMail(LPCTSTR lpszServer, LPCTSTR lpszUser, LPCTSTR lpszPassword, VARIANT_BOOL SSLConnection) { ::CoInitialize(NULL); const int MailServerPop3 = 0; const int MailServerImap4 = 1; const int MailServerEWS = 2; const int MailServerDAV = 3; const int MailServerMsGraph = 4; try { IMailServerPtr oServer = NULL; oServer.CreateInstance(__uuidof(EAGetMailObjLib::MailServer)); oServer->Server = lpszServer; oServer->User = lpszUser; oServer->Password = lpszPassword; oServer->SSLConnection = SSLConnection; oServer->Protocol = MailServerImap4; //To copy email with Exchange Web Service, please change //MailServerImap4 to MailServerEWS to MailServer.Protocol //To copy email with Exchange WebDAV, please change //MailServerImap4 to MailServerDAV to MailServer.Protocol //Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 //or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 //to download email from Exchange Server, you can use Exchange Web Service(Exchange 2007/2010 or //later version) or WebDAV(Exchange 2000/2003) protocol. //For Exchange Web Service/WebDAV, please ignore //Port property. But for Exchange Web Service, please set SSLConnection to True if(SSLConnection == VARIANT_TRUE) oServer->Port = 993; else oServer->Port = 143; IMailClientPtr oClient = NULL; oClient.CreateInstance(__uuidof(EAGetMailObjLib::MailClient)); oClient->LicenseCode = _T("TryIt"); oClient->Connect(oServer); IFolderCollectionPtr folders = oClient->GetFolderList(); IImap4FolderPtr oFolder = FindFolder(_T("Deleted Items"), folders); if(oFolder == NULL) { //"Source folder " not found throw _com_error(E_FAIL); } //select folder oClient->SelectFolder(oFolder); _tprintf(_T("%s is selected\r\n"), (TCHAR*)oClient->SelectedFolder); // retrieve emails in selected folder IMailInfoCollectionPtr infos = oClient->GetMailInfoList(); for(long i = 0; i < infos->Count; i++) { IMailInfoPtr pInfo = infos->GetItem(i); IMailPtr oMail = oClient->GetMail(pInfo); TCHAR szFile[MAX_PATH+1]; memset(szFile, 0, sizeof(szFile)); ::wsprintf(szFile, _T("d:\\tempfolder\\%d.eml"), i); //save to local disk oMail->SaveAs(szFile, VARIANT_TRUE); // delete email from server oClient->Delete(pInfo); } // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient->Quit(); } catch(_com_error &ep) { _tprintf(_T("%s\r\n"), (TCHAR*)ep.Description()); } ::CoUninitialize(); }
[Delphi] const MailServerPop3 = 0; MailServerImap4 = 1; MailServerEWS = 2; MailServerDAV = 3; function FindFolder(folderPath: WideString; folders: IFolderCollection) :IImap4Folder; var i: integer; folder: IImap4Folder; begin for i:= 0 to folders.Count - 1 do begin folder := folders.Item[i]; if CompareText(folder.LocalPath, folderPath) = 0 then begin result := folder; exit; end; // Search folder in sub-folders folder := FindFolder(folderPath, folder.SubFolderList); if not (folder = nil) then begin result := folder; exit; end; end; // No folder found result := nil; end; procedure RetrieveMail(server: WideString; user: WideString; password: WideString; useSslConnection: Boolean); var oServer: TMailServer; oClient: TMailClient; oTools: TTools; folders: IFolderCollection; oFolder: IImap4Folder; infos: IMailInfoCollection; oInfo: IMailInfo; i: Integer; begin try // set current thread code page to system default code page. SetThreadLocale(GetSystemDefaultLCID()); oTools := TTools.Create(Application); // Create a folder named "inbox" under // current directory to store the email files localInbox := GetCurrentDir() + '\inbox'; oTools.CreateFolder(localInbox); oServer := TMailServer.Create(Application); oServer.Server := server; oServer.User := user; oServer.Password := password; oServer.Protocol := MailServerImap4; // Enable SSL/TLS Connection, most modern email server require SSL/TLS connection by default. oServer.SSLConnection := useSslConnection; if useSslConnection then begin // Set 993 SSL IMAP port oServer.Port := 993; end else begin // Set 143 IMAP port oServer.Port := 143; end; oClient := TMailClient.Create(Application); oClient.LicenseCode := 'TryIt'; oClient.Connect1(oServer.DefaultInterface); ShowMessage('Connected!'); // get folder list folders := oClient.GetFolderList(); // find source folder. oFolder := FindFolder('Deleted Items', folders); if oFolder = nil then raise Exception.Create('No source folder found!'); // select "source" folder, GetMailInfoList returns the mail list in selected folder. oClient.SelectFolder(oFolder); ShowMessage(oClient.SelectedFolder + ' is selected!'); infos := oClient.GetMailInfoList(); for i := 0 to infos.Count - 1 do begin oInfo := infos.Item[i]; // Generate a random file name by current local datetime, // You can use your method to generate the filename if you do not like it fileName := localInbox + '\' + oTools.GenFileName(i) + '.eml'; // Receive email from server oMail := oClient.GetMail(oInfo); ShowMessage('From: ' + oMail.From.Address + #13#10 + 'Subject: ' + oMail.Subject); // Save email to local disk oMail.SaveAs(fileName, true); // Mark email as deleted oClient.Delete(oInfo); end; // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient.Quit; except on ep:Exception do ShowMessage('Error: ' + ep.Message); end; end;

See Also

MailClient.GetMailInfos Method