My python code selects some mails and move them to another folder.
# A.py
def __init__(self):
account = conf.account
password = conf.password
server = conf.imap_server
port = conf.imap_port
self.mail = imaplib.IMAP4_SSL(host=server, port=port)
try:
self.mail.login(account, password)
except imaplib.IMAP4.error as e:
self.logger.error("Cannot login to STMP server: %s" % str(e))
def move_handled_mails(self, mail_list, suffix=0):
archiving_folder = ''.join([str(current_year), '-', str(current_month), '-', str(suffix)])
self.mail.debug = 4
typ, data = self.mail.select(archiving_folder)
if typ != 'OK':
if "NONEXISTENT" in data[0].decode():
self.mail.create(archiving_folder)
typ, data = self.mail.select(archiving_folder)
if "Too many mail in folder" in data[0].decode():
return self.move_handled_mails(mail_list, suffix+1)
if typ != 'OK':
exit()
for num in mail_list:
print(num)
typ, data = self.mail.uid('COPY', num, archiving_folder)
if typ == 'OK':
typ, data = self.mail.uid('STORE', num, '+FLAGS', '(\\Deleted)')
if typ != 'OK':
exit()
print("Executing expunge")
typ, data = self.mail.expunge()
if typ != 'OK':
exit()
def run(self):
res, nums = self.mail.select() # default=INBOX
if res != 'OK':
self.logger.error(nums)
exit()
typ, msgnums = self.mail.uid('search', None, 'ALL')
mail_list = msgnums[0].split()
self.move_handled_mails(mail_list)
At the move_handled_mails method, creating a folder works well, but moving mails doesn't work.
Moreover, when I print all the typs and datas, they are printed as OK and None repectively.
After running the code, no error occurs, but no mail is moved either.
What could be a problem, and how should I debug this problem?
Debug log after adding self.mail.debug=4:
Mail list: [b'7144291']
03:12.87 > b'GBDI2 SELECT 2021-11-0'
03:13.04 < b'* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)'
03:13.04 < b'* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft \\*)] Flags permitted.'
03:13.04 < b'* 13534 EXISTS'
03:13.04 < b'* 0 RECENT'
03:13.04 < b'* OK [UNSEEN 1] First unseen.'
03:13.04 < b'* OK [UIDVALIDITY 119] UIDs valid'
03:13.04 < b'* OK [UIDNEXT 7129395] Predicted next UID'
03:13.04 < b'* OK [NOMODSEQ] No permanent modsequences'
03:13.04 < b'GBDI2 OK [READ-WRITE] Select completed.'
b'7144291'
03:13.04 > b'GBDI3 UID COPY 7144291 2021-11-0'
03:13.16 < b'GBDI3 OK No messages copied.'
03:13.16 > b'GBDI4 UID STORE 7144291 +FLAGS (\\Deleted)'
03:13.30 < b'GBDI4 OK Store completed.'
Executing expunge
03:13.30 > b'GBDI5 EXPUNGE'
03:13.30 < b'GBDI5 OK Expunge completed.'
Don't select the folder you're moving to. UIDs are only meaningful in their source folder. You're switching to the destination folder, then trying to move a message out of it, and that message doesn't exist.
Remove this line:
typ, data = self.mail.select(archiving_folder)
Or, at least, if you need it to check that the destination folder exists, you must switch back to the source folder: self.mail.select('INBOX') or similar.
I use MailKit and IMAP Client. There are no problems with GMail mails, but when I make the same request to another server, problems appear.
Even a simple Search with the criterion MailKit.Search.SearchQuery.NotSeen does not work. I have access to the mailbox and mails are read, but filters do not work.
The only working filter is MailKit.Search.SearchQuery.All. Also I cannot change flag from NotSeen to Seen using Inbox.AddFlags, it works only with GMail.
When I use Fetch method with MessageSummaryItems.BodyStructure it returns TextBody = null.
Does it work correctly only with GMail or I must fix some server settings?
IMAP.log:
Connected to imaps://mail.totalware.gr:993/
S: * OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE NAMESPACE LITERAL+ AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
C: A00000000 AUTHENTICATE PLAIN AGZvc3Rpcm9wb3Vsb3NAdG90YWx3YXJlLmdyAEAhZm9zdGlyb3BvdWxvc0Ah
S: A00000000 OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY PREVIEW=FUZZY STATUS=SIZE NAMESPACE LITERAL+ NOTIFY SPECIAL-USE COMPRESS=DEFLATE QUOTA] Logged in
C: A00000001 NAMESPACE
S: * NAMESPACE (("INBOX." ".")) NIL NIL
S: A00000001 OK Namespace completed (0.001 + 0.000 secs).
C: A00000002 LIST "" "INBOX" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\HasChildren) "." INBOX
S: A00000002 OK List completed (0.017 + 0.000 + 0.016 secs).
C: A00000003 LIST (SPECIAL-USE) "" "*" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\HasNoChildren \Archive) "." INBOX.Archive
S: * LIST (\Subscribed \HasNoChildren \Junk) "." INBOX.spam
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Trash) "." INBOX.Trash
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Sent) "." INBOX.Sent
S: * LIST (\Subscribed \HasNoChildren \Drafts) "." INBOX.Drafts
S: A00000003 OK List completed (0.001 + 0.000 secs).
C: A00000004 SELECT INBOX (CONDSTORE)
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
S: * 21 EXISTS
S: * 0 RECENT
S: * OK [UIDVALIDITY 1513154986] UIDs valid
S: * OK [UIDNEXT 1634] Predicted next UID
S: * OK [HIGHESTMODSEQ 3293] Highest
S: A00000004 OK [READ-WRITE] Select completed (0.002 + 0.000 + 0.001 secs).
C: A00000005 UID SEARCH RETURN () UNSEEN
S: * ESEARCH (TAG "A00000005") UID
S: A00000005 OK Search completed (0.001 + 0.000 secs).
C: A00000006 LOGOUT
S: * BYE Logging out
S: A00000006 OK Logout completed (0.001 + 0.000 secs).
According to the log, the reason that MailKit's Search(SearchQuery.Unseed) returns no results is because your IMAP server is returning 0 matches.
It turns out that some IMAP servers did not implement rfc4731 correctly. Namely, they do not treat RETURN () as equivalent to RETURN (ALL). MailKit 2.11.1 will be more explicit and use RETURN (ALL) to work around this issue.
I want to query my IMAP server with CURL and count the number of emails in my INBOX. Anybody got an idea how I can do this with Curl? Only the following e-mails should be counted:
E-Mails in my INBOX
Unread emails
The mails have a certain FLAG (e.g. Markus or \Flagged)
To illustrate what I mean by FLAGs:
$ curl --url "imaps://mail.example.com/" --user "me#example.com." -X 'SELECT "Inbox"'
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft NonJunk martin $label4 $label5 $label3 Junk $Forwarded $label1 ccw $label2 markus $MDNSent 268435456)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft NonJunk martin $label4 $label5 $label3 Junk $Forwarded $label1 ccw $label2 markus $MDNSent 268435456 \*)] Flags permitted.
* 4 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1553791398] UIDs valid
* OK [UIDNEXT 7246] Predicted next UID
* OK [HIGHESTMODSEQ 27433] Highest
I am running in the following issue with MailKit. My Email Client application is in Idle(Inbox). I am adding a new email from Outlook in the Inbox and I receive CountChanged event in my client(MailKit). Inside CountChanged Event I execute
remoteFolder.Fetch(messagesCount, -1, MessageSummaryItems.All | MessageSummaryItems.UniqueId);
//where messagesCount == 467
I have a breakpoint on this line and while I am on the breakpoint, I am switching to Outlook and I delete random email from the Inbox.
After that I am running the line above. I expect Fetch to return 1 email and MessageExpunge event immediatelly after, but what happens is that Fetch returns 2 emails - with UID 468 and 464. The one with UID 464 doesn't have Envelope property set. I would like to know if this is how MailKit Fetch function is supposed to work? Should I filter the result set of Fetch command with
.Where(mailMessage.Index > messagesCount) to get only the new messages?
This below is the server log
C: A00000045 IDLE
S: + idling
S: * 468 EXISTS
C: DONE
S: A00000045 OK Idle completed (24.713 + 24.713 + 24.712 secs).
C: A00000046 FETCH 468:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE)
S: * 468 FETCH (UID 20873 FLAGS (\Flagged \Seen) INTERNALDATE "25-Aug-2017 16:58:02 +0300" RFC822.SIZE 9648 ENVELOPE ("Fri, 25 Aug 2017 16:57:08 +0300" "Reset Your Zeplin Password" (("Zeplin" NIL "no-reply" "zeplin.io")) (("Zeplin" NIL "no-reply" "zeplin.io")) (("Zeplin" NIL "no-reply" "zeplin.io")) ((NIL NIL "emil.kalchev" "mobisystems.com")) NIL NIL NIL "<30339184.20170825135708.59a02cb4bee526.36618240#mail179-24.suw41.mandrillapp.com>"))
S: * 464 FETCH (MODSEQ (19343) FLAGS (\Flagged \Deleted \Seen))
S: A00000046 OK Fetch completed (0.001 + 0.000 secs).
C: A00000047 IDLE
S: + idling
S: * 464 EXPUNGE
I did a research on https://www.rfc-editor.org/rfc/rfc3501#page-73 and indeed by specification imap server can return "unilateral server decision (e.g.,
flag updates)" in FETCH response.
However I wonder why Mailkit doesn't raise MessageFlagChanged event for UID 464
I am trying to add a document to a Confluence page. I am using this code:
E:\APP\"curl-7.53.1"\src\curl.exe -v -S -u user:password -X POST -H "X-Atlassian-Token: no-check" -F "file=#C:\Users\srvc_mdw_dev\Desktop\conf.txt" -F "comment=this is my file" "http://confluence.example.net/rest/api/content/36375143/child/attachment"
But I get the following error:
./confNote: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported* Trying IPaddress...
* TCP_NODELAY set
* Connected to confluence.example.net (ip address) port 80 (#0)
* Server auth using Basic with user 'user'
> POST /rest/api/content/36375143/child/attachment HTTP/1.1
> Host: example.com> Authorization: Basic c291aGFpbC5vdWFiaUBtZXRyb2V4dGVybmFsLmZyOm1ldHJvNDU2Kg==
> User-Agent: curl/7.53.1
> Accept: */*
> X-Atlassian-Token: no-check> Content-Length: 333
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------646f724e33da0066
>
* HTTP 1.0, assume close after body< HTTP/1.0 302 Found
< Location: https://example.net/rest/api/content/36375143/child/attachment< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
* HTTP error before end of send, stop sending<
* Closing connection 0
******** EDIT ********
Once I add the - L i still get the same error and the following return too :
PS E:\powershell\script> ./conf
* timeout on name lookup is not supported
* Trying IP address...
* TCP_NODELAY set
* Connected to confluence.exemple.net (IP address) port 80 (#0)
> POST /rest/api/content/pageId/child/attachment HTTP/1.1
> Host: confluence.exemple.net
> User-Agent: curl/7.53.1
> Accept: */*
> X-Atlassian-Token: nocheck
> Content-Length: 633874
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------9448fd34591626bc
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 Found
< Location: https://confluence.exemple.net/rest/api/content/pageId/child/attachment
< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
* HTTP error before end of send, stop sending
<
* Closing connection 0
* Issue another request to this URL: 'https://confluence.exemple.net/rest/api/content/pageID/child/attachment'
* Switch from POST to GET
* timeout on name lookup is not supported
* Trying IPaddress...
* TCP_NODELAY set
* Connected to confluence.exemple.net (IPaddress) port 443 (#1)
* schannel: SSL/TLS connection with confluence.exemple.net port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 200 bytes...
* schannel: sent initial handshake data: sent 200 bytes
* schannel: SSL/TLS connection with confluence.exemple.net port 443 (step 2/3)
* schannel: encrypted data buffer: offset 4096 length 4096
* schannel: encrypted data length: 4004
* schannel: encrypted data buffer: offset 4004 length 4096
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection with confluence.exemple.net port 443 (step 2/3)
* schannel: encrypted data buffer: offset 4865 length 5028
* schannel: next InitializeSecurityContext failed: Unknown error (0x80092013) - The revocation function was unable to ch
eck revocation because the revocation server was offline.
* Closing connection 1
* schannel: shutting down SSL/TLS connection with confluence.exemple.net port 443
* Send failure: Connection was reset
* schannel: failed to send close msg: Failed sending data to the peer (bytes written: -1)
* schannel: clear security context handle