问题描述
这里有人对MIME邮件解码比较了解的吗?可否帮我看看到底出了什么错呢?我的程序可以获取邮件,但是解码以后却什么都显示不出来!!过两天就要交大作业了,继续各路大侠的帮助!!//获取邮件内容privatevoidRetrieveBtn_Click(objectsender,System.EventArgse){Message.Clear();//清除邮件内容显示区try{//定义接收邮箱中的第几封邮件的命令Data="RETR"+Number.Text+CRLF;szData=System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());NetStrm.Write(szData,0,szData.Length);//向服务器发送接收邮件命令stringszTemp;szTemp=RdStrm.ReadLine();//解析接收的邮件内容if(szTemp[0]!='-'){while(szTemp!="."){Message.Text=Message.Text+"发送者:"+ParseSender(szTemp).ToString()+CRLF;Message.Text=Message.Text+"接收者:"+ParseReciever(szTemp).ToString()+CRLF;Message.Text=Message.Text+"主题:"+ParseSubject(szTemp).ToString()+CRLF;Message.Text=Message.Text+"内容:"+ParseEmailText(szTemp).ToString()+CRLF;szTemp=RdStrm.ReadLine();}}}catch(InvalidOperationExceptionerr){Status.Items.Add("Error:"+err.ToString());}}//========================对发送者的信息进行解码=================================publicstringParseSender(stringemailContent){stringpat=@"^From:s*(?<sender>.*)s*rn";Matchm=Regex.Match(emailContent,pat,RegexOptions.IgnoreCase|RegexOptions.Multiline);stringsender=m.Groups["sender"].ToString();pat=""?=\?(?<TextEncoding>.+)\?(?<encodingmethod>[A-Z])\?(?<encodedText>.*)\?="?(?<other>.*)$";m=Regex.Match(sender,pat,RegexOptions.IgnoreCase);stringTextEncoding=m.Groups["TextEncoding"].ToString();stringencodingmethod=m.Groups["encodingmethod"].ToString();stringencodedText=m.Groups["encodedText"].ToString();stringother=m.Groups["other"].ToString();if(encodingmethod.ToUpper().Equals("B")){byte[]inputBytes=Convert.FromBase64String(encodedText);stringstr=System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes,0,inputBytes.Length);sender=str+other;}elseif(encodingmethod.ToUpper().Equals("Q")){sender=ConvertFromQPString(encodedText,System.Text.Encoding.GetEncoding(TextEncoding))+other;}returnsender;}//对邮件主题进行解码publicstringParseSubject(stringemailContent){stringpat=@"^Subject:s*(?<Subject>.*)n";Matchm=Regex.Match(emailContent,pat,RegexOptions.IgnoreCase|RegexOptions.Multiline);stringisubject=m.Groups["Subject"].ToString();pat=""?=\?(?<TextEncoding>.+)\?(?<encodingmethod>[A-Z])\?(?<encodedText>.*)\?="";m=Regex.Match(isubject,pat,RegexOptions.IgnoreCase);stringTextEncoding=m.Groups["TextEncoding"].ToString();stringencodingmethod=m.Groups["encodingmethod"].ToString();stringencodedText=m.Groups["encodedText"].ToString();stringother=m.Groups["other"].ToString();if(encodingmethod.ToUpper()=="B"){byte[]inputBytes=Convert.FromBase64String(encodedText);stringstr=System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes,0,inputBytes.Length);isubject=str;}elseif(encodingmethod.ToUpper().Equals("Q")){isubject=ConvertFromQPString(encodedText,System.Text.Encoding.GetEncoding(TextEncoding));}returnisubject;}//对文件内容进行解码publicstringParseEmailText(stringemailContent){stringisTextOnly=@"^Content-Type:s*multipart";boolt=Regex.IsMatch(emailContent,isTextOnly,RegexOptions.Multiline);if(t){stringpat=@"^(?<boundary>.*)nContent-Type:s*text/plain";Matchmatch=Regex.Match(emailContent,pat,RegexOptions.Multiline);stringboundary=match.Groups["boundary"].ToString();pat=@"^Content-Type:s*text/plain[sS]*?Content-Transfer-Encoding:s*(?<encodingMethod>.*)n(?<crytext>[sS]*?)"+boundary;MatchmyMatches=Regex.Match(emailContent,pat,RegexOptions.IgnoreCase|RegexOptions.Multiline);GroupCollectionmyGroup=myMatches.Groups;stringcrytext=myGroup["crytext"].ToString();//title变量存储From域的内容stringencodingMethod=myGroup["encodingMethod"].ToString();if(encodingMethod.ToUpper().Equals("BASE64")){crytext=crytext.Replace("n","");byte[]inputBytes=Convert.FromBase64String(crytext);stringEmailText=System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes,0,inputBytes.Length);returnEmailText;}else{returnConvertFromQPString(crytext,System.Text.Encoding.GetEncoding(54936)).Replace("n","rn");}}else{stringpat=@"^Content-Type:s*text/plain[sS]*?Content-Transfer-Encoding:s*(?<encodingMethod>.*)n(.*n)*?s*n(?<crytext>[sS]+)";MatchmyMatches=Regex.Match(emailContent,pat,RegexOptions.IgnoreCase|RegexOptions.Multiline);GroupCollectionmyGroup=myMatches.Groups;stringcrytext=myGroup["crytext"].ToString();//title变量存储From域的内容stringencodingMethod=myGroup["encodingMethod"].ToString();if(encodingMethod.ToUpper().Equals("BASE64")){crytext=crytext.Replace("n","");byte[]inputBytes=Convert.FromBase64String(crytext);stringEmailText=System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes,0,inputBytes.Length);returnEmailText;}else{returnConvertFromQPString(crytext,System.Text.Encoding.GetEncoding("utf-8")).Replace("n","rn");}}}publicstringConvertFromQPString(stringquoted_printableString,System.Text.Encodingencoding){stringInputString=quoted_printableString;StringBuilderbuilder1=newStringBuilder();//InputString=InputString.Replace("=rn","");for(intnum1=0;num1<InputString.Length;num1++){if(InputString[num1]=='='){try{if(HexToDec(InputString.Substring(num1+1,2))<0x80){if(HexToDec(InputString.Substring(num1+1,2))>=0){byte[]buffer1=newbyte[1]{(byte)HexToDec(InputString.Substring(num1+1,2))};builder1.Append(encoding.GetString(buffer1,0,buffer1.Length));num1+=2;}}elseif(InputString[num1+1]!='='){byte[]buffer2=newbyte[2]{(byte)HexToDec(InputString.Substring(num1+1,2)),(byte)HexToDec(InputString.Substring(num1+4,2))};builder1.Append(encoding.GetString(buffer2,0,buffer2.Length));num1+=5;}}catch{builder1.Append(InputString.Substring(num1,1));}}else{builder1.Append(InputString.Substring(num1,1));}}returnbuilder1.ToString();}privatestaticintHexToDec(stringhex){intnum1=0;stringtext1="0123456789ABCDEF";for(intnum2=0;num2<hex.Length;num2++){if(text1.IndexOf(hex[num2])==-1){return-1;}num1=(num1*0x10)+text1.IndexOf(hex[num2]);}returnnum1;}}
解决方案
解决方案二:
该回复于2011-12-26 09:17:16被版主删除