求C#高手帮我看看程序!!关于MIMI邮件解码的问题

问题描述

这里有人对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被版主删除

时间: 2024-09-20 12:36:37

求C#高手帮我看看程序!!关于MIMI邮件解码的问题的相关文章

我想在html页面中引用freemarker但不知道该怎么配置,求各位高手帮帮忙

问题描述 我想在html页面中引用freemarker但不知道该怎么配置,求各位高手帮帮忙 我想在html页面中引用freemarker但不知道该怎么配置,求各位高手帮帮忙 解决方案 纯html中无法使用.在动态项目中才能使用,如jsp,asp 你要想使用freemarker标签,必须能够被FreemarkerServlet拦截到,然后解析处理成html 所以你直接用ftl页面就行了,为什么要静态网页和动态模版混着用呢 如果真想用..web.xml 中freemarker拦截器(制定拦截*.ht

字符串处理-求大神帮解决如下程序,最基本的C语言字符串类型,不用编太难(如下为问题要求,测试用例,输出用例)

问题描述 求大神帮解决如下程序,最基本的C语言字符串类型,不用编太难(如下为问题要求,测试用例,输出用例) Background Given an m by n grid of letters and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid.

求SQL高手帮我写一个存储过程,万分感谢!

问题描述 求SQL高手帮我写一个存储过程,万分感谢! 求高手帮帮我..是一个保险类的表.要展示出目标值 实际值 达成率 上年同期 同比增长 指标代码都在图里 解决方案 拍照技术太差了,都不好看 解决方案二: 再记一个SQL分页存储过程 解决方案三: 会是会,但是不太想写, 一.题目要求资料看不清 二.50C吸引力不大 三.如果是你的作业,那你还是自己动手练习:如果是你的工作,那这价值可不止50C

求高手帮调试下程序,紧急,谢谢

问题描述 我的邮件发送和接收程序,发送成功,接收是登录服务器错误,请高手帮调试下,QQ982337685在线等 解决方案 解决方案二:Code??解决方案三:你给邮箱,我给你代码~~

求JAVA高手帮做个地图之类的程序。。

问题描述 用java编电子地图,要求实现记录(记录通过串口输入的人物经过地图上某一点的次数,时间等),查询,串口通信的功能.地图上设置几个点并有坐标(如矿井线路图),当串口输入某个人物通过某一点时,该点变红色,鼠标移动到这些点都会显示点的坐标,用鼠标点击该点,能调用该点的场景图. 解决方案 解决方案二:你還不如問問誰做過講講思路呢·這個不是說做就能做出來的--解决方案三:有谁做过吗?我是新手,想找有关地图编程方面的资料解决方案四:我知道一个方法,先将地图以图片的方式导入,然后再再图片上执行相应的

short转换成byte[]时,数值为负,请高手帮我看看程序啊。

问题描述 我需要把一个short数值A放到数组里,当A>255时,需要两个字节的数组.我发现b[1]的值为负,和我想象中的不一样.publicstaticvoidputShort(byteb[],shorts,intindex){b[index]=(byte)((s>>8)&0xff);b[index+1]=(byte)((s>>0)&0xff);}例如A=1234期望值为:b[0]=0x04,b[1]=0xD2真实值为:b[0]=0x04,b[1]=0x-4

SSH2 问题,求SSH2高手帮解决

问题描述 struts2springframework3.1.0hibernate3在Dao里,调用this.getSession().createSQLQuery(sqlString);连接池不自动关闭,时间长了连接池就会满.而调用this.getSession().createQuery(sqlString);就不会出错,但是项目需要,有LEFTJOIN之类的SQL,不调用createSQLQuery不行.我尝试了在调用完后执行了this.getSession().close();this.

求大神帮我看看程序,

问题描述 我想实现在form4上通信,socket,tcp,按键1发送open,按键2发送close但是我的有问题..哎usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.

急急急 求高手帮我调试一个ASP程序 在线等

问题描述 这是一个网上书店系统求ASP高手帮忙万分感谢 解决方案 解决方案二:没东西啊,看什么?解决方案三:你不把东西发出来,怎么看啊???解决方案四:大家留邮箱我给你们发过去我这边现在附件穿不是上去啊