When the component obtains POP3 emails, it is found that most of the emails can be obtained normally. However, for some special emails, there seems to be conversion errors, or garbled codes and some garbled codes. Some are in the title or the address of the email recipient, while others are in the content. In order to better organize related issues, I wrote this article, hoping to be of some help to everyone using this component.
1. Date conversion error problem.
Error message: [2013-05-04 10:49:03] An error occurred when converting email: Account wuhuacong@ Email title: ICP????????????????????????????????????wuhuacong)
: Header field 'Date' parsing failed.
In .Mail_Message.get_Date()
In .() position...\:Line number 160
Cause of error:Due to the different date content formats of the email format, it cannot be parsed normally. As the general format is as follows
Message-ID: <d74841c5887b4df692ebdb7ec7802054@4782e72954a24cc89535840ea2e5da5b>
Date: Fri, 26 Apr 2013 08:56:52 GMT
Mime-Version: 1.0
From: "wuhuacong2013@" <wuhuacong2013@>
To: "wuhuacong@" <wuhuacong@>
Some email date formats are 2013-05-06 19:01:44, and the Lumisoft component cannot be parsed. It needs to track the code for processing the email date, and then modify it to achieve normal email date parsing.
The official code is as follows
public DateTime Date
{
get{
if(){
throw new ObjectDisposedException(().Name);
}
MIME_h h = ("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 ;
}
}
set{
if(){
throw new ObjectDisposedException(().Name);
}
if(value == ){
("Date");
}
else{
MIME_h h = ("Date");
if(h == null){
(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
else{
(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
}
}
}
You need to add modification to the normal date format, the modified code is as follows
public DateTime Date
{
get{
if(){
throw new ObjectDisposedException(().Name);
}
MIME_h h = ("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 = (dateString, out dt);
if (success)
{
return dt;
}
else
{
throw new ParseException("Header field 'Date' parsing failed.");
}
}
}
else{
return ;
}
}
set{
if(){
throw new ObjectDisposedException(().Name);
}
if(value == ){
("Date");
}
else{
MIME_h h = ("Date");
if(h == null){
(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
else{
(new MIME_h_Unstructured("Date",MIME_Utils.DateTimeToRfc2822(value)));
}
}
}
}
2. Handshake failed due to unexpected packet format
Error message: [2013-05-04 10:13:54] : Handshake failed due to unexpected packet format.
In .TCP_Client.Connect(String host, Int32 port, Boolean ssl)
In () position......\:Line number 123
In (MailSendConfigInfo) location ...........\:Line number 66
Cause of error: Due to the incorrect configuration port of POP3, the general port must be filled in strictly according to normal.
Common configuration instructions for SMTP and POP3: Mail Smtp Server Smtp port POP3 server POP3 port Using SSL 465 995 true 25 110 true smtp. 25 pop. 110 false 25 110 false other 25 110 false
3. Garbage code of email title
Error message: Similar to the title =?utf-8?B?5rWL6K+V6YKu5Lu2?=
Cause of error:This is because of encoding problems, where =?utf-8?B is the format that indicates that the segment character is UTF-8, followed by the content in base64 format. In addition to utf-8, formats such as gb2312 or ibm-euccn can also appear. To convert the above encoding problem, I wrote a transcoding function as shown below.
private string DecodeString(string input)
{
string regex = @"=\?(?<encode>.*?)\?B\?(?<body>.*?)\?=";
Regex re = new Regex(regex, | | );
MatchCollection mcs = (input);
foreach (Match mc in mcs)
{
string encode = ["encode"].Value;
if (!(encode))
{
if (().Contains("euccn") || ().Contains("euc-cn") ||
().Contains("gbk"))
{
encode = "gb2312";
}
else if (().Contains("utf8"))
{
encode = "utf-8";
}
string body = ["body"].Value;
byte[] bytes = Convert.FromBase64String(body);
string result = (encode).GetString(bytes);
input = (, result);
}
}
return input;
}
If you can transcode and analyze the code title
= DecodeString(mime_header.Subject);
After transcoding, the title and related content can be displayed normally.
In addition to the above transcoding operation, there is another better method that can enable the email-related information to be displayed normally.
Because it is learned through analysis that since Lumisoft's Mail_Message.ParseFromByte function only converts bytes in UTF8 by default, once the bytes are in GB2312 format, a garbled conversion problem will occur. Therefore, first passes Default encoding and then obtains bytes in UTF8 to convert the email header normally.
byte[] utf8Bytes = Encoding.(());
Mail_Message mime_header = Mail_Message.ParseFromByte(utf8Bytes);
The titles and email headers obtained in this way are normal.