When reading the body of an email using ImapX there is unexpected text at the end. A simplified version of the code is
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();
}
and when a test email was sent with body
qwertyqwerqewrqwrqerwqreqrewrq
the body which was read using that code was
qwertyqwerqewrqwrqerwqreqrewrq
)
IMAPX10 OK UID completed
Is there any way I can return the body text without the extra stuff at the end? The mail server sent the following
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
I have gone threw the same issue. After passing couple of hours I found that there was issue in regex pattern sequence.
Download the latest source from codeplex and just change the sequence describe below.
Go to the >>
MessageContent.CS
& locate following code
else if (CommandEndRex.IsMatch(data))
{
for (var i = _contentBuilder.Length - 1; i >= 0; i--)
{
if (_contentBuilder[i] == ')')
{
_contentBuilder.Remove(i, _contentBuilder.Length - i);
return;
}
}
}
and keep up it one step above the previous else if statement and it's done.
Let me know if u still face the same issue..
happy coding.