BestPractice to show attachment info for N Messages

Hello,

currently we implement a IMAP Mail client using Aspose.
We want to show the attachment info (name, size, …) of every e-Mail.
For example 150 Mails takes 20 seconds to get the messages with attachment info.
Without getting the attachment info it takes 1-2 seconds.
The FetchMessage method takes 7 seconds (!!!) for a Mail with 3 small attachments!

We implement your mentoined solution: How to read or download the attachments from mail box using aspose IMAP api - #2 by kashif.iqbal

Is there a better (FASTER) way to get mails with attaechment information??

Kind regards,
Andy

@AStelzner

ImapQueryBuilder class represents the builder of search expression that used by IMAP protocol. We suggest you please use ImapQueryBuilder.CustomSearch method to achieve your requirement. Hope this helps you.

Sorry, but this does not help :blush:

I try the following and filter messages by size >0 (nearly all), but i cannot find any attachment information!

var connection = _imapClient.CreateConnection();

ImapQueryBuilder builder = new ImapQueryBuilder();

builder.MessageSize.Greater(0);

MailQuery query = builder.GetQuery();

ImapMessageInfoCollection messageColl = _imapClient.ListMessages(query);

foreach (var message in messageColl)

{

// no attachment info in message object!!!

}

Please provide an example how to get attachment infos (size, name, type, …) using your proposal.

Kind regards,

Andy

dfX3_fdc7b91e-6c57-4128-92c2-5b9fbad02274.png (1.42 KB)

QR8c341bc8-75b7-4e2c-99c3-2d2ba4031ede.png (458 Bytes)

@AStelzner

We are working over your query and will get back to you soon.

@AStelzner

Following code snippet shows how to search messages with attachments. Please use ImapQueryBuilder.CustomSearch method as shown below. Hope this helps you.

using (ImapClient client = new ImapClient("ImapUrl", 143, "EMail", "Password"))
{
    int cnt = 10;
    for (int i = 0; i < cnt; i++)
    {
        MailMessage message = new MailMessage(
            user.EMail,
            user.EMail,
            $"EMAILNET-40333 - {g} - {i}",
            "EMAILNET-40333 Filter messages containing attachments through ImapClient");
        int j = i % 2;
        if (j == 0)
            message.Attachments.Add(new Attachment(new MemoryStream(new byte[1024]), $"attachment - {i}"));
        client.AppendMessage(message);
    }
    ImapMessageInfoCollection messageInfoCol = null;
    switch (serverType)
    {
        case "Gmail":
            {
                ImapQueryBuilder builder = new ImapQueryBuilder();
                builder.CustomSearch("X-GM-RAW \"has:attachment\"");
                MailQuery query = builder.GetQuery();
                messageInfoCol = client.ListMessages(query);
            }
            break;
        default: // Normal servers
            {
                ImapQueryBuilder builder = new ImapQueryBuilder();
                builder.HasHeader("Content-Type", "multipart/mixed");
                MailQuery query = builder.GetQuery();
                messageInfoCol = client.ListMessages(query);
            }
            break;
    }
                 
    foreach (var mi in messageInfoCol)
    {
        MailMessage mm = client.FetchMessage(mi.SequenceNumber);
                    
    }
}

First of all, thank you for the example.

Unfortunately, that doesn’t solve our problem.

We want to display a list (ALL, not filtered) of emails and their attachment information (name and size). But if an email with large attachments is underneath it takes a very long time, only to SHOW the information. With a mailbox with over 1000 messages and some emails with attachments, it takes so long that you can get a coffee.

Test this code with a Email with 3 Attachments with minimum of 5 MB and meassure the time.

Kind Regards,
Andy

@AStelzner

We have logged an investigation ticket in our issue tracking system as EMAILNET-40525. We will inform you via this forum thread once there is an update available on it.

We apologize for your inconvenience.

@AStelzner

A feature has been added to get information about attachments such as name, size without fetching the attachment data.

Changes in public API

Aspose.Email.Clients.Imap.ImapAttachmentInfo - Represents an attachment information.
Aspose.Email.Clients.Imap.ImapAttachmentInfoCollection - Represents a collection of ImapAttachmentInfo.
Aspose.Email.Clients.Imap.ListAttachments(int sequenceNumber) - Gets an information for each attachment in message.

Code sample

var messageInfoCollection = imapClient.ListMessages();

foreach (var message in messageInfoCollection)
{
    var attachmentInfoCollection = imapClient.ListAttachments(message.SequenceNumber);

    foreach (var attachmentInfo in attachmentInfoCollection)
    {
        Console.WriteLine("Attachment: {0} (size: {1})", attachmentInfo.Name, attachmentInfo.Size);
    }
}