Downloading a BodySection

The following example demonstrates using aspNetIMAP to download a specific section of a message.

BodySections can either be specified by one of the FectClient.BodySection strings, or else by the BodyStructure.BodySectionId property. This is helpful in retrieving specific parts of the message, without having to download the entire message.

The BodyStructure object is a class that describes the format of a mime message, on an IMAP server without actually having to download the entire message. The message is parsed on the server, and its hierarchy is sent back to the client. Because Mime messages are nested, a BodyStructure can also contain other BodyStructures. To find or inspect child BodyStructures, check out BodyStructure.Sections.

Although there are exceptions, each BodyStructure can be considered to map to a Mime message body part. For example, if a message contains a text/plain and a text/html part, there are actually three BodyStructures; the BodyStructure of the overall message, the BodyStructure of the text/plain part, and the BodyStructure of the text/html part.

Because the IMAP protocol is complicated, we found numerous parsing errors and exceptions when testing against various IMAP servers. Although we have tried to compensate for these problems, we are sure many more still exist. Because mail server companies focus on transporting mail, and not parsing it, we found their server side parsing implementations to be generally week. If you happen to find a bodystructure that does not "seem quite right", feel free to email us the IMAP log, for further inspection.

The following example locates the first plain text formatted part of a message, and downloads it.

[C#]

IMAP4 imap = new IMAP4( "127.0.0.1", "dave@blah.com", "test"  );  
 
//set some logging properties  
imap.Logger = new IMAPLog(); 
imap.Logger.Overwrite = true; 
imap.Logger.InMemory = true; 
imap.Logger.Path = "c:\\imap.log"; 
 
imap.Login();  
 
MailFolder mf = imap.SelectInbox();
 
//grab the body structure
BodyStructure[] structures = mf.FetchClient.BodyStructure(1).ToArray();
 
//loop through the body structure, and, as an example, find the first text/plain part, and display it
//this could easily be done with a html part, attachments, embedded parts, or forward messages.
//the biggest limitation is the capability of the remote IMAP server.
bool foundText = false;
 
foreach( BodyStructure bs in structures )
{
    if( (bs.ContentType != null ) && ( bs.ContentType.ToLower() == "text/plain" ) )
    {
        string section = bs.BodySectionId;
        string text = mf.FetchClient.BodySection(1, section );
        Console.WriteLine( text );
        foundText = true;
        break;
    }
}
 
if( !foundText )
{
    Console.WriteLine( "this message did not have a plain/text part, as specified by the IMAP server." );
}
 
 
imap.Disconnect();  
 
//write out the log 
Console.WriteLine( imap.Logger.ToString() ); 
 
Console.WriteLine( "Done" );  
Console.ReadLine();

 

[VB.NET]

Dim imap As New IMAP4("127.0.0.1", "dave@blah.com", "test")
 
'set some logging properties  
imap.Logger = New IMAPLog()
imap.Logger.Overwrite = True
imap.Logger.InMemory = True
imap.Logger.Path = "c:\imap.log"
 
imap.Login()
 
Dim mf As MailFolder = imap.SelectInbox()
 
'grab the body structure
Dim structures As BodyStructure() = mf.FetchClient.BodyStructure(1).ToArray()
 
'loop through the body structure, and, as an example, find the first text/plain part, and display it
'this could easily be done with a html part, attachments, embedded parts, or forward messages.
'the biggest limitation is the capability of the remote IMAP server.
Dim foundText As Boolean = False
 
Dim bs As BodyStructure
For Each bs In  structures
   If Not (bs.ContentType Is Nothing) Then 
    IF bs.ContentType.ToLower() = "text/plain" Then
      Dim section As String = bs.BodySectionId
      Dim [text] As String = mf.FetchClient.BodySection(1, section)
      Console.WriteLine([text])
      foundText = True
      Exit For 
    End If   
   End If
Next bs
 
If Not foundText Then
   Console.WriteLine("this message did not have a plain/text part, as specified by the IMAP server.")
End If
 
 
imap.Disconnect()
 
'write out the log 
Console.WriteLine(imap.Logger.ToString())
 
Console.WriteLine("Done")
Console.ReadLine()