Need a fuction to avoid strange characters in e-mail

I'm writing an application where POP mail is one functionality. Not a big problem, but the mail including headers comes as quoted_printable, which means extended ASII characters are displayed like =E9 etc. Does anybody know of a method to parse e-mail messages and convert these codes to the correct characters

Answer this question

Need a fuction to avoid strange characters in e-mail

  • Ido F.

    As far as I know, there is no native .NET support for Quoted_Printable.  You would have to either parse the text by hand or use a third-party component to do it (there are some out there -- a quick Google search will find a couple).

    -Ari

  • Janice_777

    To moderator: I am an Asp.Net moderator and would as such appreciate if I could edit my postings, hence I would like to be unmoderated if possible.
  • Seyfert

    What are you encoding from/to   (I used two encodings picked at random for my illustration. :-))

    -Ari

  • Aknght

    I am writing POP-mail functionality into my application. When retrieving the raw mail message from server all text is served as a particular Mime type called Quoted_Printable, which means the ordinary ASCII character set. To display other character sets in a readable fashion, the message will have to be converted from Quoted_Printable text to 8bit, if not, the extended ASCII characters will come out as escape sequences (ofter 3 letters looking like this: =E9 as 'ö' e.g.), which make the the mail to/from, subject and body unreadable. If you want to see some code probably doing what I want, I have found some code written i Python at this address: <a href="http://web.pydoc.org/get.cgi/usr/lib/python2.1/mimify.py">http://web.pydoc.org/get.cgi/usr/lib/python2.1/mimify.py</a>. This might give you a better understanding of what I'm after.

    I have tried to accomplish what I'm after by using Regular Expressions, but it is not an optimal solution as the escape codes changes depending on whether the character is sent as a Stand Alone character or within a word. RegEX will have to be used for cleaning up the messages, but I need help with the rest.

  • kritchai vanchainavin

    Have you looked at System.Text.Encoding   There's a Convert() function in there that will convert a byte array of one encoding type to another.  So you can do something like this:

    using System.Text;

    ... ' Normal declarations and stuff

    public string ConvertString(string s)
    {
       ' Create the encodings
       Encoding ucode = Encoding.Unicode;
       Encoding ascii = Encoding.ASCII;

       ' Convert() takes a byte array, so convert the string to one
       byte[] beforeEncoding = ucode.GetBytes(s);

       ' Do the actual encoding conversion
       byte[] afterEncoding = Encoding.Convert(ucode, ascii, beforeEncoding);

       ' Return it to a string
       return ascii.GetString(afterEncoding);
    }


    Hope this helps!

    -Ari

  • Raja Ratheesh

    Thanks for the suggestion Ari. I converted the Code to VB.Net and tried (I had tried using Encoding before actually), but to no avail. My name, <b>André Colbiörnsen</b>, came out like this:

    = iso-8859-1 Q Andr=E9_Colbi=F6rnsen =

    And the subject, <b>Cola®</b>, like this

    = iso-8859-1 B Q29sYa4= = 

    Any other suggestions

  • Need a fuction to avoid strange characters in e-mail