0 votes

Hello,

I get email by following method in c#.net.

imapSetting.Select(item.FolderName);     //imap object and passed "Inbox"
List<long> localUids = getUIDFromDB();  //get UID of existing mail.
List<long> serverUids = imapSetting.Search(Flag.All)
//Find the MessageID which is not exists in our local database 
var allnewMessageID = serverUids.ToList().Except(localUids).ToList();
//Here we have fetch the mails which is not availabel in our local
var mailInfos = imapSetting.GetMessageInfoByUID(allnewMessageID);   

foreach (MessageInfo info in mailInfos.OrderByDescending(t => t.UID))
{
       //Insert in database
}

I have 90,000 emails in Inbox. It takes so much time to get email from server. Also it takes 200MB of my RAM.
If I have 10 account with such number of mail, my computer will hang and stop to perform as it will cross the RAM limit.

Can you please suggest me how to handle it in this situation?

by (3.0k points)

1 Answer

+2 votes
 
Best answer

First, you don't need to use GetAll: UIDs are assigned by an IMAP server in ascending order, which means you can find the highest uids in your list and ask for a range:

List<long> uids = imap.Search().Where(
    Expression.UID(Range.From(largestUID)));
uids.Remove(largestUID);

Second, page the results as you would in any other case: You're looking for the Skip and Take extension methods (Skip moves past the first N elements in the result, returning the remainder; Take returns the first N elements in the result, dropping any remaining elements):

uids.Skip(pageIndex * pageSize).Take(pageSize);

You can use this method to compute total number of pages:

int numberOfPages = (uids.Count / pageSize)
    + (uids.Count % pageSize == 0 ? 0 : 1);

It takes so much time to get email from server.

Are you sure the problem is not inserting to your database one-by-one?

by (270k points)
selected by
Hello,
Thank you for your interest.
It takes time on following line in my code.

var mailInfos = imapSetting.GetMessageInfoByUID(allnewMessageID);
Mail.dll is as fast as your network, IMAP server, and the number of data you download.
Thank you.
It is working as I expected.
...