Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,


While i am trying to parsing email from rediffmail then i got subject in diffrent format like "=?utf-8?B?RnJvbSBSZWRpZmY=?=" i have done some research for it but not get any satisfaction result ..
can any body know what is it and how can i convert into readable format?

original ---rediffmail mail body part is---



MSIL
Date: 16 Apr 2010 13:45:07 -0000
Message-ID: <20100416134507.21423.qmail@f4mail-234-123.rediffmail.com>
MIME-Version: 1.0
To: <s.valaki@yahoo.com>
Received: from unknown 202.131.110.147 by rediffmail.com via HTTP; 16 Apr 2010 13:45:07 -0000
X-ASG-Orig-Subj: =?utf-8?B?RnJvbSBSZWRpZmY=?=
Subject:=?utf-8?B?RnJvbSBSZWRpZmY=?=
From: "jai panchali" <jai_panchali@rediffmail.com>
Content-Type: multipart/mixed;
    boundary="=_2a4919e36fafbcc6b25b248043cf1efa"



please help about this issue...
Posted

You can use the following code which allow you decoding a MIME-encoded string

static Encoding _encoding = Encoding.ASCII;
static string MatchEvaluatorHexToString(System.Text.RegularExpressions.Match m)
{
    // Get the matched string.  
    string str = m.ToString();
    Byte[] b = new Byte[1];
    b[0] = byte.Parse(m.Groups[1].Value.Replace("=", ""), System.Globalization.NumberStyles.HexNumber);
    string str4 = _encoding.GetString(b);
    return str.Replace(m.Groups[1].ToString(), str4);
}

static string MatchEvaluatorMIMEEnc2Str(System.Text.RegularExpressions.Match m)
{
    _encoding = Encoding.GetEncoding(m.Groups[1].Value);
    if (m.Groups[2].Value == "B")
    {
        Decoder sDecoder = _encoding.GetDecoder();
        byte[] todecodeByte = Convert.FromBase64String(m.Groups[3].Value);
        int charCount = sDecoder.GetCharCount(todecodeByte, 0, todecodeByte.Length);
        char[] decodedChar = new char[charCount];
        sDecoder.GetChars(todecodeByte, 0, todecodeByte.Length, decodedChar, 0);
        string result = new String(decodedChar);
        return (result);
    }
    System.Text.RegularExpressions.Regex regexItems = new System.Text.RegularExpressions.Regex("(=[0-9a-fA-F]{2})");
    string str6 = regexItems.Replace(m.Groups[3].Value.Replace("_", " "), new System.Text.RegularExpressions.MatchEvaluator(MatchEvaluatorHexToString));
    return str6;
}

static public string Decode(string str)
{
    System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("=\\?(.+?)\\?(.+?)\\?(.+?)\\?=");
    System.Text.RegularExpressions.MatchCollection mc = regEx.Matches(str);
    return regEx.Replace(str, new System.Text.RegularExpressions.MatchEvaluator(MatchEvaluatorMIMEEnc2Str));
}




Then call the Decode method to get your decoded subject:

String decodedSubject=Decode("=?utf-8?B?RnJvbSBSZWRpZmY=?=");


Do not forget to include the "=" signs.

Hope it will help ;)

Crusty Applesniffer
 
Share this answer
 
Usually the e-mail is encoded to base64, so you can use any decode to base64 functionality. Check out Base64 Decode in C# or Java[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900