8

我正在尝试学习如何使用 MailKit 库,但我正在努力检索附件。到目前为止,我的代码将打开一个邮箱,浏览每条消息并存储发件人、主题、正文、日期等数据,但我无法处理附件。

我曾尝试使用在此处、github 和其他网站上找到的其他人的解决方案,但我仍然不明白他们在他们的代码中做了什么,当我接近让解决方案正常工作时,它会导致更多错误,所以我感到压力并删除所有代码。我并不是要显得懒惰,但如果有人能解释我如何做到这一点,我会很高兴。我基本上是在尝试为 Web 表单应用程序构建邮件客户端。

下面是我的代码,所以你可以看到我相当无能:)

        // Open the Inbox folder
        client.Inbox.Open(FolderAccess.ReadOnly, cancel.Token);

        //get the full summary information to retrieve all details
        var summary = client.Inbox.Fetch(0, -1, MessageSummaryItems.Full, cancel.Token);
        foreach (var msg in summary)
        {
            //this code originally downloaded just the text from the body
            var text = msg.Body as BodyPartText;
            //but I tried altering it so that it will get attachments here also
            var attachments = msg.Body as BodyPartBasic;

            if (text == null)
            {
                var multipart = msg.Body as BodyPartMultipart;

                if (multipart != null)
                {
                    text = multipart.BodyParts.OfType<BodyPartText>().FirstOrDefault();
                }
            }

            if (text == null)
                continue;

            //I hoped this would get the messages where the content dispositon was not null
            //and let me do something like save the attachments somewhere but instead it throws exceptions
            //about the object reference not set to an instance of the object so it's very wrong
            if (attachments.ContentDisposition != null && attachments.ContentDisposition.IsAttachment)
            {
                //I tried to do the same as I did with the text here and grab the body part....... but no 
                var attachedpart = client.Inbox.GetBodyPart(msg.Index, attachments, cancel.Token);
            }

            else 
            {
                //there is no plan b :(
            }

            // this will download *just* the text 
            var part = client.Inbox.GetBodyPart(msg.Index, text, cancel.Token);
            //cast main body text to Text Part
            TextPart _body = (TextPart)part;
4

2 回答 2

16

我并不完全清楚您想要完成什么,但如果您只想下载邮件附件(而不下载整个邮件)并将这些附件保存到文件系统,您可以通过以下方式完成此操作:

var messages = client.Inbox.Fetch (0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId);
int unnamed = 0;

foreach (var message in messages) {
    var multipart = message.Body as BodyPartMultipart;
    var basic = message.Body as BodyPartBasic;

    if (multipart != null) {
        foreach (var attachment in multipart.BodyParts.OfType<BodyPartBasic> ().Where (x => x.IsAttachment)) {
            var mime = (MimePart) client.Inbox.GetBodyPart (message.UniqueId.Value, attachment);
            var fileName = mime.FileName;

            if (string.IsNullOrEmpty (fileName))
                fileName = string.Format ("unnamed-{0}", ++unnamed);

            using (var stream = File.Create (fileName))
                mime.ContentObject.DecodeTo (stream);
        }
    } else if (basic != null && basic.IsAttachment) {
        var mime = (MimePart) client.Inbox.GetBodyPart (message.UniqueId.Value, basic);
        var fileName = mime.FileName;

        if (string.IsNullOrEmpty (fileName))
            fileName = string.Format ("unnamed-{0}", ++unnamed);

        using (var stream = File.Create (fileName))
            mime.ContentObject.DecodeTo (stream);
    }
}
于 2014-06-28T01:13:29.940 回答
3

另一种对我有用的替代方法,但似乎更简单一些:

var messages = client.Inbox.Fetch (0, -1, MessageSummaryItems.Full | MessageSummaryItems.BodyStructure | MessageSummaryItems.UniqueId);
int unnamed = 0;

foreach (var message in messages) {
    foreach (var attachment in message.Attachments) {
        var mime = (MimePart) client.Inbox.GetBodyPart (message.UniqueId.Value, attachment);
        var fileName = mime.FileName;

        if (string.IsNullOrEmpty (fileName))
            fileName = string.Format ("unnamed-{0}", ++unnamed);

        using (var stream = File.Create (fileName))
            mime.ContentObject.DecodeTo (stream);
    }
}

请注意,这是在 Fetch 语句中要求使用 BODYSTRUCTURE 而不是 BODY,这似乎解决了附件未被标记为此类的问题。

于 2016-10-27T10:28:36.523 回答