MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

Client.Search()

Open TKYV opened this issue 5 years ago • 49 comments

Sorry to raise this question here about client. Search()

Can you provide a link to imapclient. Noop() or imapfolder. Check material?

There are the following questions

I don't quite understand what you said. After client 1 changes, it will not be synchronized to client 2?

But I called the search interface again

TKYV avatar Aug 08 '21 16:08 TKYV

ImapClient.NoOp() ImapFolder.Check()

I don't quite understand what you said. After client 1 changes, it won't be synchronized to client 2?

Correct. Not all IMAP server implementations synchronize their connections correctly. QQ obviously doesn't, for example.

But I called the search interface again

Doesn't matter. It's up to the IMAP server to synchronize. It's a bug (or a "feature") in the server.

jstedfast avatar Aug 08 '21 20:08 jstedfast

Yes, I use QQ email, The first search query is OK. When I change the status, I search again, and the query results are still the results of the last time

And I try the check method as follows

folder.Open(FolderAccess.ReadWrite); folder.Check(); var uniqueIds = folder.Search(SearchQuery.NotSeen);

Then you can't

I think it's a QQ email. I want to hear your experience. What should be the problem

TKYV avatar Aug 09 '21 01:08 TKYV

The way some IMAP servers are implemented, they fork the state between each connection so each connection can make changes that aren't synchronized to the other connections immediately.

Sometimes the state is only different until the client leaves and comes back to the folder where some message flags were changed.

Think of it like this:

  • Client1 and Client2 connect and both open the Inbox.
  • Each client is actually connected to its own server instance and so when the clients open the Inbox, each server instance loads the state of the folder into memory.
  • Client1 makes changes to some message flags on Server1 and maybe it does or maybe it doesn't get saved to disk, but it doesn't matter because Server2 already has what it thinks is the folder's state fully loaded into memory and so doesn't check the disk again.
  • Client2 does a SEARCH query to check for the flag changes, but Server2 has the folder's state in memory, so it just performs the search on the in-memory representation of the flags from when Client1 requested that the Inbox be opened.

This is why closing the folder and then re-opening it can sometimes work-around the issue.

The CHECK command can also help sometimes because it is supposed to make the server re-synchronize its in-memory representation with its on-disk representation, but most servers seem to do nothing with this command, so it's not very reliable.

The NOOP command is only officially meant to keep the connection alive, but a lot of clients also use it to get the latest updates of flag changes, etc from the server and so many servers actually handle NOOP commands by re-synchronizing with the disk to see if anything has changed since they last loaded state from disk. It's an unofficial expectation, but it can work.

Hope that helps. I don't have any other suggestions for you other than you could always try disconnecting and reconnecting, but that isn't a practical solution.

jstedfast avatar Aug 09 '21 02:08 jstedfast

Well, according to your explanation I tried check and didn't solve it The code written now is also added folder.Close(); client.Disconnect(true); The problem still exists, so I don't think it's the code problem. It should be caused by an individual IMAP mail server, such as QQ. I hope you can try. This scenario does exist

TKYV avatar Aug 09 '21 03:08 TKYV

If it's an individual server problem, I'm not sure what code you expect me to add to MailKit to fix this issue.

It's a bug in the server.

If you disconnect and reconnect and it's still returning the wrong results for SEARCH, then there's nothing I can do about it.

jstedfast avatar Aug 09 '21 13:08 jstedfast

This is not the first bug report I've seen about QQ IMAP server's bugginess regarding the SEARCH command.

Here are some others:

  • issue #946
  • issue #1143

jstedfast avatar Aug 09 '21 13:08 jstedfast

But it hasn't been solved for so long

TKYV avatar Aug 09 '21 13:08 TKYV

Maybe you should consider using a better quality IMAP server? :)

jstedfast avatar Aug 09 '21 13:08 jstedfast

Is there a good solution

TKYV avatar Aug 09 '21 13:08 TKYV

dovecot is a good quality IMAP server that I feel good about recommending.

jstedfast avatar Aug 09 '21 13:08 jstedfast

See, I thought IMAP server was the reason at the beginning Dovecot supports docking with other mailboxes, such as QQ

TKYV avatar Aug 09 '21 14:08 TKYV

Another question is that if the attachment exceeds 20m, it will report an error without attachment

image

TKYV avatar Aug 13 '21 11:08 TKYV

If you can't get more than 20m through mailkit, you will be prompted that there is no attachment, but Foxmail can read it. Why

TKYV avatar Aug 13 '21 11:08 TKYV

I don't understand what you are saying. Show me the SMTP and/or IMAP protocol logs.

jstedfast avatar Aug 13 '21 13:08 jstedfast

I use this Foxmail tool, which is actually an IMAP service. I can get attachments, but I can't get them with your mailkit

Attachments cannot be obtained after 20m

TKYV avatar Aug 13 '21 13:08 TKYV

I've used MailKit to download attachments larger than 20 MB before, so this isn't a limitation in MailKit.

There might be a bug somewhere, but I will need protocol logs to figure out what the problem is.

When you create the ImapClient instance, pass the .ctor a new ProtocolLogger ("imap.log"):

using (var client = new ImapClient (new ProtocolLogger ("imap.log")) {
    ...
}

jstedfast avatar Aug 13 '21 13:08 jstedfast

Will imap.log be overwritten every time a request is made

TKYV avatar Aug 13 '21 13:08 TKYV

Connected to imaps://imap.exmail.qq.com:993/
S: * OK [CAPABILITY IMAP4 IMAP4rev1 ID AUTH=PLAIN AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready
C: A00000000 AUTHENTICATE PLAIN
S: +
C: AHRpYW5jaGFuZ0BjMTY1MC5vbmV4bWFpbC5jb20AV1NmdXBvMjMzMw==
S: A00000000 OK Success login ok
C: A00000001 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 XLIST MOVE IDLE XAPPLEPUSHSERVICE NAMESPACE CHILDREN ID UIDPLUS
S: A00000001 OK CAPABILITY Completed
C: A00000002 NAMESPACE
S: * NAMESPACE (("" "/")) NIL NIL
S: A00000002 OK NAMESPACE Success
C: A00000003 LIST "" "INBOX"
S: * LIST (\HasNoChildren) "/" "INBOX"
S: A00000003 OK LIST completed
C: A00000004 XLIST "" "*"
S: * XLIST (\NoSelect \HasChildren) "/" "&UXZO1mWHTvZZOQ-"
S: * XLIST (\HasNoChildren \Drafts) "/" "Drafts"
S: * XLIST (\HasNoChildren \Junk) "/" "Junk"
S: * XLIST (\HasNoChildren \Trash) "/" "Deleted Messages"
S: * XLIST (\HasNoChildren \Sent) "/" "Sent Messages"
S: * XLIST (\HasNoChildren \Inbox) "/" "INBOX"
S: A00000004 OK XLIST completed
C: A00000005 SELECT INBOX
S: * 65 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 1]
S: * OK [UIDVALIDITY 1628767717] UID validity status
S: * OK [UIDNEXT 68] Predicted next UID
S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * OK [PERMANENTFLAGS (\* \Answered \Flagged \Deleted \Draft \Seen)] Permanent flags
S: A00000005 OK [READ-WRITE] SELECT complete
C: A00000006 UID SEARCH UNSEEN
S: * SEARCH 62
S: A00000006 OK UID SEARCH completed
C: A00000007 UID FETCH 62 (BODY.PEEK[])
S: * 60 FETCH (UID 62 BODY[] {5306}
S: X-QQ-GoodBg: 1
S: X-QQ-SSF: 0011000000000030
S: X-QQ-FEAT: TFgIKou4GCmCF5ew7Y1lnUMBBqrpSdLp+9KRlPLmjNE=
S: X-QQ-BUSINESS-ORIGIN: 2
S: X-Originating-IP: 116.237.77.112
S: X-QQ-STYLE: 
S: X-QQ-mid: llogic20t1628853514t9088412
S: From: "=?utf-8?B?5Y2i6I2j5o2V?=" <[email protected]>
S: To: "=?utf-8?B?dGlhbmNoYW5n?=" <[email protected]>
S: Subject: =?utf-8?B?5aSn5LqOMjBNQuWOi+e8qeWMheaWh+S7tuaPkA==?=
S:  =?utf-8?B?56S6?=
S: Mime-Version: 1.0
S: Content-Type: multipart/alternative;
S: 	boundary="----=_NextPart_6116550A_1AA5DF80_58F6DE34"
S: Content-Transfer-Encoding: 8Bit
S: Date: Fri, 13 Aug 2021 19:18:34 +0800
S: X-Priority: 3
S: Message-ID: <[email protected]>
S: X-QQ-MIME: TCMime 1.0 by Tencent
S: X-Mailer: QQMail 2.x
S: X-QQ-Mailer: QQMail 2.x
S: 
S: This is a multi-part message in MIME format.
S: 
S: ------=_NextPart_6116550A_1AA5DF80_58F6DE34
S: Content-Type: text/plain;
S: 	charset="utf-8"
S: Content-Transfer-Encoding: base64
S: 
S: 5LuO6IW+6K6v5LyB5Lia6YKu566x5Y+R5p2l55qE6LaF5aSn6ZmE5Lu2DQoNCjIwTUItMjAu
S: cmFyICgyMC42MU0sIDIwMjHlubQwOeaciDEy5pelIDE5OjE4IOWIsOacnynov5vlhaXkuIvo
S: vb3pobXpnaLvvJpodHRwOi8vbWFpbC5xcS5jb20vY2dpLWJpbi9mdG5FeHNfZG93bmxvYWQ/
S: dD1leHNfZnRuX2Rvd25sb2FkJmFtcDtrPTI3MzM2NDMzMzQ3YzhlY2MxZTFjMDcxZTE3NjIw
S: ZjAwNDk1NjAyMDA1NjUyMGYwNjA1MWU1YzA2NTQwNDE1MDY1NTA0MDUxZTUxNTEwYjU2NGIw
S: YjAyNTI1NjAxNWMwMTU2NTcwMTAwMDU2MjMzMzI1NDAzMjk3MTFkNTAwODFjMTQ1MjE2MzMw
S: ZSZhbXA7Y29kZT1mM2QzMGI4MiZhbXA7ZmlkPTcyL2VmM2YwNzRjLTg1ZGYtNDM3YS1hMzNk
S: LThmYWZjZDMwZGUzNQ==
S: 
S: ------=_NextPart_6116550A_1AA5DF80_58F6DE34
S: Content-Type: text/html;
S: 	charset="utf-8"
S: Content-Transfer-Encoding: base64
S: 
S: PGRpdj48Zm9udD48YnI+PC9mb250PjwvZGl2PjxkaXY+PGluY2x1ZGV0YWlsPjwhLS08IVtl
S: bmRpZl0tLT48L2luY2x1ZGV0YWlsPjwvZGl2PjxkaXYgaWQ9IlFRTWFpbEJpZ0F0dGFjaCIg
S: c3R5bGU9InBhZGRpbmc6IDJweDsgbWFyZ2luLWJvdHRvbTogMTVweDtiYWNrZ3JvdW5kLWNv
S: bG9yOiNFMEVDRjk7d2lkdGg6YXV0bztmb250LWZhbWlseTpWZXJkYW5hLEFyaWFsLFRhaG9t
S: YTtmb250LXNpemU6MTRweDsiPjxociBzdHlsZT0iZGlzcGxheTpub25lOyI+PGRpdiBzdHls
S: ZT0idGV4dC1hbGlnbjpsZWZ0O3BhZGRpbmc6IDZweCAwcHQgMTBweCA2cHg7Ij48YiBzdHls
S: ZT0iZm9udC1zaXplOiAxNHB4OyI+PGltZyBib3JkZXI9IjAiIGFsaWduPSJhYnNtaWRkbGUi
S: IHN0eWxlPSJtYXJnaW4tcmlnaHQ6NHB4OyIgc3JjPSJodHRwOi8vcmVzLm1haWwucXEuY29t
S: L3poX0NOL2h0bWxlZGl0aW9uL2ltYWdlcy9pY29uX2F0dC5naWYiPuS7juiFvuiur+S8geS4
S: mumCrueuseWPkeadpeeahOi2heWkp+mZhOS7tjwvYj48L2Rpdj48ZGl2IH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A00000009 LOGOUT
S: * BYE LOGOUT received
S: A00000009 OK LOGOUT Completed

TKYV avatar Aug 13 '21 13:08 TKYV

Will imap.log be overwritten every time a request is made

It will be overwritten every time you create a new ImapClient instance

jstedfast avatar Aug 13 '21 13:08 jstedfast

@TKYV based on your log, your IMAP server is not providing the attachment to MailKit even though MailKit asked for the whole message.

Seems like a bug in your IMAP server.

jstedfast avatar Aug 13 '21 13:08 jstedfast