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

No comments: