jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.19k stars 822 forks source link

Client.Search() #1241

Closed TKYV closed 3 years ago

TKYV commented 3 years ago

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

jstedfast commented 3 years ago

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.

TKYV commented 3 years ago

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

jstedfast commented 3 years ago

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:

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.

TKYV commented 3 years ago

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

jstedfast commented 3 years ago

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 commented 3 years ago

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

Here are some others:

TKYV commented 3 years ago

But it hasn't been solved for so long

jstedfast commented 3 years ago

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

TKYV commented 3 years ago

Is there a good solution

jstedfast commented 3 years ago

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

TKYV commented 3 years ago

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

TKYV commented 3 years ago

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

image

TKYV commented 3 years ago

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

jstedfast commented 3 years ago

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

TKYV commented 3 years ago

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

jstedfast commented 3 years ago

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")) {
    ...
}
TKYV commented 3 years ago

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

TKYV commented 3 years ago
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?=" <lrb@gooneymc.com>
S: To: "=?utf-8?B?dGlhbmNoYW5n?=" <tianchang@c1650.onexmail.com>
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: <tencent_5D14955B0BCE31A979DD731F@qq.com>
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+PGluY2x1ZGV0YWlsPjwhLS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5LiL6L296aG16Z2iPC9hPjxz
S: cGFuIHN0eWxlPSJkaXNwbGF5Om5vbmU7Ij7vvJpodHRwOi8vbWFpbC5xcS5jb20vY2dpLWJp
S: bi9mdG5FeHNfZG93bmxvYWQ/dD1leHNfZnRuX2Rvd25sb2FkJmFtcDtrPTI3MzM2NDMzMzQ3
S: YzhlY2MxZTFjMDcxZTE3NjIwZjAwNDk1NjAyMDA1NjUyMGYwNjA1MWU1YzA2NTQwNDE1MDY1
S: NTA0MDUxZTUxNTEwYjU2NGIwYjAyNTI1NjAxNWMwMTU2NTcwMTAwMDU2MjMzMzI1NDAzMjk3
S: MTFkNTAwODFjMTQ1MjE2MzMwZSZhbXA7Y29kZT1mM2QzMGI4MiZhbXA7ZmlkPTcyL2VmM2Yw
S: NzRjLTg1ZGYtNDM3YS1hMzNkLThmYWZjZDMwZGUzNTwvc3Bhbj48L2Rpdj48L2Rpdj48L2Rp
S: dj48L2Rpdj48L2Rpdj48L2Rpdj48L2Rpdj4=
S: 
S: ------=_NextPart_6116550A_1AA5DF80_58F6DE34--
S: 
S: )
S: A00000007 OK UID FETCH Completed
C: A00000008 UID STORE 62 FLAGS.SILENT (\Seen)
S: A00000008 OK UID STORE Completed
C: A00000009 LOGOUT
S: * BYE LOGOUT received
S: A00000009 OK LOGOUT Completed
jstedfast commented 3 years ago

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

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

jstedfast commented 3 years ago

@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.

TKYV commented 3 years ago

Yes, mailkit didn't get the attachment

TKYV commented 3 years ago

It's not the fault of the IMAP server. The IMAP service I linked with Foxmail tool can get attachments, but I can't get them with mailkit's IMAP. I don't know why, as long as it exceeds 20m, I can't get them, and I'm not good at giving feedback to others

jstedfast commented 3 years ago

It's not the fault of the IMAP server.

Oh? How is it not the fault of the IMAP server if it did not include the attachment in the response to MailKit's request?

The IMAP service I linked with Foxmail tool can get attachments

Then it's not using IMAP like you think it is.

Here's the IMAP specification: https://datatracker.ietf.org/doc/html/rfc3501

Please explain to me how MailKit is at fault by showing me how the command that MailKit sends is wrong or by showing me the correct IMAP command to use.

jstedfast commented 3 years ago

Tell you what, try this:

var uids = new UniqueIdSet ();
uids.Add (new UniqueId (62));

var summaries = client.Inbox.Fetch (uids, MessageSummaryItems.BodyStructure);

I want to see the log of that. Let's see if QQMail even knows that the message has attachments or not.

Maybe Foxmail is asking for individual parts and reconstructing the message rather than asking for the whole message like you are doing in your code.

TKYV commented 3 years ago

Yes, you seem to be effective, but how to write the specific download attachment code?

TKYV commented 3 years ago

var summaries = client.Inbox.Fetch(uids, MessageSummaryItems.BodyStructure); foreach (var summarie in summaries) { foreach (var attachment in summarie.Attachments) { var entity = client.Inbox.GetBodyPart(uniqueId, attachment); } }

TKYV commented 3 years ago

I use the above method to get the attachment, but I still can't get it This is Tencent's corporate email. I don't know why Foxmail can get attachments alone or can't get them

jstedfast commented 3 years ago

What does the protocol log show?

TKYV commented 3 years ago
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?=" <lrb@gooneymc.com>
S: To: "=?utf-8?B?dGlhbmNoYW5n?=" <tianchang@c1650.onexmail.com>
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: <tencent_5D14955B0BCE31A979DD731F@qq.com>
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: bG9yOiNFMEVDRjk7d2lkdGg6YXV0bztmb250LW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TE3NjIwZjAwNDk1NjAyMDA1NjUyMGYwNjA1MWU1YzA2NTQwNDE1MDY1
S: NTA0MDUxZTUxNTEwYjU2NGIwYjAyNTI1NjAxNWMwMTU2NTcwMTAwMDU2MjMzMzI1NDAzMjk3
S: MTFkNTAwODFjMTQ1MjE2MzMwZSZhbXA7Y29kZT1mM2QzMGI4MiZhbXA7ZmlkPTcyL2VmM2Yw
S: NzRjLTg1ZGYtNDM3YS1hMzNkLThmYWZjZDMwZGUzNTwvc3Bhbj48L2Rpdj48L2Rpdj48L2Rp
S: dj48L2Rpdj48L2Rpdj48L2Rpdj48L2Rpdj4=
S: 
S: ------=_NextPart_6116550A_1AA5DF80_58F6DE34--
S: 
S: )
S: A00000007 OK UID FETCH Completed
C: A00000008 UID FETCH 62 BODYSTRUCTURE
S: * 60 FETCH (UID 62 BODYSTRUCTURE (("TEXT" "PLAIN" ("charset" "utf-8") NIL NIL "BASE64" 542 9 NIL NIL NIL)("TEXT" "HTML" ("charset" "utf-8") NIL NIL "BASE64" 3666 51 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "----=_NextPart_6116550A_1AA5DF80_58F6DE34") NIL NIL))
S: A00000008 OK UID FETCH Completed
C: A00000009 UID STORE 62 FLAGS.SILENT (\Seen)
S: A00000009 OK UID STORE Completed
C: A00000010 LOGOUT
S: * BYE LOGOUT received
S: A00000010 OK LOGOUT Completed
jstedfast commented 3 years ago
C: A00000008 UID FETCH 62 BODYSTRUCTURE
S: * 60 FETCH (UID 62 BODYSTRUCTURE (("TEXT" "PLAIN" ("charset" "utf-8") NIL NIL "BASE64" 542 9 NIL NIL NIL)("TEXT" "HTML" ("charset" "utf-8") NIL NIL "BASE64" 3666 51 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "----=_NextPart_6116550A_1AA5DF80_58F6DE34") NIL NIL))
S: A00000008 OK UID FETCH Completed

Based on this, the IMAP server does not see the attachment.

If we parse that BODYSTRUCTURE response, we get the following MIME structure:

multipart/alternative; boundary="----=_NextPart_6116550A_1AA5DF80_58F6DE34"
    text/plain; charset="utf-8"
    text/html; charset="utf-8"

The BODYSTRUCTURE response does not list any attachments.

TKYV commented 3 years ago

I don't understand. Can't you get it? But there are attachments
You can see that the Foxmail tool in my picture above is also using IMAP service What should I do

jstedfast commented 3 years ago

I looked at your screenshot again. It doesn't fetch the attachment. It gives you a link to the attachment that is located on the web.

If you base64 decode your HTML body part, the link is there.

TKYV commented 3 years ago
                                    var uids = new UniqueIdSet();
                                    uids.Add(uniqueId);
                                    var summaries = client.Inbox.Fetch(uids, MessageSummaryItems.BodyStructure);
                                    foreach (var summarie in summaries)
                                    {
                                        if (summarie.Attachments.Count() > 0)
                                        {
                                            foreach (var attachment in summarie.Attachments)
                                            {
                                                var saveFilePath = Path.Combine(basePath, "EmailCollFiles", from.Address, Guid.NewGuid().ToString("N"));
                                                var dir = new DirectoryInfo(saveFilePath);
                                                if (!dir.Exists) dir.Create();
                                                // 像我们对内容所做的那样下载附件
                                                var entity = client.Inbox.GetBodyPart(uniqueId, attachment);
                                                // 附件可以是message / rfc822部件或常规MIME部件
                                                var messagePart = entity as MessagePart;
                                                if (messagePart != null)
                                                {
                                                    var rfc822 = messagePart;
                                                    var filePath = Path.Combine(saveFilePath, attachment.PartSpecifier + ".eml");
                                                    await rfc822.Message.WriteToAsync(filePath);
                                                    logStr.AppendFormat("附件名: {0} 路径: {0} 已经被下载。", attachment.FileName, filePath).AppendLine();
                                                }
                                                else
                                                {
                                                    var part = (MimePart)entity;
                                                    //下载附件
                                                    using (var cancel = new System.Threading.CancellationTokenSource())
                                                    {
                                                        var filePath = Path.Combine(saveFilePath, attachment.FileName);
                                                        using (var stream = File.Create(filePath))
                                                            await part.Content.DecodeToAsync(stream, cancel.Token);
                                                        logStr.AppendFormat("附件名: {0} 路径: {0} 已经被下载。", attachment.FileName, filePath).AppendLine();
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            logStr.AppendLine("附件信息:【无附件信息】");
                                            var exec = SendMail(from.Address, "邮箱收取失败", "无附件信息!", ref logStr);
                                        }
                                    }

Excuse me, how should I change my code

TKYV commented 3 years ago

The above is my code. Where should I change it?

jstedfast commented 3 years ago

Just download the full message like you did before, get the HtmlBody, parse the HTML using htmlagilitypack or some such library, find all the <a href=...> links and download them.

TKYV commented 3 years ago

var html = folder.GetBodyPart(summary.UniqueId, summary.HtmlBody);

is this one?

jstedfast commented 3 years ago

Sure.

TKYV commented 3 years ago

但是 我有一个疑问 第一个 我这边应该是附件上传,为什么会出现在HtmlBody内 第二个问题 是在HtmlBody 我怎么区分 那个是附件?

But I have a question

The first one on my side should be attachment upload. Why does it appear in the htmlbody

The second question is how do I distinguish between attachments in the HTML body?

TKYV commented 3 years ago

text `从腾讯企业邮箱发来的超大附件

20MB-20.rar (20.61M, 2021年09月12日 19:18 到期)进入下载页面:http://mail.qq.com/cgi-bin/ftnExs_download?t=exs_ftn_download&amp;k=27336433347c8ecc1e1c071e17620f004956020056520f06051e5c06540415065504051e51510b564b0b025256015c015657010005623332540329711d50081c145216330e&amp;code=f3d30b82&amp;fid=72/ef3f074c-85df-437a-a33d-8fafcd30de35`

html

`


`

TKYV commented 3 years ago

image

TKYV commented 3 years ago

This is the case when opening the internal. How can I get attachments?

TKYV commented 3 years ago

Is there a standard object?

jstedfast commented 3 years ago

There is no standard object. You are on your own. Good luck!

TKYV commented 3 years ago

Well, the reason may be that you choose to upload super large attachments,

Is this the case? Mailkit can't get attachments. It can only be obtained through the web page. Has anyone encountered such a problem before

TKYV commented 3 years ago

Moreover, I really want to be unable to obtain this attachment. In HTML, I send you an example. Do you think there is any way to download the attachment?

TKYV commented 3 years ago

http://mail.qq.com/cgi-bin/ftnExs_download?t=exs_ftn_download&k=27336433347c8ecc1e1c071e17620f004956020056520f06051e5c06540415065504051e51510b564b0b025256015c015657010005623332540329711d50081c145216330e&code=f3d30b82&fid=72/ef3f074c-85df-437a-a33d-8fafcd30de35 I can only get this link

TKYV commented 3 years ago

I looked at your screenshot again. It doesn't fetch the attachment. It gives you a link to the attachment that is located on the web.

If you base64 decode your HTML body part, the link is there.

This sentence is very important, but I have to judge by what he points to the attachment link

Based on that parameter?

I look forward to your reply

rogatec commented 2 years ago

Hey @jstedfast, sorry to answer in a close question, but I thought this question could fit into here (at least a bit).

Is it possible to check the imap server if deleting a message or updating a message (e.g. adding a header to the e-mail) is allowed/permitted? I had a customer configuration of an imap server where updating a header would cause an error message from the imap server, which I never saw before. Regards

jstedfast commented 2 years ago

@rogatec If the IMAP server supports ACLs, then you can check the ACLs via the GetMyAccessRights() method.