日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

深入Lumisoft.NET組件開發(fā)碰到亂碼等問題的解決方法_.Net教程

編輯Tag賺U幣

推薦:深入Lumisoft.NET組件POP3郵件接收與刪除操作的使用詳解
本篇文章對Lumisoft.NET組件POP3郵件接收與刪除操作的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下

在Lumisoft.NET組件獲取POP3郵件的時候,發(fā)現(xiàn)大多數(shù)郵件都能正常獲取,不過對于一些特殊的郵件,好像總是會出現(xiàn)轉(zhuǎn)換錯誤,或者出現(xiàn)亂碼及部分亂碼現(xiàn)象,有些在標(biāo)題里面或者郵件接收人地址,而有些則在內(nèi)容里面,為了更好整理相關(guān)的問題,寫了本文,希望對大家使用該組件有一定的幫助作用。

1、 日期轉(zhuǎn)換出錯問題。
錯誤信息:[2013-05-04 10:49:03] 轉(zhuǎn)換郵件的Date出錯:賬號[email protected] 郵件標(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:行號 160

錯誤原因:由于郵件格式的日期內(nèi)容格式不同,導(dǎo)致無法正常解析。如一般的格式為下面

復(fù)制代碼 代碼如下:hl5o.cn

Message-ID: <d74841c5887b4df692ebdb7ec7802054@4782e72954a24cc89535840ea2e5da5b>
Date: Fri, 26 Apr 2013 08:56:52 GMT
Mime-Version: 1.0
From: "[email protected]" <[email protected]>
To: "[email protected]" <[email protected]>

有些郵件日期格式是2013-05-06 19:01:44,則Lumisoft組件無法解析,需要跟蹤到他的郵件日期處理的代碼,然后進(jìn)行修改才可以實(shí)現(xiàn)正常的郵件日期解析了。

官方的代碼如下所示

復(fù)制代碼 代碼如下:hl5o.cn

public DateTime Date
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}

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)));
}
}
}
}


需要增加對普通日期格式的修改,修改后的代碼如下所示
復(fù)制代碼 代碼如下:hl5o.cn

public DateTime Date
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}

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)));
}
}
}
}


2、由于意外的數(shù)據(jù)包格式,握手失敗
錯誤信息:[2013-05-04 10:13:54] System.IO.IOException: 由于意外的數(shù)據(jù)包格式,握手失敗。

在 LumiSoft.Net.TCP.TCP_Client.Connect(String host, Int32 port, Boolean ssl)

在 WHC.PlugInService.SmtpHelper.Send() 位置 ........\SmtpHelper.cs:行號 123

在 WHC.PlugInService.SendMailService.DataThreadHandle(MailSendConfigInfo info) 位置 ...............\SendMailService.cs:行號 66

錯誤原因:由于POP3的配置端口不正確導(dǎo)致,一般的端口必須嚴(yán)格按照正常的來填寫。

郵件SMTP和POP3常用配置說明:

3、郵件標(biāo)題亂碼問題

錯誤信息:標(biāo)題出現(xiàn)類似=?utf-8?B?5rWL6K+V6YKu5Lu2?=

錯誤原因:這個是因為編碼的問題,其中=?utf-8?B是表示該段字符為UTF-8的格式,后面的是base64格式的內(nèi)容。除了utf-8,還可以出現(xiàn)gb2312或者ibm-euccn等格式。為了轉(zhuǎn)換上面的編碼問題,我寫了一個轉(zhuǎn)碼函數(shù),如下所示。

復(fù)制代碼 代碼如下:hl5o.cn

private string DecodeString(string input)
{
string regex = @"=\?(?<encode>.*?)\?B\?(?<body>.*?)\?=";

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;
}


如可以通過代碼吧標(biāo)題進(jìn)行轉(zhuǎn)碼解析
復(fù)制代碼 代碼如下:hl5o.cn

info.Title = DecodeString(mime_header.Subject);

轉(zhuǎn)碼后,標(biāo)題和相關(guān)的內(nèi)容都可以正常顯示了。

除了上面的轉(zhuǎn)碼操作,還有一種更好的方法,能夠使得郵件相關(guān)信息正常顯示。

因為通過分析了解到,由于Lumisoft的Mail_Message.ParseFromByte函數(shù)默認(rèn)只是以UTF8轉(zhuǎn)換字節(jié),一旦字節(jié)為GB2312格式,就會發(fā)生轉(zhuǎn)換亂碼問題,因此先經(jīng)過Default編碼轉(zhuǎn)換,然后再以UTF8獲取字節(jié),即可正常轉(zhuǎn)換郵件頭部。

復(fù)制代碼 代碼如下:hl5o.cn

byte[] utf8Bytes = Encoding.UTF8.GetBytes(message.HeaderToString());
Mail_Message mime_header = Mail_Message.ParseFromByte(utf8Bytes);

這樣獲取到的標(biāo)題,以及郵件頭部等信息,都是正常的了。

分享:ASP.NET26個常用性能優(yōu)化方
訪問數(shù)據(jù)庫資源需要創(chuàng)建連接、打開連接和關(guān)閉連接幾個操作。這些過程需要多次與數(shù)據(jù)庫交換信息以通過身份驗證,比較耗費(fèi)服務(wù)器資源。 ASP.NET中提供了連接池(Connection Pool)改善打開和關(guān)閉數(shù)據(jù)庫對性能的影響。系統(tǒng)將用戶的數(shù)據(jù)庫連接放在連接池中,需要時取出,關(guān)閉

來源:模板無憂//所屬分類:.Net教程/更新時間:2013-05-08
相關(guān).Net教程