Selects an IMAP4/MS Exchange folder to operate.
[Visual Basic] Public Sub SelectFolder( _ folder As Imap4Folder _ ) Public Async Function SelectFolderAsync( _ folder As Imap4Folder _ ) As Task
[C#] public void SelectFolder( Imap4Folder folder ); public async Task SelectFolderAsync( Imap4Folder folder );
[C++] public: Void SelectFolder( Imap4Folder* folder );
[JScript] public function SelectFolder( folder: Imap4Folder );
Parameters
Remarks
Example
[Visual Basic, C#, C++] The following sample demonstrates how to retrieve email from "Deleted Items". To get the full samples of EAGetMail, please refer to Samples section.
[VB - Retrieve email from specified 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 ' Generate an unqiue email file name based on date time. Shared Function _generateFileName(ByVal sequence As Integer) As String Dim currentDateTime As DateTime = DateTime.Now Return String.Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", New CultureInfo("en-US")), currentDateTime.Millisecond, sequence) End Function Public Sub ReceiveMail(server As String, user As String, password As String, useSsl As Boolean) 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 ' ExchangeEWS Or ExchangeWebDAV protocol also supports SelectFolder. ' 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) ' Lookup folder based on path. Dim folder As Imap4Folder = FindFolder("Deleted Items", oClient.GetFolders()) If folder Is Nothing Then Throw New Exception("Folder was not found") End If ' Select this folder oClient.SelectFolder(folder) Console.WriteLine("Retrieving email list ...") Dim infos() As MailInfo = oClient.GetMailInfos() Console.WriteLine("Total {0} email(s) in {1}", infos.Length, folder.LocalPath) For i As Integer = 0 To infos.Length - 1 Console.WriteLine("Checking {0}/{1} ...", i + 1, infos.Length) Dim info As MailInfo = infos(i) ' Generate an unqiue email file name based on date time. Dim fileName As String = _generateFileName(i + 1) Dim fullPath As String = String.Format("{0}\{1}", localInbox, fileName) Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length) Dim oMail As Mail = oClient.GetMail(info) ' Save email to local disk oMail.SaveAs(fullPath, True) ' Mark email as deleted on server. Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length) oClient.Delete(info) Next Console.WriteLine("Disconnecting ...") ' Delete method just mark the email as deleted, ' Quit method expunge the emails from server permanently. oClient.Quit() Console.WriteLine("Completed!") Catch ep As Exception Console.WriteLine("Error: {0}", ep.Message) End Try End Sub End Class
[C# - Retrieve email from specified 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; } // Generate an unqiue email file name based on date time static string _generateFileName(int sequence) { DateTime currentDateTime = DateTime.Now; return string.Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", new CultureInfo("en-US")), currentDateTime.Millisecond, sequence); } public void ReceiveMail(string server, string user, string password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory.Exists(localInbox)) { Directory.CreateDirectory(localInbox); } // ExchangeEWS Or ExchangeWebDAV protocol also supports SelectFolder. // Most modern email server 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); // find folder Imap4Folder folder = FindFolder("Deleted Items", oClient.GetFolders()); if (folder == null) { throw new Exception("Folder not found!"); } // select dest folder oClient.SelectFolder(folder); Console.WriteLine("Retreiving email list ..."); MailInfo[] infos = oClient.GetMailInfos(); Console.WriteLine("Total {0} email(s) in {1}", infos.Length, folder.LocalPath); for (int i = 0; i < infos.Length; i++) { Console.WriteLine("Checking {0}/{1} ...", i+1, infos.Length); MailInfo info = infos[i]; // Generate an unqiue email file name based on date time. string fileName = _generateFileName(i + 1); string fullPath = string.Format("{0}\\{1}", localInbox, fileName); Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length); Mail oMail = oClient.GetMail(info); // Save mail to local file oMail.SaveAs(fullPath, true); // Mark the email as deleted on server. Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length); oClient.Delete(info); } Console.WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient.Quit(); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } } }
[C++/CLI - Retrieve email from specified 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; } // Generate an unqiue email file name based on date time static String ^ _generateFileName(int sequence) { DateTime currentDateTime = DateTime::Now; return String::Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", gcnew CultureInfo("en-US")), currentDateTime.Millisecond, sequence); } void ReceiveMail(String ^server, String ^user, String ^password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. String ^localInbox = String::Format("{0}\\inbox", Directory::GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory::Exists(localInbox)) { Directory::CreateDirectory(localInbox); } // ExchangeEWS Or ExchangeWebDAV protocol also supports SelectFolder. // Most modern email server 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); // find folder Imap4Folder ^ folder = FindFolder("Deleted Items", oClient->GetFolders()); if (folder == nullptr) { throw gcnew Exception("Folder not found!"); } // select dest folder oClient->SelectFolder(folder); Console::WriteLine("Retreiving email list ..."); array<MailInfo^>^infos = oClient->GetMailInfos(); Console::WriteLine("Total {0} email(s) in {1}", infos->Length, folder->LocalPath); for (int i = 0; i < infos->Length; i++) { Console::WriteLine("Checking {0}/{1}", i + 1, infos->Length); MailInfo ^info = infos[i]; // Generate an unqiue email file name based on date time String^ fileName = _generateFileName(i + 1); String^ fullPath = String::Format("{0}\\{1}", localInbox, fileName); Console::WriteLine("Downloading {0}/{1}", i + 1, infos->Length); Mail ^oMail = oClient->GetMail(info); // Save email to local disk oMail->SaveAs(fullPath, true); // Mark email as deleted on server. Console::WriteLine("Deleting ... {0}/{1}", i + 1, infos->Length); oClient->Delete(info); } Console::WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient->Quit(); Console::WriteLine("Completed!"); } catch (Exception ^ep) { Console::WriteLine("Error: {0}", ep->Message); } }
See Also