溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Lumisoft.NET組件開發(fā)碰到亂碼等問題的解決方法

發(fā)布時間:2021-07-22 22:45:41 來源:億速云 閱讀:140 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Lumisoft.NET組件開發(fā)碰到亂碼等問題的解決方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Lumisoft.NET組件開發(fā)碰到亂碼等問題的解決方法”吧!

在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出錯:賬號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:行號 160

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

復(fù)制代碼 代碼如下:


Message-ID: <d74841c5887b4df692ebdb7ec7802054@4782e72954a24cc89535840ea2e5da5b>
Date: Fri, 26 Apr 2013 08:56:52 GMT
Mime-Version: 1.0
From: "wuhuacong2013@163.com" <wuhuacong2013@163.com>
To: "wuhuacong@96900.com.cn" <wuhuacong@96900.com.cn>


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

官方的代碼如下所示

復(fù)制代碼 代碼如下:


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ù)制代碼 代碼如下:


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)致,一般的端口必須嚴格按照正常的來填寫。

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

郵箱

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)題亂碼問題

錯誤信息:標(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ù)制代碼 代碼如下:


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)題進行轉(zhuǎn)碼解析

復(fù)制代碼 代碼如下:


info.Title = DecodeString(mime_header.Subject);


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

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

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

復(fù)制代碼 代碼如下:


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


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

感謝各位的閱讀,以上就是“Lumisoft.NET組件開發(fā)碰到亂碼等問題的解決方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Lumisoft.NET組件開發(fā)碰到亂碼等問題的解決方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI