Pop3协议之三(编码)
取材自csdn文章http://dev.csdn.net/develop/article/18/18448.shtm
通过pop3协议传输的主要是可打印字符ascii,其他编码比如unicode需要经过编码,通常的编码方式是Base64(=?*****?B)和Quoted-printable(=?******?Q) 其中*代表字符集
示例
Subject:在邮件头,表示本行为邮件主题
=?gb2312?B? 表明后面一直到下一个?=出现,中间的字符串是通过base64编码的
Subject: =?GB2312?Q?=D0=A1=F6=A9=B5=D8=D6=B7?=同理,=?GB2312?Q? 一直到后面的 ?= 之间都是通过Quoted-printable编码
具体如何编码可以看上面的文章,现在说解码
Class Common
通过pop3协议传输的主要是可打印字符ascii,其他编码比如unicode需要经过编码,通常的编码方式是Base64(=?*****?B)和Quoted-printable(=?******?Q) 其中*代表字符集
示例
Subject:=?gb2312?B?KMjw0MfM4cq+LbTL08q8/r/JxNzKx8Csu/jTyrz+KQ==?==?GB2312?B?1vfM4qO6?=
Subject:在邮件头,表示本行为邮件主题
=?gb2312?B? 表明后面一直到下一个?=出现,中间的字符串是通过base64编码的
![](/Images/OutliningIndicators/None.gif)
具体如何编码可以看上面的文章,现在说解码
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;
}
{
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;
}
{
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;
}