Decode IMAP4 folder name with UTF-7.
[Syntax] C++: HRESULT DecodeIMAP4Folder( BSTR Folder, BSTR* pVal ) Visual Basic: DecodeIMAP4Folder( Folder As String ) As String C#: string DecodeIMAP4Folder( string Folder )
Parameters
Folder
Folder name to decode.
Return Value
This method returns the string decoded by UTF-7.
Remarks
Based on IMAP4 protocol, the folder(mailbox) name with NON-ASCII characters needs to be encoded by UTF-7. With execIMAP4Command method, every folder name should be encoded by EncodeIMAP4Folder at first; and the result of execIMAP4Command method should be decoded by DecodeIMAP4Folder as well.
For IMAP4Folder property, the user should not use EncodeIMAP4Folder or DecodeIMAP4Folder methods, ANPOP object will encode/decode it automatically.
Usage Example
[Visual Baisc]
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
Sub QueryFolders( imap4Server As String, imap4User As String, imap4Password As String )
Dim oImap4 As ANPOPLib.POPMAIN
Set oImap4 = new ANPOPLib.POPMAIN 'Create object instance
oImap4.IMAP4Connection = 1
oImap4.ServerPort = 143
nRet = oImap4.Connect( imap4Server, imap4User, imap4Password ) 'Connect IMAP4 server
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() 'Close connection
Set oImap4 = Nothing
End Sub
[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 );
}
}
public void QueryFolders( string imap4Server, string imap4User, string imap4Password )
{
POPMAINClass oImap4 = new POPMAINClass(); //Create object instance
int nRet = 0;
try
{
oImap4.IMAP4Connection = 1;
oImap4.ServerPort = 143;
nRet = oImap4.Connect( imap4Server,
imap4User,
imap4Password ); //Connect IMAP4 server
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());
Console.WriteLine( folder );
}
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
oImap4.Close(); //Close connection
oImap4 = null;
}
See Also
EncodeIMAP4Folder Method
execIMAP4Command Method
ParseIMAP4Response Method
2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.