2

I am having the same problem as it is described here: MailKit save Attachments

foreach(var attachment in message.Attachments) {
  using(var stream = File.Create("fileName")) {
    if (attachment is MessagePart) {
      var part = (MessagePart) attachment;

      part.Message.WriteTo(stream);
    } else {
      var part = (MimePart) attachment;

      part.Content.DecodeTo(stream);
    }
  }

}

and on "part.Content.DecodeTo(stream);" i get an error :

MimeKit.MimePart does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'MimeKit.MimePart' could be found (are you missing a using directive or an assembly reference?)

It is driving me crazy since the only thing left for me is to save the attachment to the computer. I get the file names of the attachments, i get the correct number of attachments ...

I am not downloading emails, but doing this to an .eml file stored on computer.

Please help me out.

Thank you.

HEki
  • 43
  • 6
  • What version of MimeKit are you using? – ProgrammingLlama Sep 24 '22 at 04:52
  • `.Content` only seems to exist in MimeKit version 2 and beyond. Version 2 was released in December 2017, so surely you're not using an older version than that? In older versions it seems there's a `part.WriteTo` that _might_ do what you want, though I'm not really sure. – ProgrammingLlama Sep 24 '22 at 05:12
  • A MIME attachment is in the body of an HTML message (Request/Response). Each attachment starts with a line with two dashes. See following : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140) – jdweng Sep 24 '22 at 08:15
  • Yes, you guys were right. This library is from 2017. I have to check the rest of my code, before i can replace it with the new version. How ever. I did replace it for the current task and i got no errors. The only problem i got was that there were no files created ... I have 2 attachments in the email and the loop goes trough 2 times. No files are created. I will give it a try again tomorrow. – HEki Sep 24 '22 at 17:15
  • Still no success. What am i doing wrong? – HEki Sep 26 '22 at 05:25
  • You'd have to step through your code and check. Even without writing anything to the file, the fact that you're calling `File.Create` should product a file when the loop completes. Either you're looking in the wrong location for the created files, or the loop doesn't run (`message.Attachments` is empty). – ProgrammingLlama Sep 27 '22 at 02:29
  • 1
    Prior to MimeKit 2.0, the `Content` property was called `ContentObject` and it had the same methods on it (or, at least, the non-Async versions of the methods). – jstedfast Sep 27 '22 at 23:11
  • 1
    @jstedfast and everyone else: THANK YOU! The problem was with my "path" and the files were created in 1 directory higher because of that mistake. – HEki Oct 05 '22 at 04:03

0 Answers0