Pop3协议之三(编码)

取材自csdn文章 http://dev.csdn.net/develop/article/18/18448.shtm

通过pop3协议传输的主要是可打印字符ascii,其他编码比如unicode需要经过编码,通常的编码方式是Base64(=?*****? B)和Quoted-printable(=?******? Q) 其中*代表字符集

示例

Subject:=?gb2312?B?KMjw0MfM4cq+LbTL08q8/r/JxNzKx8Csu/jTyrz+KQ==?==?GB2312?B?1vfM4qO6?=

Subject:在邮件头,表示本行为邮件主题

=?gb2312? B?    表明后面一直到下一个?=出现,中间的字符串是通过 base64编码的

None.gif Subject: =?GB2312?Q?=D0=A1=F6=A9=B5=D8=D6=B7?=
同理,=?GB2312? Q?   一直到后面的 ?= 之间都是通过 Quoted-printable编码

具体如何编码可以看上面的文章,现在说解码

         private   static   string  ConvertCharSet( string  source)
        {
            
string  result  =  source;
            Regex regex 
=   new  Regex( @" =\?([a-zA-Z0-9\-]*)\?B\?([a-zA-Z0-9\+\/=]*)\?= " ,RegexOptions.IgnoreCase);
            Match m 
=  regex.Match(source);
            
while (m.Success)
            {
                result  =  regex.Replace(result ,System.Text.Encoding.GetEncoding( " GB2312 " ).GetString(
                    System.Convert.FromBase64String(m.Groups[
2 ].Value)));
                m
= m.NextMatch();
            }
            regex 
=   new  Regex( @" =\?([a-zA-Z0-9\-]*)\?Q\?([a-zA-Z0-9\+\/=]*)\?= " ,RegexOptions.IgnoreCase);
            m 
=  regex.Match(result);
            
while  (m.Success)
            {
                result 
=  Common.FromQuote(m.Groups[ 2 ].Value);
                m 
=  m.NextMatch();
            }
             return  result;
        }

Class Common
         public   static   string  FromQuote( string  data)
        {
            
string  result  =  data;

            Regex regex1  =   new  Regex( " ((=[a-fA-F0-9]{2})+) " );
            Match m1 
=  regex1.Match(data);
            
while (m1.Success)
            {
                
string  temp  =  m1.Groups[ 1 ].Value;
                Regex regex2 
=   new  Regex( " =([a-fA-F0-9]{2}) " );
                Match m2 
=  regex2.Match(temp);
                
string  str  =   string .Empty;
                
byte [] buffer  =   new   byte [ 0 ];
                
while  (m2.Success)
                {
                
                    
byte [] bs  =  buffer;
                    
byte  b  =   byte .Parse(m2.Groups[ 1 ].Value.ToLower(),System.Globalization.NumberStyles.HexNumber);
                    buffer 
=   new   byte [buffer.Length + 1 ];
                    bs.CopyTo(buffer,
0 );
                    buffer[buffer.Length
- 1 =  b;

                    m2
= m2.NextMatch();
                }
                temp 
=  System.Text.Encoding.Default.GetString(buffer);
                result 
=  regex1.Replace(result,temp, 1 );
                m1 
=  regex1.Match(result);
            }
            
return  result;
        }

转载于:https://www.cnblogs.com/pblee/archive/2006/01/19/320460.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值