Deep Lumisoft. NET component development encountered messy code and other problems to solve

  • 2020-06-07 04:23:52
  • OfStack

In Lumisoft. NET component to get POP3 email, found that most can normal access to email, but for some special email, always seem to appear conversion errors, or garbled words and part of the code, some in title or email recipient address, while others in content, in order to better organize related problem, wrote this article, want to use this component to everybody there 1 help.

1. Date conversion error.
Error message: E-mail 10:49:03 [2013-05-04] conversion Date error: account wuhuacong@163.com email title: ICP????????????????????????????????? wuhuacong)

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

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

In WHC. PlugInService. Pop3Helper. Receive (position)... \ Pop3Helper cs: line number 160

Error: Unable to parse properly due to a different date content format in the message format. The format as 1 is shown below


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>

Some email date formats are 2013-05-06 19:01:44, then Lumisoft component cannot parse, so it needs to track the code of its email date processing, and then make modifications to achieve normal email date parsing.

The official code is shown below


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

You need to add changes to the normal date format, as shown below

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{
                        // Try to convert a normal date 
                        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. Handshake failed due to unexpected packet format
System. IO. IOException: Handshake failed due to unexpected packet format.

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

In WHC. PlugInService. SmtpHelper. Send (position)... \ SmtpHelper cs: line number 123

In WHC. PlugInService. SendMailService. DataThreadHandle (MailSendConfigInfo info) position... \ SendMailService cs: line number 66

Error: Due to the incorrect configuration of the POP3 port, 1-like ports must be filled exactly as normal.

Email SMTP and POP3 Common configuration Description:

email

Smtp server

Smtp port

POP3 server

POP3 port

Using 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

other

smtp.test.com

25

pop.test.com

110

false

3. Confused email subject line

Error message: Title appears similar to =? utf - 8? B? 5 rWL6K + V6YKu5Lu2? =

Error reason: This is due to coding problem, where =? utf - 8? B is the format for es214EN-8, followed by base64. In addition to utf-8, formats such as gb2312 or ES218en-ES219en can also appear. To translate the coding problem above, I wrote a transcoding function, as shown below.


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

If you can transcode the title through the code

info.Title = DecodeString(mime_header.Subject);

After transcoding, the title and related content are displayed normally.

In addition to the above transcoding operation, there is a better way to make the relevant message display properly.

It is understood through analysis that the es231EN_ES232en.ParseFromByte function of Lumisoft only converts the bytes in UTF8 by default, and the one denier byte is in GB2312 format. Therefore, after the Default encoding conversion, and then gets the bytes in UTF8, the message headers can be converted normally.


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

The headers and the headers are normal.


Related articles: