How can I get the attachment content when using MimeKit? This is what I have:
var mimeMessage = MimeMessage.Load(@"test.eml");
var attachments = mimeMessage.Attachments.ToList();
foreach (var attachment in attachments)
{
// how do I get the content here (array of bytes or stream)
}
This should do what you need:
var mimeMessage = MimeMessage.Load(@"test.eml");
var attachments = mimeMessage.Attachments.ToList();
foreach (var attachment in attachments)
{
using (var memory = new MemoryStream ())
{
if (attachment is MimePart)
((MimePart) attachment).Content.DecodeTo (memory);
else
((MessagePart) attachment).Message.WriteTo (memory);
var bytes = memory.ToArray ();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With