In-depth Lumisoft. NET component development and troubleshooting

Source: Internet
Author: User

In Lumisoft. when the NET component obtains POP3 mails, it finds that most mails can be obtained normally. However, for some special emails, it seems that conversion errors or garbled characters may always occur, some are in the title or email recipient's address, while some are in the content. In order to better sort out related issues, I wrote this article, hoping to help you use this component.

1. date conversion error.
Error message: [10:49:03] Date of converted mail error: account wuhuacong@163.com mail title: ICP ??????????????????????? Wuw.cong)

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

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

At the position of WHC. PlugInService. Pop3Helper. Receive ()... \ Pop3Helper. cs: row number 160

Error cause:Due to the date and content formats of different mail formats, normal resolution fails. The format is as follows:

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

When the mail date format is 19:01:44, The Lumisoft component cannot parse the mail date. You need to track the code for processing the mail date and modify it to parse the normal mail date.

The official code is as follows:

Copy codeThe Code is as follows: 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 )));
}
}
}
}

To modify the normal date format, the modified Code is as follows:
Copy codeThe Code is as follows: 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 the 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 data packet format
Error message: [10:13:54] System. IO. IOException: handshaking failed due to unexpected data packet format.

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

At the position of WHC. PlugInService. SmtpHelper. Send () ...... \ SmtpHelper. cs: row number 123

At the position of WHC. PlugInService. SendMailService. DataThreadHandle (MailSendConfigInfo info) ...... \ SendMailService. cs: row number 66

Error cause: The POP3 Configuration Port is incorrect. Generally, the port must be set as normal.

Common SMTP and POP3 mail configurations:

Email

Smtp Server

Smtp Port

POP3 server

POP3 port

Use 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

Others

Smtp.test.com

25

Pop.test.com

110

False

3. garbled email titles

Error message: the title appears like =? UTF-8? B? 5rWL6K + V6YKu5Lu2? =

Error cause:This is because of Encoding Problems, where =? UTF-8? B indicates that the character segment is in UTF-8 format, followed by base64 format content. In addition to UTF-8, you can also see gb2312, ibm-euccn, and other formats. To solve the above encoding problem, I wrote a transcoding function, as shown below.

Copy codeThe Code is as follows: 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;
}

For example, you can use the title of the code bar for transcoding resolution.Copy codeThe Code is as follows: info. Title = DecodeString (mime_header.Subject );

After transcoding, the title and related content can be displayed normally.

In addition to the above transcoding operations, there is also a better way to make the mail-related information properly displayed.

According to the analysis, the Mail_Message.ParseFromByte function of Lumisoft only converts UTF-8 bytes by Default. Once the bytes are in GB2312 format, conversion garbled characters will occur. Therefore, Default encoding is used first, then you can use UTF8 to obtain the bytes to convert the mail header.

Copy codeThe Code is as follows: byte [] utf8Bytes = Encoding. UTF8.GetBytes (message. HeaderToString ());
Mail_Message mime_header = Mail_Message.ParseFromByte (utf8Bytes );

In this way, the obtained title and mail header information are normal.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.