c# - Saving attachments using MailKit library ? -


i'm trying learn how use mailkit library struggling retrieve attachments. far code open mailbox, go through each message , store data such sender, subject, body, date etc. can't deal attachments.

i have tried use other peoples solutions found on here, on github , other sites still don't understand doing in code , when come close getting solution working causes more bugs stressed , delete code. don't mean seem lazy love if explain how can achieve this. i'm trying build mail client web forms app.

below code, can see i'm clueless :)

        // open inbox folder         client.inbox.open(folderaccess.readonly, cancel.token);          //get full summary information retrieve details         var summary = client.inbox.fetch(0, -1, messagesummaryitems.full, cancel.token);         foreach (var msg in summary)         {             //this code downloaded text body             var text = msg.body bodyparttext;             //but tried altering attachments here             var attachments = msg.body bodypartbasic;              if (text == null)             {                 var multipart = msg.body bodypartmultipart;                  if (multipart != null)                 {                     text = multipart.bodyparts.oftype<bodyparttext>().firstordefault();                 }             }              if (text == null)                 continue;              //i hoped messages content dispositon not null             //and let me save attachments somewhere instead throws exceptions             //about object reference not set instance of object it's wrong             if (attachments.contentdisposition != null && attachments.contentdisposition.isattachment)             {                 //i tried same did text here , grab body part....... no                  var attachedpart = client.inbox.getbodypart(msg.index, attachments, cancel.token);             }              else              {                 //there no plan b :(             }              // download *just* text              var part = client.inbox.getbodypart(msg.index, text, cancel.token);             //cast main body text text part             textpart _body = (textpart)part; 

i'm not entirely clear on want accomplish, if want download message attachments (without downloading entire message) , save attachments file system, here's how 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 bodypartmultipart;     var basic = message.body 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);     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -