在Lumisoft.NET組件獲取POP3郵件的時(shí)候,發(fā)現(xiàn)大多數(shù)郵件都能正常獲取,不過(guò)對(duì)于一些特殊的郵件,好像總是會(huì)出現(xiàn)轉(zhuǎn)換錯(cuò)誤,或者出現(xiàn)亂碼及部分亂碼現(xiàn)象,有些在標(biāo)題里面或者郵件接收人地址,而有些則在內(nèi)容里面,為了更好整理相關(guān)的問(wèn)題,寫(xiě)了本文,希望對(duì)大家使用該組件有一定的幫助作用。
1、 日期轉(zhuǎn)換出錯(cuò)問(wèn)題。
錯(cuò)誤信息:[2013-05-04 10:49:03] 轉(zhuǎn)換郵件的Date出錯(cuò):賬號(hào)wuhuacong@163.com 郵件標(biāo)題:ICP???????????????????????wuhuacong)
LumiSoft.Net.ParseException: Header field 'Date' parsing failed.
在 LumiSoft.Net.Mail.Mail_Message.get_Date()
在 WHC.PlugInService.Pop3Helper.Receive() 位置 ......\Pop3Helper.cs:行號(hào) 160
錯(cuò)誤原因:由于郵件格式的日期內(nèi)容格式不同,導(dǎo)致無(wú)法正常解析。如一般的格式為下面
官方的代碼如下所示
MIME_h h = this.Header.GetFirst("Date");
if(h != null){
try{
return MIME_Utils.ParseRfc2822DateTime(((MIME_h_Unstructured)h).Value);
}
catch{
throw new ParseException("Header field 'Date' parsing failed.");
}
}
else{
return DateTime.MinValue;
}
}
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
if(value == DateTime.MinValue){
this.Header.RemoveAll("Date");
}
else{
MIME_h h = this.Header.GetFirst("Date");
if(h == null){
this.Header.Add(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
else{
this.Header.ReplaceFirst(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
}
}
}
MIME_h h = this.Header.GetFirst("Date");
if(h != null){
try{
return MIME_Utils.ParseRfc2822DateTime(((MIME_h_Unstructured)h).Value);
}
catch{
//嘗試轉(zhuǎn)換正常的日期
DateTime dt;
string dateString = ((MIME_h_Unstructured)h).Value;
bool success = DateTime.TryParse(dateString, out dt);
if (success)
{
return dt;
}
else
{
throw new ParseException("Header field 'Date' parsing failed.");
}
}
}
else{
return DateTime.MinValue;
}
}
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
if(value == DateTime.MinValue){
this.Header.RemoveAll("Date");
}
else{
MIME_h h = this.Header.GetFirst("Date");
if(h == null){
this.Header.Add(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
else{
this.Header.ReplaceFirst(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
}
}
}
在 LumiSoft.Net.TCP.TCP_Client.Connect(String host, Int32 port, Boolean ssl)
在 WHC.PlugInService.SmtpHelper.Send() 位置 ........\SmtpHelper.cs:行號(hào) 123
在 WHC.PlugInService.SendMailService.DataThreadHandle(MailSendConfigInfo info) 位置 ...............\SendMailService.cs:行號(hào) 66
錯(cuò)誤原因:由于POP3的配置端口不正確導(dǎo)致,一般的端口必須嚴(yán)格按照正常的來(lái)填寫(xiě)。
郵件SMTP和POP3常用配置說(shuō)明:
郵箱 |
Smtp服務(wù)器 |
Smtp端口 |
POP3服務(wù)器 |
POP3端口 |
使用SSL |
Gmail.com |
smtp.gmail.com |
465 |
pop.gmail.com |
995 |
true |
QQ.com |
smtp.qq.com |
25 |
pop.qq.com |
110 |
true |
163.com |
smtp.163.com |
25 |
pop.163.com |
110 |
false |
Sina.com |
smtp.sina.com |
25 |
pop.sina.com |
110 |
false |
其他 |
smtp.test.com |
25 |
pop.test.com |
110 |
false |
3、郵件標(biāo)題亂碼問(wèn)題
錯(cuò)誤信息:標(biāo)題出現(xiàn)類似=?utf-8?B?5rWL6K+V6YKu5Lu2?=
錯(cuò)誤原因:這個(gè)是因?yàn)榫幋a的問(wèn)題,其中=?utf-8?B是表示該段字符為UTF-8的格式,后面的是base64格式的內(nèi)容。除了utf-8,還可以出現(xiàn)gb2312或者ibm-euccn等格式。為了轉(zhuǎn)換上面的編碼問(wèn)題,我寫(xiě)了一個(gè)轉(zhuǎn)碼函數(shù),如下所示。
Regex re = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
MatchCollection mcs = re.Matches(input);
foreach (Match mc in mcs)
{
string encode = mc.Groups["encode"].Value;
if (!string.IsNullOrEmpty(encode))
{
if (encode.ToLower().Contains("euccn") || encode.ToLower().Contains("euc-cn") ||
encode.ToLower().Contains("gbk"))
{
encode = "gb2312";
}
else if (encode.ToLower().Contains("utf8"))
{
encode = "utf-8";
}
string body = mc.Groups["body"].Value;
byte[] bytes = Convert.FromBase64String(body);
string result = Encoding.GetEncoding(encode).GetString(bytes);
input = input.Replace(mc.Value, result);
}
}
return input;
}
除了上面的轉(zhuǎn)碼操作,還有一種更好的方法,能夠使得郵件相關(guān)信息正常顯示。
因?yàn)橥ㄟ^(guò)分析了解到,由于Lumisoft的Mail_Message.ParseFromByte函數(shù)默認(rèn)只是以UTF8轉(zhuǎn)換字節(jié),一旦字節(jié)為GB2312格式,就會(huì)發(fā)生轉(zhuǎn)換亂碼問(wèn)題,因此先經(jīng)過(guò)Default編碼轉(zhuǎn)換,然后再以UTF8獲取字節(jié),即可正常轉(zhuǎn)換郵件頭部。
標(biāo)簽:西藏 福州 湖州 西寧 宣城 紅河 衢州 岳陽(yáng)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《深入Lumisoft.NET組件開(kāi)發(fā)碰到亂碼等問(wèn)題的解決方法》,本文關(guān)鍵詞 深入,Lumisoft.NET,組件,開(kāi)發(fā),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。