求C#高手帮我看看程序!!关于MIMI邮件解码的问题
这里有人对MIME邮件解码比较了解的吗?可否帮我看看到底出了什么错呢?我的程序可以获取邮件,但是解码以后却什么都显示不出来!!过两天就要交大作业了,继续各路大侠的帮助!!//获取邮件内容
private void RetrieveBtn_Click(object sender, System.EventArgs e)
{
Message.Clear();//清除邮件内容显示区
try
{
// 定义接收邮箱中的第几封邮件的命令
Data = "RETR " + Number.Text + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length); //向服务器发送接收邮件命令
string szTemp;
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(InvalidOperationException err)
{
Status.Items.Add("Error: "+err.ToString());
}
}
//========================对发送者的信息进行解码=================================
public string ParseSender(string emailContent)
{
string pat = @"^From:\s*(?<sender>.*)\s*\r\n";
Match m = Regex.Match(emailContent, pat, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string sender = m.Groups["sender"].ToString();
pat = "\"?=\\?(?<TextEncoding>.+)\\?(?<encodingmethod>[A-Z])\\?(?<encodedText>.*)\\?=\"?(?<other>.*)$";
m = Regex.Match(sender, pat, RegexOptions.IgnoreCase);
string TextEncoding = m.Groups["TextEncoding"].ToString();
string encodingmethod = m.Groups["encodingmethod"].ToString();
string encodedText = m.Groups["encodedText"].ToString();
string other = m.Groups["other"].ToString();
if (encodingmethod.ToUpper().Equals("B"))
{
byte[] inputBytes = Convert.FromBase64String(encodedText);
string str = System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes, 0, inputBytes.Length);
sender = str + other;
}
else if (encodingmethod.ToUpper().Equals("Q"))
{
sender = ConvertFromQPString(encodedText, System.Text.Encoding.GetEncoding(TextEncoding)) + other;
}
return sender;
}
//对邮件主题进行解码
public string ParseSubject(string emailContent)
{
string pat = @"^Subject:\s*(?<Subject>.*)\n";
Match m = Regex.Match(emailContent, pat, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string isubject = m.Groups["Subject"].ToString();
pat = "\"?=\\?(?<TextEncoding>.+)\\?(?<encodingmethod>[A-Z])\\?(?<encodedText>.*)\\?=\"";
m = Regex.Match(isubject, pat, RegexOptions.IgnoreCase);
string TextEncoding = m.Groups["TextEncoding"].ToString();
string encodingmethod = m.Groups["encodingmethod"].ToString();
string encodedText = m.Groups["encodedText"].ToString();
string other = m.Groups["other"].ToString();
if (encodingmethod.ToUpper()=="B")
{
byte[] inputBytes = Convert.FromBase64String(encodedText);
string str = System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes, 0, inputBytes.Length);
isubject = str;
}
else if (encodingmethod.ToUpper().Equals("Q"))
{
isubject = ConvertFromQPString(encodedText, System.Text.Encoding.GetEncoding(TextEncoding));
}
return isubject;
}
//对文件内容进行解码
public string ParseEmailText(string emailContent)
{
string isTextOnly =@"^Content-Type:\s*multipart";
bool t = Regex.IsMatch(emailContent,isTextOnly,RegexOptions.Multiline);
if(t)
{
string pat = @"^(?<boundary>.*)\nContent-Type:\s*text/plain";
Match match = Regex.Match(emailContent, pat, RegexOptions.Multiline);
string boundary = match.Groups["boundary"].ToString();
pat = @"^Content-Type:\s*text/plain[\s\S]*?Content-Transfer-Encoding:\s*(?<encodingMethod>.*)\n(?<crytext>[\s\S]*?)"+boundary;
Match myMatches = Regex.Match(emailContent, pat, RegexOptions.IgnoreCase | RegexOptions.Multiline);
GroupCollection myGroup = myMatches.Groups;
string crytext = myGroup["crytext"].ToString();//title变量存储From域的内容
string encodingMethod = myGroup["encodingMethod"].ToString();
if (encodingMethod.ToUpper().Equals("BASE64"))
{
crytext = crytext.Replace("\n", "");
byte[] inputBytes = Convert.FromBase64String(crytext);
string EmailText = System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes, 0, inputBytes.Length);
return EmailText;
}
else
{
return ConvertFromQPString(crytext,System.Text.Encoding.GetEncoding(54936)).Replace("\n","\r\n");
}
}
else
{
string pat = @"^Content-Type:\s*text/plain[\s\S]*?Content-Transfer-Encoding:\s*(?<encodingMethod>.*)\n(.*\n)*?\s*\n(?<crytext>[\s\S]+)";
Match myMatches = Regex.Match(emailContent, pat, RegexOptions.IgnoreCase | RegexOptions.Multiline);
GroupCollection myGroup = myMatches.Groups;
string crytext = myGroup["crytext"].ToString();//title变量存储From域的内容
string encodingMethod = myGroup["encodingMethod"].ToString();
if (encodingMethod.ToUpper().Equals("BASE64"))
{
crytext = crytext.Replace("\n", "");
byte[] inputBytes = Convert.FromBase64String(crytext);
string EmailText = System.Text.Encoding.GetEncoding("utf-8").GetString(inputBytes, 0, inputBytes.Length);
return EmailText;
}
else
{
return ConvertFromQPString(crytext, System.Text.Encoding.GetEncoding("utf-8")).Replace("\n", "\r\n");
}
}
}
public string ConvertFromQPString(string quoted_printableString, System.Text.Encoding encoding)
{
string InputString = quoted_printableString;
StringBuilder builder1 = new StringBuilder();
// InputString = InputString.Replace("=\r\n", "");
for (int num1 = 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 = new byte[1] { (byte)HexToDec(InputString.Substring(num1 + 1, 2)) };
builder1.Append(encoding.GetString(buffer1, 0, buffer1.Length));
num1 += 2;
}
}
else if (InputString[num1 + 1] != '=')
{
byte[] buffer2 = new byte[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));
}
}
return builder1.ToString();
}
private static int HexToDec(string hex)
{
int num1 = 0;
string text1 = "0123456789ABCDEF";
for (int num2 = 0; num2 < hex.Length; num2++)
{
if (text1.IndexOf(hex[num2]) == -1)
{
return -1;
}
num1 = (num1 * 0x10) + text1.IndexOf(hex[num2]);
}
return num1;
}
} --------------------编程问答-------------------- --------------------编程问答-------------------- http://lutianmao.iteye.com/blog/144933
补充:.NET技术 , C#