Download Attachment by Index

The following example demonstrates using aspNetIMAP to download an attachment by index.

[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();
 
//downloads the first attachment
MimeMessage attachment = mf.FetchClient.DownloadAttachment( 1,  0, IndexType.Ordinal, false );
if( attachment != null )
{
    attachment.Save( "c:\\temp" );
}
 
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()
  
 'downloads the first attachment
 Dim attachment As MimeMessage = mf.FetchClient.DownloadAttachment(1, 0, IndexType.Ordinal, False)
 If Not (attachment Is Nothing) Then
    attachment.Save("c:\temp")
 End If
  
 imap.Disconnect()
  
 'write out the log 
 Console.WriteLine(imap.Logger.ToString())
  
 Console.WriteLine("Done")
 Console.ReadLine()