Thursday, October 6, 2016

Generic extension method for traversing object graphs

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

Tuesday, August 23, 2016

Extract attachments and inline files from a MIME message

MimeMessage msg;
using (var stream = File.OpenRead(fileName))
{
    msg = MimeMessage.Load(stream);
}
foreach (var part in msg.BodyParts.OfType().Where(part => !string.IsNullOrEmpty(part.FileName)))
{
    using (var stream = File.OpenWrite(Path.Combine(Path.GetDirectoryName(fileName), part.FileName)))
    {
        part.ContentObject.DecodeTo(stream);
    }
}
MimeKit