ITPub博客

首页 > IT基础架构 > 网络安全 > 自制Pop3邮件接收系统(一):利用MatchEvaluator实现HZ的解码 (转)

自制Pop3邮件接收系统(一):利用MatchEvaluator实现HZ的解码 (转)

原创 网络安全 作者:amyz 时间:2007-10-17 18:35:42 0 删除 编辑
自制Pop3邮件接收系统(一):利用MatchEvaluator实现HZ的解码 (转)[@more@]

要作自制的Pop3邮件接收系统,就要自己处理邮件的编码

大家可能会收到以下的“乱码”,
~{4sC#,NRJG~}qiushuiwuhen~{#,;6S-@45=~}csdn.

实际上是简体中文的HZ编码,在邮件中经常会出现,

因为邮件协议体系通常是七位的,而汉字却是双字节的

所以就使用以下的方法对其进行解码

String hzDecode(Match m){
 String tmp=m.ToString();
 byte[] ret=new byte[tmp.Length-4];
 for(int i=0;i ret[i]=(byte)(tmp[i+2]+128);
 return Encoding.GetEncoding(936).GetString(ret);
}


使用范例:
 string strHz="~{4s strHz=Regex.Replace(strHz,@"~{(.+?)~}",new MatchEvaluator(hzDecode));
 Response.Write("nHZ Decode="+strHz);


随便带上编码的代码(可用来发送邮件用)

String hzEncode(Match m){
 byte[] tmp=Encoding.GetEncoding(936).GetBytes(m.ToString());
 StringBuilder ret=new StringBuilder("~{");
 for(int i=0;i ret.Append((char)(tmp[i]-128));
 ret.Append("~}");
 return ret.ToString(); 
}

使用范例:
 string strHz="大家好,我是qiushuiwuhen,欢迎来到csdn.";
 strHz=Regex.Replace(strHz,@"[^x00-xff]+",new MatchEvaluator(hzEncode));
 Response.Write("

HZ Encode="+strHz);</P> <P>原理:使用MatchEvaluator的回调(CallBack)<A href="tag-266-1.html">函数</A><A href="tag-261-1.html">执行</A>Regex的替换操作<BR></P><BR></SPAN> <p style="clear:both;"></p> <p class="translate"> 来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-977238/,如需转载,请注明出处,否则将追究法律责任。 </p> </div> </div> <div class="preview-footer clearfix"> <!--已点赞、收藏--> <div class="icon-praise" id="praise" onclick="blogObj.praise($(this),'/praise/')"><span class="text"></span><span class="num">0</span></div> <div class="icon-collect" id="collect" onclick="blogObj.praise($(this),'/favour/')"><span class="text"></span><span class="num">0</span></div> <div class="share-group fr"> <div class="bdsharebuttonbox bdshare-button-style0-16" data-bd-bind="1522216707615"> <span class="fl">分享到:</span> <a href="javascript:;" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a> <a href="javascript:;" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a> <a href="javascript:;" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a> <a href="javascript:;" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a> </div> <script> window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdDesc": "", "bdMini": "", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "32" }, "share": {"bdSize": 16} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)]; if ($(window).width() < 1320) { if ($(window).width() < 1200) { $(".sideslip").css({ "left": "20px", "margin-left": 0 }) } else { $(".sideslip").css({ "margin-left": "-590px" }) } }</script> </div> </div> <div class="article-context"> <div class="fl"> <span class="color77">上一篇:</span> <a href="http://blog.itpub.net/10752019/viewspace-977235/">告别理想主义,走向经验主义 (转)</a> </div> <div class="fr"> <span class="color77">下一篇:</span> <a href="http://blog.itpub.net/10752019/viewspace-977239/">Delphi的BUGS之我见 (转)</a> </div> </div> <input type="hidden" id="blogId" value="977238"> <!--评论开始--> <div class="blog-comment"> <div class="new-comment"> <input type="hidden" id="hid" name="hid" value="977238"> <input type="hidden" name="_token" value="QfU5AUsfYBfbML1Mrsn0GU7hQ7gtZiN7iIlK7cBY"> <a class="avatar" href="javascript:;"> <img src="http://blog.itpub.net/images/user_pic_default.png"></a> <!--用户未登录--> <div class="sign-container"> <span>请登录后发表评论</span> <a class="btn btn-sign"> <a class="layui-btn ml30" onclick="blog.login(event)">登录</a> </a> </div> <!--用户已登录--> <textarea placeholder="请写下你的评论…" onfocus="blogObj.textareaFocus($(this),200)" maxlength=200></textarea> </div> <div class="comment-list"> <div class="top-title"><span class="all-comment">全部评论</span> <span class="colorbb ml20"></span> </div> <div class="comment-items" id="comment_items"> <script type="text/template" id='blog_template'> <%for(var i=0;i <items.length;i++){%> <div class="comment-item"> <div class="author"> <a target="_blank" href="/<%=items[i].uid%>" class="avatar"> <img src="<%=items[i].headimg%>" alt=""> </a> <div class="info"> <div class="name"><a target="_blank" href="/<%=items[i].uid%>"><%=items[i].username%></a> <span class="fr reply-btn" onclick="blogObj.newComment($(this),'<%=items[i].username%>',<%=items[i].id%>)">回复</span></div> <div class="time colorbb"><%=items[i].createtime%></div> </div> </div> <p class="comment-wrap"><%=items[i].content%></p> <%if(items[i].items.items.length) { %> <div class="sub-comment-list"> <%for(var j=0;j<items[i].items.items.length;j++){%> <div class="sub-comment"> <div class="color77 time-reply"> <span class="time"><%=items[i].items.items[j].createtime%></span> <span class="reply-btn fr" onclick="blogObj.newComment($(this),'<%=items[i].items.items[j].username%>',<%=items[i].items.items[j].id%>)">回复</span> </div> <p><a href="/<%=items[i].uid%>"><%=items[i].items.items[j].username%></a>&nbsp;&nbsp;&nbsp;回复&nbsp;&nbsp;&nbsp;<a href="/<%=items[i].touid%>"><%=items[i].items.items[j].tousername%></a>: <span class="content"><%=items[i].items.items[j].content%></span></p> </div> <%}%> <%if(items[i].items.total > 5) { %> <div class="more-comment"><span class="sub-comment-count">还有<i class="count"><%=items[i].items.total-5%></i>条评论</span><span class="more-comment-btn" onclick=blogObj.loadSubComment($(this),<%=items[i].id%>) data-count=1 data-flag=true>点击查看</span></div> <%}%> </div> <%}%> </div> <%}%> </script> </div> </div> </div> <!--评论结束--> </div> <script> </script> <div class="fr w290"> <!--作者信息开始--> <!--作者信息开始--> <div class="author-info right-fixed "> <div class="head-img"> <a href="http://blog.itpub.net/10752019/"><img src="http://account.itpub.net/api/avatar.php?uid=10752019" alt=""></a> </div> <div class="author-name"><a href="http://blog.itpub.net/10752019/">amyz</a></div> <div class="author-intro"> </div> <ul class="tree-list clearfix"> <li> <div class="item-tt">博文量</div> <a href="http://blog.itpub.net/10752019/"><span class="item-num blognum">3984</span></a> </li> <li> <div class="item-tt">访问量</div> <div class="item-num blogviewnum">7361785</div> </li> </ul> </div> <!--作者信息结束--> <script> var data = {uid: 10752019}; $._ajax({ url: '/getAuthorInfo/', data: data, type: 'get', dataType: 'json', success: function (data) { if (data.code == 200) { var data = data.data; $(".blognum").text(data.blognum); $(".blogviewnum").text(data.visitednum); } } }); </script> <!--作者信息结束--> <!--博文推荐开始--> <div class="blog-choice right-fixed"> <h3 class="choice-title">最新文章</h3> <ul class="newul"> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-1001318/" class="clearfix"> 防止关闭windows (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-1001315/" class="clearfix"> TList,AnsiString和DynamicArray的妙用 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-1001311/" class="clearfix"> 在OpenGL中使用各异向性纹理过滤 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-1001310/" class="clearfix"> 完成端口(上) (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-987000/" class="clearfix"> WebLogic的初步研究(2)--结构篇 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-986998/" class="clearfix"> 玩转Windows桌面图标 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-986997/" class="clearfix"> Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数! (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-986996/" class="clearfix"> 图像平滑滚动效果的VC实现 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-986995/" class="clearfix"> VC++初学者常见问题解惑 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752019/viewspace-986993/" class="clearfix"> 例程详析动态链接库 (转) </a> </li> <ul> </div> <!--大牛精选结束--> </div> </div> <div class="icon-back-top" onclick="$('html,body').animate({scrollTop:0},'slow');"></div> <!--main部分结束--> <!--百度推送--> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!--2020-02-18 03:03:35--> <!--footer部分开始--> <div class="blog-footer"> <div class="w1200 pr"> <div class="footer-links"> <a class="icon-blog icon-wx mr30"> <div class="wx-qrcode"> <img src="http://edu.itpub.net/images/qrcode.jpg" alt=""> </div> </a> <a href="https://weibo.com/itpub2001?from=myfollow_all" class="icon-blog icon-wb" target="_blank"></a> </div> <p class="footer-nav"> <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank">支持我们</a> <a href="http://www.it168.com/bottomfile/tgzn.shtml" target="_blank">作者招募</a> <a href="http://www.it168.com/bottomfile/sytk.shtml" target="_blank">用户协议</a> <a href="http://blog.itpub.net/31509949/viewspace-2157750/" target="_blank">FAQ</a> <a href="http://edu.itpub.net/contactus.html" target="_blank">Contact Us</a> <script src="https://s22.cnzz.com/z_stat.php?id=1274521965&web_id=1274521965" language="JavaScript"></script> </p> <p>北京盛拓优讯信息技术有限公司. 版权所有&nbsp;&nbsp;<a style="color:#777777;" target="_blank" href="http://beian.miit.gov.cn">京ICP备09055130号-4</a>&nbsp;&nbsp;北京市公安局海淀分局网监中心备案编号:11010802021510</p> <p>广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员</p> </div> </div> <!--footer部分结束--> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?5016281862f595e78ffa42f085ea0f49"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- END STAT PV --> </body> </html>