简体   繁体   English

根据主题中的编号下载附件(MailKit库)

[英]Download attachments according to number in subject (MailKit library)

I'm downloading attachments from e-mail with this code: 我正在使用以下代码从电子邮件中下载附件:

int count = client.Count();
List<MimeMessage> allMessages = new List<MimeMessage>(count);
for (int i = 0; i < count; i++)
{
    allMessages.Add(client.GetMessage(i));
    foreach (var attachment in allMessages[i].Attachments)
    {
        using (var stream = File.Create(AppDomain.CurrentDomain.BaseDirectory + "/folderForSegments/" + attachment.ContentType.Name))
        {
            if (attachment is MessagePart)
            {
                var part = (MessagePart)attachment;
                part.Message.WriteTo(stream);
            }
            else
            {
                var part = (MimePart)attachment;
                part.ContentObject.DecodeTo(stream);
            }
        }
    }
}

It works perfect but I want download attachments in sequence, according to number in subject . 它工作正常,但我想按主题中的编号顺序下载附件 For example: if my inbox looks like this 例如:如果我的收件箱是这样的

在此处输入图片说明 attachments will be saved on my disc in order: 6, 8, 7, 3, 2... I want save attachments in order: 1, 2, 3, 4, 5... How can I do this? 附件将按以下顺序保存在光盘上:6,8,7,3,2 ...我要按顺序保存附件:1,2,3,4,5 ...我该怎么做?

For POP3, there's no way to download the messages in that order without knowing ahead of time what order the messages were in on the server. 对于POP3,如果不提前知道服务器上邮件的顺序,就无法按该顺序下载邮件。

If order is more important than wasted bandwidth, you could download the headers first using client.GetHeader(i) for each message so that you can use the Subject header value to determine the order, but that's a lot of wasted bandwidth because you'd just end up downloading the message headers a second time when you downloaded the messages. 如果顺序比浪费的带宽更为重要,则可以首先使用client.GetHeader(i)为每条消息下载标题,以便可以使用Subject标题值来确定顺序,但这会浪费大量带宽,因为只是在下载邮件时第二次下载邮件头。

Another option is to download all of the messages, add them to a List<T> and then sort them based on Subject before iterating over the messages and saving the attachments, but this might use too much RAM depending on how large your messages are. 另一种选择是下载所有邮件,将它们添加到List<T> ,然后在遍历邮件并保存附件之前根据Subject对它们进行排序,但这可能会占用过多的RAM,具体取决于邮件的大小。

Edit: 编辑:

For IMAP, assuming your server supports the SORT extension, you can do something like this: 对于IMAP,假设您的服务器支持SORT扩展,则可以执行以下操作:

if (client.Capabilities.HasFlag (ImapCapabilities.Sort)) {
    var query = SearchQuery.SubjectContains ("damian_mistrz_");
    var orderBy = new OrderBy[] { OrderBy.Subject };
    foreach (var uid in folder.Sort (query, orderBy) {
        var message = folder.GetMessage (uid);

        // save attachments...
    }
}

If your server does not support SORT, then you could probably do something like this: 如果您的服务器支持排序,那么你很可能做这样的事情:

var query = SearchQuery.SubjectContains ("damian_mistrz_");
var orderBy = new OrderBy[] { OrderBy.Subject };
var uids = folder.Search (query);
var items = folder.Fetch (uids, MessageSummaryItems.Envelope | MessageSummaryItems.UniqueId);

items.Sort (orderBy);

foreach (var item in items) {
    var message  = folder.GetMessage (item.UniqueId);

    // save the attachments...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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