user3776734
user3776734

Reputation: 1

VB How to extract attachments from a mime encoded text file

We use a 3rd party company for our procurement. Some POs have supporting documents like Word, Excel...etc. The company can provide us back the documents in a txt file containing the documents mime encoded. Example below.

Looking for a VB solution to read this file and extract the document(s) to disk. Any help would be greatly appreciated.

-------------------------------MIME_BOUNDARY_FOR_ATTACHMENTS
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>
Content-Disposition: attachment; filename="happy_path_reqs.png"

‰PNG
IHDR   È   ¥   2²  € IDATxÚì]`TUÖNOè`n¯ %4»»k_ËZ鮺ºÝ†kTºÒ{jX‘
µ)&ؼBßÛGËY3pV£O!@â­È(i f’刘ðî]Û³©¬@¼B;ÿš„=´†kË Mî
µ)&ؼBßÛGËY3pV£O!@â­È(i f’刘ðî]Û³©¬@¼B;ÿš„=´†kË Mî
µ)&ؼBßÛGËY3pV£O!@â­È(i f’刘ðî]Û³©¬@¼B;ÿš„=´†kË Mî
µ)&ؼBßÛGËY3pV£O!@â­È(i f’刘ðî]Û³©¬@¼B;ÿš„=´†kË Mî
-------------------------------MIME_BOUNDARY_FOR_ATTACHMENTS--

Upvotes: 0

Views: 1183

Answers (1)

user708641
user708641

Reputation:

I'm not familiar with VB.NET, but you should be able to use MimeKit to parse these MIME formatted files. I've been using it with a lot of success for quite a while now.

In C#, you would do something like this:

var message = MimeMessage.Load ("email.msg");

foreach (var attachment in message.Attachments) {
    using (var stream = File.Create (attachment.FileName))
        attachment.ContentObject.DecodeTo (stream);
}

In VB.NET, it would probably be something like this:

Dim message As MimeMessage.Load ("email.msg")

For Each attachment As MimePart In message.Attachments
    Dim stream As File.Create (attachment.FileName)
    attachment.ContentObject.DecodeTo (stream)
    stream.Dispose ()
Next

Upvotes: 1

Related Questions