程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> 深入Lumisoft.NET組件開發碰到亂碼等問題的解決方法

深入Lumisoft.NET組件開發碰到亂碼等問題的解決方法

編輯:ASP.NET基礎

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

1、 日期轉換出錯問題。
錯誤信息:[2013-05-04 10:49:03]    轉換郵件的Date出錯:賬號[email protected] 郵件標題:ICP???????????????????????wuhuacong)

LumiSoft.Net.ParseException: Header field 'Date' parsing failed.

   在 LumiSoft.Net.Mail.Mail_Message.get_Date()

   在 WHC.PlugInService.Pop3Helper.Receive() 位置 ......\Pop3Helper.cs:行號 160

錯誤原因:由於郵件格式的日期內容格式不同,導致無法正常解析。如一般的格式為下面
復制代碼 代碼如下:
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組件無法解析,需要跟蹤到他的郵件日期處理的代碼,然後進行修改才可以實現正常的郵件日期解析了。

官方的代碼如下所示
復制代碼 代碼如下:
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)));
                    }
                }
            }
        }

需要增加對普通日期格式的修改,修改後的代碼如下所示
復制代碼 代碼如下:
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{

                        //嘗試轉換正常的日期
                        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、由於意外的數據包格式,握手失敗
錯誤信息:[2013-05-04 10:13:54]    System.IO.IOException: 由於意外的數據包格式,握手失敗。

   在 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的配置端口不正確導致,一般的端口必須嚴格按照正常的來填寫。

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

郵箱

Smtp服務器

Smtp端口

POP3服務器

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、郵件標題亂碼問題

錯誤信息:標題出現類似=?utf-8?B?5rWL6K+V6YKu5Lu2?=

錯誤原因:這個是因為編碼的問題,其中=?utf-8?B是表示該段字符為UTF-8的格式,後面的是base64格式的內容。除了utf-8,還可以出現gb2312或者ibm-euccn等格式。為了轉換上面的編碼問題,我寫了一個轉碼函數,如下所示。
復制代碼 代碼如下:
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;
        }

如可以通過代碼吧標題進行轉碼解析
復制代碼 代碼如下:
info.Title = DecodeString(mime_header.Subject);

轉碼後,標題和相關的內容都可以正常顯示了。

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

因為通過分析了解到,由於Lumisoft的Mail_Message.ParseFromByte函數默認只是以UTF8轉換字節,一旦字節為GB2312格式,就會發生轉換亂碼問題,因此先經過Default編碼轉換,然後再以UTF8獲取字節,即可正常轉換郵件頭部。
復制代碼 代碼如下:
byte[] utf8Bytes = Encoding.UTF8.GetBytes(message.HeaderToString());
Mail_Message mime_header = Mail_Message.ParseFromByte(utf8Bytes);

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved