How to get mail attachment size

0 votes
asked May 11, 2011 by Rebex KB (8,190 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 (16,920 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 http://www.rebex.net/mail.net/tutorial-imap.aspx#downloading 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.Connect("imap.example.com");
   client.Login("username", "password");

   // select working folder
   client.SelectFolder("Inbox");

   // 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);
...