使用 ImapX 阅读电子邮件时返回意外字符

Unexpected characters returned when reading email using ImapX

使用 ImapX 阅读电子邮件正文时,末尾出现意外文本。代码的简化版本是

using (var imapClient = new ImapClient(host, port, useSSL))
{
    if (!imapClient.Connect()) 
        throw new Exception(string.Format("Failed to connect to {0}:{1}", host, port));
    if (!imapClient.Login(username, password)) 
        throw new Exception(string.Format("Login failed for {0} connecting to {1}:{2}", username, host, port));
    imapClient.Folders.Inbox.Messages.Download();
    foreach (var message in imapClient.Folders.Inbox.Messages)
    {
        var body = message.Body.HasText ? message.Body.Text : message.Body.Html;
        //Other stuff
    }
    imapClient.Disconnect();
}

当发送带有正文的测试电子邮件时

qwertyqwerqewrqwrqerwqreqrewrq

使用该代码读取的正文是

qwertyqwerqewrqwrqerwqreqrewrq
)
IMAPX10 OK UID completed

有什么方法可以 return 正文文本而末尾没有多余的东西吗?邮件服务器发送了以下内容

SENT: * OK IMAPrev1  
RECEIVED: IMAPX1 CAPABILITY  
SENT: * CAPABILITY IMAP4 IMAP4rev1 CHILDREN IDLE QUOTA SORT ACL NAMESPACE RIGHTS=texk[nl]IMAPX1 OK CAPABILITY completed  
RECEIVED: IMAPX2 LOGIN "the email address" ***  
SENT: IMAPX2 OK LOGIN completed  
RECEIVED: IMAPX3 LIST "" %  
SENT: * LIST (\HasNoChildren) "." "INBOX"[nl]* LIST (\HasNoChildren) "." "Trash"[nl]IMAPX3 OK LIST completed  
RECEIVED: IMAPX4 EXAMINE "INBOX"  
SENT: * 1 EXISTS[nl]* 1 RECENT[nl]* FLAGS (\Deleted \Seen \Draft \Answered \Flagged)[nl]* OK [UIDVALIDITY 1453984022] current uidvalidity[nl]* OK [UIDNEXT 9] next uid[nl]* OK [PERMANENTFLAGS ()] limited[nl]IMAPX4 OK [READ-ONLY] EXAMINE completed  
RECEIVED: IMAPX5 EXAMINE "Trash"  
SENT: * 0 EXISTS[nl]* 0 RECENT[nl]* FLAGS (\Deleted \Seen \Draft \Answered \Flagged)[nl]* OK [UIDVALIDITY 1454000202] current uidvalidity[nl]* OK [UIDNEXT 1] next uid[nl]* OK [PERMANENTFLAGS ()] limited[nl]IMAPX5 OK [READ-ONLY] EXAMINE completed  
RECEIVED: IMAPX6 SELECT "INBOX"  
SENT: * 1 EXISTS[nl]* 1 RECENT[nl]* FLAGS (\Deleted \Seen \Draft \Answered \Flagged)[nl]* OK [UIDVALIDITY 1453984022] current uidvalidity[nl]* OK [UIDNEXT 9] next uid[nl]* OK [PERMANENTFLAGS (\Deleted \Seen \Draft \Answered \Flagged)] limited[nl]IMAPX6 OK [READ-WRITE] SELECT completed  
RECEIVED: IMAPX7 EXAMINE "INBOX"  
SENT: * 1 EXISTS[nl]* 0 RECENT[nl]* FLAGS (\Deleted \Seen \Draft \Answered \Flagged)[nl]* OK [UIDVALIDITY 1453984022] current uidvalidity[nl]* OK [UIDNEXT 9] next uid[nl]* OK [PERMANENTFLAGS ()] limited[nl]IMAPX7 OK [READ-ONLY] EXAMINE completed  
RECEIVED: IMAPX8 UID SEARCH ALL  
SENT: * SEARCH 8  
SENT: IMAPX8 OK UID completed  
RECEIVED: IMAPX9 UID FETCH 8 (FLAGS INTERNALDATE RFC822.SIZE BODY.PEEK[HEADER.FIELDS (FROM TO DATE SUBJECT CC CONTENT-TYPE)] BODYSTRUCTURE)  
SENT: * 1 FETCH (UID 8 RFC822.SIZE 2553 FLAGS (\Seen) INTERNALDATE "29-Jan-2016 14:47:58 +0000" BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "7bit" 6 2)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "7bit" 27 2) "ALTERNATIVE" ("BOUNDARY" "001a113fefbe6b8657052a7a1da1") NIL NIL) BODY[HEADER.FIELDS (FROM TO DATE SUBJECT CC CONTENT-TYPE)] {235}  
SENT: )  
SENT: IMAPX9 OK UID completed  
RECEIVED: IMAPX10 UID FETCH 8 (BODY.PEEK[1.MIME] BODY.PEEK[1])  
SENT: * 1 FETCH (UID 8 BODY[1.MIME] {43}  
SENT:  BODY[1] {6}  
SENT: )  
SENT: IMAPX10 OK UID completed  
RECEIVED: IMAPX11 UID FETCH 8 (BODY.PEEK[2.MIME] BODY.PEEK[2])  
SENT: * 1 FETCH (UID 8 BODY[2.MIME] {42}  
SENT:  BODY[2] {27}  
SENT: )  
SENT: IMAPX11 OK UID completed  
RECEIVED: IMAPX12 SELECT "INBOX"  
SENT: * 1 EXISTS[nl]* 0 RECENT[nl]* FLAGS (\Deleted \Seen \Draft \Answered \Flagged)[nl]* OK [UIDVALIDITY 1453984022] current uidvalidity[nl]* OK [UIDNEXT 9] next uid[nl]* OK [PERMANENTFLAGS (\Deleted \Seen \Draft \Answered \Flagged)] limited[nl]IMAPX12 OK [READ-WRITE] SELECT completed  
RECEIVED: IMAPX13 UID STORE 8 +FLAGS (\Seen \Deleted)  
SENT: * 1 FETCH (FLAGS (\Deleted \Seen) UID 8)  
SENT: IMAPX13 OK UID completed  
RECEIVED: IMAPX14 SELECT "INBOX"  
SENT: * 1 EXISTS[nl]* 0 RECENT[nl]* FLAGS (\Deleted \Seen \Draft \Answered \Flagged)[nl]* OK [UIDVALIDITY 1453984022] current uidvalidity[nl]* OK [UIDNEXT 9] next uid[nl]* OK [PERMANENTFLAGS (\Deleted \Seen \Draft \Answered \Flagged)] limited[nl]IMAPX14 OK [READ-WRITE] SELECT completed  
RECEIVED: IMAPX15 EXPUNGE  

我已经抛出了同样的问题。几个小时后,我发现正则表达式模式序列存在问题。

从 codeplex 下载最新的源代码,只需更改下面描述的顺序即可。

转到>>

MessageContent.CS

& 找到以下代码

else if (CommandEndRex.IsMatch(data))
            {
                for (var i = _contentBuilder.Length - 1; i >= 0; i--)
                {
                    if (_contentBuilder[i] == ')')
                    {
                        _contentBuilder.Remove(i, _contentBuilder.Length - i);
                        return;
                    }
                }
            }

继续在前面的 else if 语句之上执行一步,就完成了。

如果您仍然遇到同样的问题,请告诉我..

编码愉快。

另一种更简单的方法是,如果您不特别需要从那时起 changed/fixed/added 的任何东西,请降级到 ImapX 版本 2.0.0.15。也许一些中间版本也可以,但这肯定有效。