简体   繁体   中英

Using mimekit.mimemessage to load eml files from hard disk how can I load only text?

This is how I'm using it today

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int counter = 0;
    MimekitallLoadedMessages = new List<MimeKit.MimeMessage>();
    MimeKit.MimeMessage loadedMessage = null;
    DirectoryInfo di = new DirectoryInfo(emailsDirectory);
    FileInfo[] files = di.GetFiles();
    for (int i = 0; i < files.Length; i++)
    {
        string uid = seenUids[0];
        loadedMessage = MimeKit.MimeMessage.Load(files[i].FullName);
        MimekitallLoadedMessages.Add(loadedMessage);
        downloaded.Add(seenUids[i]);
        counter += 1;
        int nProgress = counter * 100 / files.Length;
        backgroundWorker2.ReportProgress(nProgress);
    }
}

The method Load just load the whole message. But i wander if i can Load for exmaple on the subject of each message and teh add it to a listView for example so the user later will be able to select a specific email to load it's all content like the html or the whole body content.

So loading only the subject and make a list of all the emails in the listView will load the messages faster. I have like 6000 eml files on hard disk.

Loading all the files and add all the messages to the listView might take some time. Instead maybe loading/parsing only the text might be faster ?

Is it possible ? And logic ? Maybe when i download the messages first time i should create a text file with all the subjects of each email and then when running my program just to read the lines from the text file, each line is a subject ?

UPDATE

This is the dowork event now:

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            MimeKit.HeaderList loaded = new MimeKit.HeaderList();
            int counter = 0;
            MimekitallLoadedMessages = new List<MimeKit.MimeMessage>();
            MimeKit.MimeMessage loadedMessage = null;
            DirectoryInfo di = new DirectoryInfo(emailsDirectory);
            FileInfo[] files = di.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                string uid = seenUids[0];
                loaded = MimeKit.HeaderList.Load(files[i].FullName);
                var subject = loaded[MimeKit.HeaderId.Subject];
                downloaded.Add(seenUids[i]);
                counter += 1;
                int nProgress = counter * 100 / files.Length;
                backgroundWorker2.ReportProgress(nProgress, subject);
            }
         }

And the progresschanged event how i'm updating the listView control:

private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbt1.Value = e.ProgressPercentage;
            pbt1.Text = e.ProgressPercentage.ToString() + "%";
            pbt1.Invalidate();
            if (e.UserState != null)
            {

                ListViewCostumControl.lvnf.Items.Add(new ListViewItem(new string[]
            {
                e.UserState.ToString()
            }));
            }
         }

You can parse the headers only if you want via HeaderList.Load . This will be faster than parsing the whole message. Here is an example:

string filename = ...

var headerList = HeaderList.Load(filename);

var subject = headerList[HeaderId.Subject];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM