ANPOP Developers Center > Using execIMAP4Command to manage folders and retrieve emails.
Important notice:* This product has integrated to EAGetMail POP3 Component, Please use EAGetMail POP3 Component instead of ANPOP POP3 Component.
EAGetMail Related Links:
Retrieve Email and Parse Email in Visual Basic
Retrieve Email and Parse Email in Visual C++
Retrieve Email and Parse Email in Delphi
Retrieve Email and Parse Email in VB.NET
Retrieve Email and Parse Email in C#
Retrieve Email and Parse Email in Managed C++
The POPMAIN object was built based on POP3 protocol before, although now it supports retrieving, deleting email from IMAP4 server. However, it can't implement all features of IMAP4 protocol by old methods. execIMAP4Command method helps developers to implement the all features of IMAP4 such as "Create Folder", "Delete Folder", "Move Email", "Copy Email" and etc. The following samples demonstrates the most operations in IMAP4 protocol.
Common Functions used in the following sample
[Visual Basic]
Function IsGoodResponse(ByVal s As String, ByVal id As String) As Boolean
id = id & " OK"
If InStr(1, s, id, vbTextCompare) > 0 Then
IsGoodResponse = True
Else
IsGoodResponse = False
End If
End Function
[C#]
public void VerifyResponse( string szId, string response )
{
string s;
s = szId.ToUpper();
s += " OK";
string r = response.ToUpper();
if( r.Length == 0 )
{
throw new Exception( "Networking timeout, please try it later!" );
}
if( r.IndexOf( s ) < 0 )
{
throw new Exception( response );
}
}
Query IMAP4 Folders
Protocol detail: RFC 2060 LIST and LSUB commands.
[Visual Basic]
Sub QueryFolders( imap4Server As String, _
imap4User As String, _
imap4Password As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
Dim cmdId, response, command
cmdId = "A001"
command = cmdId & " LIST """" *" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
Dim arLine
arLine = Split(response, Chr(13) & Chr(10))
Dim i, nCount
nCount = UBound(arLine)
Dim folder, s
For i = LBound(arLine) To nCount
s = Trim(arLine(i))
s = Replace(s, Chr(10), "")
s = Replace(s, Chr(13), "")
If Trim(oImap4.ParseIMAP4Response(s, 0)) = "*" Then
folder = oImap4.ParseIMAP4Response(s, 4)
folder = oImap4.DecodeIMAP4Folder(folder)
folder = Replace(folder, """", "")
MsgBox folder
End If
Next
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void QueryFolders( string imap4Server,
string imap4User,
string imap4Password )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
string cmdId, response, command;
cmdId = "A001";
command = String.Format( "{0} LIST \"\" *\r\n", cmdId );
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
string[] ar = response.Split( "\n".ToCharArray() );
for( int i = 0; i < ar.Length; i++ )
{
string s = ar[i].Trim("\r\n".ToCharArray());
if( s.Length == 0 )
continue;
if( s[0] != '*' )
continue;
string folder = oImap4.ParseIMAP4Response(s, 4);
folder = oImap4.DecodeIMAP4Folder(folder);
folder = folder.Trim( "\"".ToCharArray());
MessageBox.Show( folder );
}
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Create IMAP4 Folder
Protocol detail: RFC 2060 CREATE command.
[Visual Basic]
Sub CreateFolder( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
folder As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
Dim cmdId, response, command
cmdId = "A001"
command = cmdId & " CREATE """ & oImap4.EncodeIMAP4Folder(folder) & """" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox "OK"
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void CreateFolder( string imap4Server,
string imap4User,
string imap4Password,
string folder )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
string cmdId, response, command;
cmdId = "A001";
command = String.Format( "{0} CREATE \"{1}\"\r\n", cmdId, oImap4.EncodeIMAP4Folder(folder));
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( "OK" );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Delete IMAP4 Folder
Protocol detail: RFC 2060 DELETE command.
[Visual Basic]
Sub DeleteFolder( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
folder As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
Dim cmdId, response, command
cmdId = "A001"
command = cmdId & " DELETE """ & oImap4.EncodeIMAP4Folder(folder) & """" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox "OK"
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void DeleteFolder( string imap4Server,
string imap4User,
string imap4Password,
string folder )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
string cmdId, response, command;
cmdId = "A001";
command = String.Format( "{0} DELETE \"{1}\"\r\n", cmdId, oImap4.EncodeIMAP4Folder(folder));
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( "OK" );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Rename IMAP4 Folder
Protocol detail: RFC 2060 RENAME command.
[Visual Basic]
Sub RenameFolder( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
folder As String, newfolder As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
Dim cmdId, response, command
cmdId = "A001"
command = cmdId & " RENAME """ & oImap4.EncodeIMAP4Folder(folder) & """"
command = command & " """ & oImap4.EncodeIMAP4Folder(newfolder) & """" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox "OK"
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void RenameFolder( string imap4Server,
string imap4User,
string imap4Password,
string folder,
string newfolder )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
string cmdId, response, command;
cmdId = "A001";
command = String.Format( "{0} RENAME \"{1}\" \"{2}\"\r\n", cmdId,
oImap4.EncodeIMAP4Folder(folder),
oImap4.EncodeIMAP4Folder(newfolder));
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( "OK" );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Append Message to IMAP4 Folder
Protocol detail: RFC 2060 APPEND command.
[Visual Basic]
Sub AppendEml( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
folder As String, emlFileName As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
Dim oMsg AS ANPOPLib.POPMSG
Set oMsg = new ANPOPLib.POPMSG
If oMsg.ImportFile(emlFileName) <> 0 Then
MsgBox "Unable to open the file: " & emlFileName
GoTo ErrorHandler
End If
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
Dim cmdId, response, command
cmdId = "A001"
command = cmdId & " APPEND """ & oImap4.EncodeIMAP4Folder(folder) & """ {"
command = command & Len(oMsg.RawContent) & "}" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If InStr(response, "+") <> 1 Then
MsgBox response
GoTo ErrorHandle
End If
extId = "-A001"
command = extId & " " & oMsg.RawContent & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox "OK"
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void AppendEml( string imap4Server,
string imap4User,
string imap4Password,
string folder,
string emlFileName )
{
POPMAINClass oImap4 = new POPMAINClass();
POPMSGClass oMsg = new POPMSGClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
if( oMsg.ImportFile( emlFileName ) != 0 )
{
throw new Exception( "Unable open the selected file!" );
}
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
string cmdId, response, command;
command = String.Format( "{0} APPEND \"{1}\" {{{2}}}\r\n",
cmdId,
oImap4.EncodeIMAP4Folder(folder),
oMsg.GetMessageSize());
response = m_oImap4.execIMAP4Command( command ).Trim();
if( response.Length == 0 )
{
throw new Exception( "Networking timeout, please try it again!");
}
if( response[0] != '+' )
{
throw new Exception( response );
}
string extId;
extId = "-";
extId += cmdId;
command = extId + " " + oMsg.RawContent + "\r\n";
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( "OK" );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Query Message's IMAP4 Flags
Protocol detail: RFC 2060 FETCH command.
Note: uid parameter is returned by GetMsgID method of POPMAIN object.
[Visual Basic]
Sub QueryFlags( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
uid As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
oImap4.IMAP4Folder = "Inbox"
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
oImap4.GetTotalOfMails
Dim cmdId, response, command
cmdId = "A001"
command = cmdId & " UID FETCH " & uid & " (UID FLAGS)" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox response
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void QueryFlags( string imap4Server,
string imap4User,
string imap4Password,
string uid )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
oImap4.IMAP4Folder = "Inbox";
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
oImap4.GetTotalOfMail();
string cmdId, response, command;
cmdId = "A001";
command = String.Format( "{0} UID FETCH {1} (UID FLAGS)\r\n", cmdId, uid);
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( response );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Change Message's IMAP4 Flags
Protocol detail: RFC 2060 STORE command.
Note: uid parameter is returned by GetMsgID method of POPMAIN object.
[Visual Basic]
Sub ChangeFlags( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
uid As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
oImap4.IMAP4Folder = "Inbox"
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
oImap4.GetTotalOfMails
Dim cmdId, response, command
cmdId = "A001"
'add deleted and seen flags to this message.
command = cmdId & " UID STORE " & uid & " +FLAGS.SILENT (\Deleted \Seen)" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox "OK"
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void ChangeFlags( string imap4Server,
string imap4User,
string imap4Password,
string uid )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
oImap4.IMAP4Folder = "Inbox";
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
oImap4.GetTotalOfMail();
string cmdId, response, command;
cmdId = "A001";
//add seen and deleted flags to this message
command = String.Format( "{0} UID STORE {1} +FLAGS.SILENT (\\Deleted \\Seen)\r\n", cmdId, uid);
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( "OK" );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Copy Message
Protocol detail: RFC 2060 COPY command.
Note: uid parameter is returned by GetMsgID method of POPMAIN object.
[Visual Basic]
Sub CopyMessage( imap4Server As String, _
imap4User As String, _
imap4Password As String, _
uid As String,
targetFolder As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
oImap4.IMAP4Folder = "Inbox"
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password )
If nRet <> 0 Then
MsgBox "error with connecting server"
GoTo ErrorHandler
End If
oImap4.GetTotalOfMails
Dim cmdId, response, command
cmdId = "A001"
'copy this message from inbox to targetfolder
command = cmdId & " UID COPY " & uid & " """ & _
oImap4.EncodeIMAP4Folder(targetFolder) & """" & Chr(13) & Chr(10)
response = Trim(oImap4.execIMAP4Command(command))
If response = "" Then
MsgBox "Networking timeout, please try it later!"
GoTo ErrorHandle
End If
If Not IsGoodResponse(response, cmdId) Then
MsgBox response
GoTo ErrorHandle
End If
MsgBox "OK"
ErrorHandler:
Call oImap4.Close()
Set oImap4 = Nothing
End Sub
[C#]
public void CopyMessage( string imap4Server,
string imap4User,
string imap4Password,
string uid,
string targetFolder )
{
POPMAINClass oImap4 = new POPMAINClass();
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
oImap4.IMAP4Folder = "Inbox";
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password );
if( nRet != 0 )
throw new Exception( "error with Connect" );
oImap4.GetTotalOfMail();
string cmdId, response, command;
cmdId = "A001";
//copy this message from "inbox" to targetFolder
command = String.Format( "{0} UID COPY {1} \"{2}\"\r\n",
cmdId,
uid,
oImap4.EncodeIMAP4Folder(targetFolder));
response = oImap4.execIMAP4Command( command ).Trim();
VerifyResponse( cmdId, response );
MessageBox.Show( "OK" );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
oImap4.Close();
oImap4 = null;
}
Samples
Please refer to IMAP4Folder, IMAP4FolderCSharp in ANPOP installation package.
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is likely
that your smtp server bounced our reply. In this case, please try to use another
email address to contact us. Your Hotmail or Yahoo email account is recommended.
|