Ramin Hossaini's Blog

15Mar/11

Extracting attachments from .EML files

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.

Private
Page 1 of 11
Bear