Ramin Hossaini

Extracting attachments from .EML files

Mar
15

Note: If you’re only interested in the download, scroll down to the bottom of the post.

An inconsiderate friend sent me a couple of .eml files with attachments that I had to look through. I downloaded the files and found that I had no associated application to open them. So instead of finding an application to open them, I thought I’d take a closer look at the files:

The top portion had a whole bunch of stuff I had no interest in whatsoever:

After all the HTML, I found the code for the attachment:

So I figured I just had to decode the Base64-encoded data and save it as the filename (in this case, a PDF)

The most logical thing at this point was to write my own application to do it. Just made a simple C# form with a textbox for the Base64-encoded data, a textbox for the filename to write to and a Decode button to get things going:

The Decode function is pretty simple:

1
2
3
4
5
public byte[] decode(string data)
{
    byte[] output = Convert.FromBase64String( data );
    return output;
}

So feed the function the Base64 part and it spits out the good stuff that you just write to a file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FileStream fs = new FileStream(txtFilename.Text, FileMode.Create, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
try
{
    for (int i = 0; i < decodedData.Length; i++)
    {
        writer.Write(decodedData[i]);
    }
}
finally
{
    writer.Close();
    fs.Close();
}

You can also just download the latest version of the app here: Base64 Decoder

The open file function is a bit experimental and does some .EML file clean-up.

It requires the .NET framework and no, it doesn’t come supported, and I can’t promise that I’ll continue working on it.

8 Responses to Extracting attachments from .EML files

  1. The Meddling Monk

    Thanks heaps for this. Straightforward guide and very handy little program!

  2. Thanks Ramin. Twice this Spring I’ve faced the same situation as you describe, with no handy way to open .eml files. Your code solved the problem. Thank you!

  3. Great program, better then installing outlook express or any other email client (i’am a fan of webmails such as gmail).
    Very simple, very fast. Thank You Ramin!

  4. you can share code

  5. Simple and Works Wonders. Thanks

  6. Thank you, excellent instructions. Working on me.

  7. Or 1 line …

    System.IO.File.WriteAllBytes( txtFilename.Text, Convert.FromBase64String( data ) )

Leave a Reply

Your email address will not be published. Required fields are marked *