How to get mail attachment size

0 votes
asked May 11, 2011 by Rebex KB (8,380 points)
edited May 11, 2011

I want to get the attachment size of a file within a message. I am unable to locate that.

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered May 11, 2011 by Jan Sotola (17,310 points)
edited May 11, 2011
Best answer

You have to retrieve the mail message using GetMailMessage() method of your Imap or Pop3 client object (see the following tutorial for more information).

The MailMessage class has a property Attachments with collection of message's attachemnts. Each Attachment has a GetContentLength() method, returning length of the attachment in bytes.

Here is a complete sample how to retrieve size of all attachment of individual mail message on IMAP server:

   // create client, connect and log in   
   Imap client = new Imap();
   client.Login("username", "password");

   // select working folder

   // get the topmost MailMessage in the folder
   MailMessage message = client.GetMailMessage(client.CurrentFolder.TotalMessageCount);

   // show count of message attachments    
   Console.WriteLine("Subject: {0} [{1} attachaments]", message.Subject, message.Attachments.Count);

   // show name and size of each attachment 
   foreach (Attachment attachment in message.Attachments)
         Console.WriteLine("\t{0} ({1}KB)", attachment.FileName, attachment.GetContentLength() / 1024);