节约使用MailKit库的附件? [英] Saving attachments using MailKit library ?

查看:788
本文介绍了节约使用MailKit库的附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用MailKit库,但我在努力找回附件。到目前为止,我的code将打开邮箱,通过每个消息和存储数据,如发件人,主题,正文,日期等。但我不能带附件的处理。

我曾尝试使用在这里找到其他解决方案的人民,在GitHub等网站,但我还是不明白,究竟他们在code在做什么,当我接近得到一个解决方案工作会引起更多的错误,所以我感到压力,并删除所有code。我的意思不是显得懒散,但我很乐意,如果有人可以解释我怎么能做到这一点。基本上,我试图建立一个邮件客户端的web窗体应用程序。

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

  //打开收件箱文件夹
        client.Inbox.Open(FolderAccess.ReadOnly,cancel.Token);        //获取完整的摘要信息检索的所有细节
        变种摘要= client.Inbox.Fetch(0,-1,MessageSummaryItems.Full,cancel.Token);
        的foreach(在汇总VAR MSG)
        {
            //这个code最初是从身体下载只是文本
            VAR文本= msg.Body为BodyPartText;
            //但我想改变它,这样它会得到这里的附件也
            VAR附件= msg.Body为BodyPartBasic;            如果(文字== NULL)
            {
                VAR多部分= msg.Body为BodyPartMultipart;                如果(多部分!= NULL)
                {
                    文= multipart.BodyParts.OfType< BodyPartText>()FirstOrDefault()。
                }
            }            如果(文字== NULL)
                继续;            //我希望这会得到的消息,其中内容dispositon不是空
            //让我做类似的地方保存附件,而是它抛出异常
            //有关对象引用未设置为对象的实例,所以它是非常错误的
            如果(attachments.ContentDisposition = NULL&放大器;!&安培; attachments.ContentDisposition.IsAttachment)
            {
                //我试图做同样的像我一样的文字在这里和抓住身体的一部分.......但没有
                VAR attachedpart = client.Inbox.GetBodyPart(msg.Index,附件,cancel.Token);
            }            其他
            {
                //没有B计划:(
            }            //这将下载* *只是文本
            VAR的部分= client.Inbox.GetBodyPart(msg.Index,文字,cancel.Token);
            //投主体文本文本部分
            TextPart _body =(TextPart)的一部分;


解决方案

我不是你想要完成的任务完全清楚,但如果你只是想下载邮件附件(无需下载整个邮件),并保存这些附件文件系统,这里是你如何能做到的是:

  VAR消息= client.Inbox.Fetch(0,-1,MessageSummaryItems.Full | MessageSummaryItems.UniqueId);
INT无名= 0;的foreach(在邮件中VAR消息){
    VAR多部分= message.Body为BodyPartMultipart;
    VAR基本= message.Body为BodyPartBasic;    如果(多部分!= NULL){
        的foreach(VAR附件中multipart.BodyParts.OfType< BodyPartBasic>()。凡(X => x.IsAttachment)){
            VAR哑剧=(MimePart)client.Inbox.GetBodyPart(message.UniqueId.Value,附件);
            变种文件名= mime.FileName;            如果(string.IsNullOrEmpty(文件名))
                文件名=的String.Format(unnamed- {0},++未命名);            使用(VAR流= File.Create(文件名))
                mime.ContentObject.De codeTO(流);
        }
    }否则如果(基本= NULL&放大器;!&安培; basic.IsAttachment){
        VAR哑剧=(MimePart)client.Inbox.GetBodyPart(message.UniqueId.Value,基本);
        变种文件名= mime.FileName;        如果(string.IsNullOrEmpty(文件名))
            文件名=的String.Format(unnamed- {0},++未命名);        使用(VAR流= File.Create(文件名))
            mime.ContentObject.De codeTO(流);
    }
}

I'm trying to learn how to use the MailKit library but I am struggling to retrieve attachments. So far my code will open a mailbox, go through each message and store data such as sender, subject, body, date etc. but I can't deal with attachments.

I have tried to use other peoples solutions found on here, on github and other sites but I still don't understand exactly what they are doing in their code and when I come close to getting a solution working it causes more bugs so I get stressed and delete all the code. I don't mean to seem lazy but I would love if somebody could explain how I can achieve this. I'm basically trying to build a mail client for a web forms app.

Below is my code, so as you can see I'm fairly clueless :)

        // 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;

解决方案

I'm not entirely clear on what you want to accomplish, but if you just want to download the message attachments (without downloading the entire message) and save those attachments to the file system, here's how you can accomplish that:

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);
    }
}

这篇关于节约使用MailKit库的附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆