Thursday, October 6, 2016

Generic extension method for traversing object graphs

  1. public static IEnumerable Traverse(this T root, Func> getChildren)
  2. {
  3. return Enumerable.Repeat(root, 1)
  4. .Concat((getChildren(root) ?? Enumerable.Empty())
  5. .SelectMany(child => Traverse(child, getChildren)));
  6. }

Tuesday, August 23, 2016

Extract attachments and inline files from a MIME message

  1. MimeMessage msg;
  2. using (var stream = File.OpenRead(fileName))
  3. {
  4. msg = MimeMessage.Load(stream);
  5. }
  6. foreach (var part in msg.BodyParts.OfType().Where(part => !string.IsNullOrEmpty(part.FileName)))
  7. {
  8. using (var stream = File.OpenWrite(Path.Combine(Path.GetDirectoryName(fileName), part.FileName)))
  9. {
  10. part.ContentObject.DecodeTo(stream);
  11. }
  12. }
MimeKit