How to decode mime attachment


  • Hello all,
    I want to a) download an email message from an IMAP server, then decode the attachment (.WAV file in this case). The problem is the MIME message is sometimes multipart, sometime not. The encoding is base64 so far; that is, I have not seen other encoding, but I cannot be so sure.

    My question is, is there any free-license code to do this out there? I really hate to reinvent the wheel. If not, then I have to roll my own and here is my rough algorithm:

    Locate the "Content-Type"
    If content type is multipart
        Loop until the end of input
            Skip to the beginning of the next part (including the first part
            Decode the wav file
            If succeeded: return
        End loop
        Out of the loop and have not found .wav attachment: throw exception
    else (deals with single part)
        Decode the wav file
    End if

    // Function to decode .wav file
    Read a line from the input
    While line != empty && not end of input
        If line contains the Content-Type tag
            If the content type is not audio/wave: throw exception
        If the line contains the encoding AND encoding != base64: throw exception
    End while

    // Gets here means we encountered an empty line or end of input
    If end of input: throw exception

    Read to the end of the MIME part and decode using Convert.FromBase64String()

    Please take a look at my pseudo code and post your comments, suggestions, or corrections; I appreciate your time.

    - Hai
    • Edited by HaiVu Tuesday, September 23, 2008 4:04 PM
    Tuesday, September 23, 2008 3:55 PM

All replies

  • HaiVu;
     Please forgive me if I'm not right on these ideas.  I have not ever done work with multipart mime so here's how I'd do it as a novice...

    1) Find the boundaries of the mulipart mime content that clearly tell me it is a .wav
    2) Somehow append all of the parts of the wav into a single stream of bytes (decoding as needed to the proper format).
    3) Saving the file as a .wav file so that it is associated with the proper programs.

    P.S. I did have success writing an application once that could take streaming data and save it into playable wav files.  The concepts for that application are very similar to what you're doing here. 

    Here's a reference for you, look down the page a bit to see the multipart mime discussion.
    Thursday, September 25, 2008 3:43 AM
  • There is one great article for email client with MIME Support. Although it's downloads as a POP3 but MIME code in that article could be helpful to you.


    Gaurav Khanna
    Thursday, September 25, 2008 3:56 AM