/// <summary> /// Decode the attachment to bytes /// </summary> /// <returns>Decoded attachment bytes</returns> public byte[] DecodedAsBytes() { if (_rawAttachment == null) { return(null); } if (_contentFileName != "") { byte [] decodedBytes = null; if (_contentType != null && _contentType.ToLower() == "message/rfc822".ToLower()) { decodedBytes = Encoding.Default.GetBytes(Utility.DecodeText(_rawAttachment)); } else if (_contentTransferEncoding != null) { string bytContent = _rawAttachment; if (!IsEncoding("7bit")) { if (IsEncoding("8bit") && _contentCharset != null & _contentCharset != "") { bytContent = Utility.Change(bytContent, _contentCharset); } if (Utility.IsQuotedPrintable(_contentTransferEncoding)) { decodedBytes = Encoding.Default.GetBytes(DecodeQP.ConvertHexContent(bytContent)); } else if (IsEncoding("8bit")) { decodedBytes = Encoding.Default.GetBytes(bytContent); } else { decodedBytes = Convert.FromBase64String(Utility.RemoveNonB64(bytContent)); } } else { decodedBytes = Encoding.Default.GetBytes(bytContent); } } else if (_contentCharset != null) { decodedBytes = Encoding.Default.GetBytes(Utility.Change(_rawAttachment, _contentCharset)); //Encoding.Default.GetString(Encoding.GetEncoding(_contentCharset).GetBytes(_rawAttachment)); } else { decodedBytes = Encoding.Default.GetBytes(_rawAttachment); } return(decodedBytes); } else { return(null); } }
/// <summary> /// Decode the attachment to text /// </summary> /// <returns>Decoded attachment text</returns> public string DecodeAsText() { string decodedAttachment = null; try { if (_contentType.ToLower() == "message/rfc822".ToLower()) { decodedAttachment = Utility.DecodeText(_rawAttachment); } else if (_contentTransferEncoding != null) { decodedAttachment = _rawAttachment; if (!IsEncoding("7bit")) { if (IsEncoding("8bit") && _contentCharset != null & _contentCharset != "") { decodedAttachment = Utility.Change(decodedAttachment, _contentCharset); } if (Utility.IsQuotedPrintable(_contentTransferEncoding)) { decodedAttachment = DecodeQP.ConvertHexContent(decodedAttachment); } else if (IsEncoding("8bit")) { // Commented out by Hugh Hall to eliminate compile time warning. //decodedAttachment=decodedAttachment; } else { decodedAttachment = Utility.deCodeB64s(Utility.RemoveNonB64(decodedAttachment)); } } } else if (_contentCharset != null) { decodedAttachment = Utility.Change(_rawAttachment, _contentCharset); //Encoding.Default.GetString(Encoding.GetEncoding(_contentCharset).GetBytes(_rawAttachment)); } else { decodedAttachment = _rawAttachment; } } catch { decodedAttachment = _rawAttachment; } return(decodedAttachment); }
/// <summary> /// Decode text /// </summary> /// <param name="strText">Source text</param> /// <returns>Decoded text</returns> public static string DecodeText(string strText) { /*try * { * string strRet=""; * string strBody=""; * MatchCollection mc=Regex.Matches(strText,@"\=\?(?<Charset>\S+)\?(?<Encoding>\w)\?(?<Content>\S+)\?\="); * * for(int i=0;i<mc.Count;i++) * { * if(mc[i].Success) * { * strBody=mc[i].Groups["Content"].Value; * * switch(mc[i].Groups["Encoding"].Value.ToUpper()) * { * case "B": * strBody=deCodeB64s(strBody,mc[i].Groups["Charset"].Value); * break; * case "Q": * strBody=DecodeQP.ConvertHexContent(strBody);//, m.Groups["Charset"].Value); * break; * default: * break; * } * strRet+=strBody; * } * else * { * strRet+=mc[i].Value; * } * } * return strRet; * } * catch * {return strText;}*/ try { string strRet = ""; string[] strParts = Regex.Split(strText, "\r\n"); string strBody = ""; const string strRegEx = @"\=\?(?<Charset>\S+)\?(?<Encoding>\w)\?(?<Content>\S+)\?\="; Match m = null; for (int i = 0; i < strParts.Length; i++) { m = Regex.Match(strParts[i], strRegEx); if (m.Success) { strBody = m.Groups["Content"].Value; switch (m.Groups["Encoding"].Value.ToUpper()) { case "B": strBody = deCodeB64s(strBody, m.Groups["Charset"].Value); break; case "Q": strBody = DecodeQP.ConvertHexContent(strBody); //, m.Groups["Charset"].Value); break; default: break; } strRet += strBody; } else { if (!IsValidMIMEText(strParts[i])) { strRet += strParts[i]; } else { //blank text } } } return(strRet); } catch { return(strText); } /* * { * try * { * if(strText!=null&&strText!="") * { * if(IsValidMIMEText(strText)) * { * //position at the end of charset * int intPos=strText.IndexOf("=?"); * int intPos2=strText.IndexOf("?",intPos+2); * if(intPos2>3) * { * string strCharset=strText.Substring(2,intPos2-2); * string strEncoding=strText.Substring(intPos2+1,1); * int intPos3=strText.IndexOf("?=",intPos2+3); * string strBody=strText.Substring(intPos2+3,intPos3-intPos2-3); * string strHead=""; * if(intPos>0) * { * strHead=strText.Substring(0,intPos-1); * } * string strEnd=""; * if(intPos3<strText.Length-2) * { * strEnd=strText.Substring(intPos3+2); * } * switch(strEncoding.ToUpper()) * { * case "B": * strBody=deCodeB64s(strBody); * break; * case "Q": * strBody=DecodeQP.ConvertHexContent(strBody); * break; * default: * break; * } * strText=strHead+strBody+strEnd; * if(IsValidMIMEText(strText)) * return DecodeText(strText); * else * return strText; * } * else * {return strText;} * } * else * {return strText;} * } * else * {return strText;} * } * catch * {return strText;}*/ }