I created some code in C# to read the email from a folder on my (local/NAS) IMAP server. Code is not finished, but in the end I would like to have a list of all the mailsubjects.
The response from below code is:
Total messages: 93
MessageError 0: The IMAP server has unexpectedly disconnected.
This question: "The IMAP server has unexpectedly disconnected" using 993 port and MailKit did not solve the problem, because I also had it when accessing via port 993, and now via port 143 i still have the same problem.
When checking that folder using Thunderbird I do see those 93 messages (and I am able to read those messages).
Any help would be appreciated to be able to read those messages.
public static void CheckIMAP()
{
using (var client = new ImapClient(new ProtocolLogger(@"d:\temp\imap.log")))
{
//client.Connect(mailserver, 993, true);
client.Connect(mailserver, 993, SecureSocketOptions.SslOnConnect);
//client.Connect(mailserver, 143, SecureSocketOptions.None);
client.Authenticate(username, password);
IMailFolder folder = client.GetFolder("INBOX.subfolder");
var fo = folder.Open(FolderAccess.ReadOnly);
Console.WriteLine ("Total messages: {0}", folder.Count);
for (int i = 0; i < folder.Count; i++)
{
try
{
var message = folder.GetMessage(i);
Console.WriteLine("Message: {0}", message.Subject);
}
catch (Exception ex)
{
System.Console.WriteLine($"MessageError {i}: {ex.Message}");
break;
}
}
System.Console.WriteLine();
client.Disconnect(true);
}
}
The end result of this program should be almost (because date of the message should also be in the result, but that's not the question right now ) the same as this Linux command running on my NAS:
$ grep '^Subject:' ~/.Maildir/.INBOX.subfolder/cur/*
Versions:
Mailkit: 4.10.0; Dotnet: 9.0.200
Developing on Windows 11, when finished it should run under Linux (on my Synology NAS)
EDIT:
The contents of the imap.log
:
Connected to imaps://example:993/
S: * OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN] Dovecot ready.
C: A00000000 AUTHENTICATE PLAIN ********
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 PREVIEW STATUS=SIZE SAVEDATE LITERAL+ NOTIFY SPECIAL-USE] Logged in
C: A00000001 NAMESPACE
S: * NAMESPACE (("" ".")) NIL NIL
S: A00000001 OK Namespace completed (0.001 + 0.000 secs).
C: A00000002 LIST "" "INBOX" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\Subscribed \HasChildren) "." INBOX
S: A00000002 OK List completed (0.076 + 0.000 + 0.075 secs).
C: A00000003 LIST (SPECIAL-USE) "" "*" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Drafts) "." Drafts
S: * LIST (\Subscribed \HasNoChildren \Sent) "." Sent
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Junk) "." Junk
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Trash) "." Trash
S: A00000003 OK List completed (0.007 + 0.000 + 0.006 secs).
C: A00000004 LIST "" INBOX.subfolder RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\Subscribed \HasNoChildren) "." INBOX.subfolder
S: A00000004 OK List completed (0.007 + 0.000 + 0.006 secs).
C: A00000005 EXAMINE INBOX.subfolder (CONDSTORE)
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: * OK [PERMANENTFLAGS ()] Read-only mailbox.
S: * 93 EXISTS
S: * 0 RECENT
S: * OK [UIDVALIDITY 1516544306] UIDs valid
S: * OK [UIDNEXT 103] Predicted next UID
S: * OK [HIGHESTMODSEQ 158] Highest
S: A00000005 OK [READ-ONLY] Examine completed (0.005 + 0.000 + 0.004 secs).
C: A00000006 FETCH 1 (BODY.PEEK[])
S: * 1 FETCH (BODY[] {950}
S: Return-Path: <[email protected]>
S: X-Original-To: [email protected]
S: Delivered-To: [email protected]
S: Received: from <obfuscated>
S: (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
S: key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
S: (No client certificate requested)
S: by example (Postfix) with ESMTPSA id BB6801EF0A5
S: for <[email protected]>; Thu, 11 Apr 2024 20:14:49 +0200 (CEST)
S: X-Is-Generated-Message-Id: true
S: MIME-Version: 1.0
S: Content-Transfer-Encoding: quoted-printable
S: Content-Type: text/plain;
S: charset=UTF-8
S: Subject: <obfuscated>
S: From: Luuk <[email protected]>
S: Date: Thu, 9 Apr 2024 20:14:48 +0200
S: To: [email protected]
S: Message-ID: <[email protected]>
S: X-MailScanner-ID: BB6801EF0A5.A5150
S: X-MailScanner: Found to be clean
S: X-MailScanner-From: [email protected]
S: X-Spam-Status: No
S:
S:
I created some code in C# to read the email from a folder on my (local/NAS) IMAP server. Code is not finished, but in the end I would like to have a list of all the mailsubjects.
The response from below code is:
Total messages: 93
MessageError 0: The IMAP server has unexpectedly disconnected.
This question: "The IMAP server has unexpectedly disconnected" using 993 port and MailKit did not solve the problem, because I also had it when accessing via port 993, and now via port 143 i still have the same problem.
When checking that folder using Thunderbird I do see those 93 messages (and I am able to read those messages).
Any help would be appreciated to be able to read those messages.
public static void CheckIMAP()
{
using (var client = new ImapClient(new ProtocolLogger(@"d:\temp\imap.log")))
{
//client.Connect(mailserver, 993, true);
client.Connect(mailserver, 993, SecureSocketOptions.SslOnConnect);
//client.Connect(mailserver, 143, SecureSocketOptions.None);
client.Authenticate(username, password);
IMailFolder folder = client.GetFolder("INBOX.subfolder");
var fo = folder.Open(FolderAccess.ReadOnly);
Console.WriteLine ("Total messages: {0}", folder.Count);
for (int i = 0; i < folder.Count; i++)
{
try
{
var message = folder.GetMessage(i);
Console.WriteLine("Message: {0}", message.Subject);
}
catch (Exception ex)
{
System.Console.WriteLine($"MessageError {i}: {ex.Message}");
break;
}
}
System.Console.WriteLine();
client.Disconnect(true);
}
}
The end result of this program should be almost (because date of the message should also be in the result, but that's not the question right now ) the same as this Linux command running on my NAS:
$ grep '^Subject:' ~/.Maildir/.INBOX.subfolder/cur/*
Versions:
Mailkit: 4.10.0; Dotnet: 9.0.200
Developing on Windows 11, when finished it should run under Linux (on my Synology NAS)
EDIT:
The contents of the imap.log
:
Connected to imaps://example:993/
S: * OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN] Dovecot ready.
C: A00000000 AUTHENTICATE PLAIN ********
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 PREVIEW STATUS=SIZE SAVEDATE LITERAL+ NOTIFY SPECIAL-USE] Logged in
C: A00000001 NAMESPACE
S: * NAMESPACE (("" ".")) NIL NIL
S: A00000001 OK Namespace completed (0.001 + 0.000 secs).
C: A00000002 LIST "" "INBOX" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\Subscribed \HasChildren) "." INBOX
S: A00000002 OK List completed (0.076 + 0.000 + 0.075 secs).
C: A00000003 LIST (SPECIAL-USE) "" "*" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Drafts) "." Drafts
S: * LIST (\Subscribed \HasNoChildren \Sent) "." Sent
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Junk) "." Junk
S: * LIST (\Subscribed \HasNoChildren \UnMarked \Trash) "." Trash
S: A00000003 OK List completed (0.007 + 0.000 + 0.006 secs).
C: A00000004 LIST "" INBOX.subfolder RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\Subscribed \HasNoChildren) "." INBOX.subfolder
S: A00000004 OK List completed (0.007 + 0.000 + 0.006 secs).
C: A00000005 EXAMINE INBOX.subfolder (CONDSTORE)
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: * OK [PERMANENTFLAGS ()] Read-only mailbox.
S: * 93 EXISTS
S: * 0 RECENT
S: * OK [UIDVALIDITY 1516544306] UIDs valid
S: * OK [UIDNEXT 103] Predicted next UID
S: * OK [HIGHESTMODSEQ 158] Highest
S: A00000005 OK [READ-ONLY] Examine completed (0.005 + 0.000 + 0.004 secs).
C: A00000006 FETCH 1 (BODY.PEEK[])
S: * 1 FETCH (BODY[] {950}
S: Return-Path: <[email protected]>
S: X-Original-To: [email protected]
S: Delivered-To: [email protected]
S: Received: from <obfuscated>
S: (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
S: key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
S: (No client certificate requested)
S: by example (Postfix) with ESMTPSA id BB6801EF0A5
S: for <[email protected]>; Thu, 11 Apr 2024 20:14:49 +0200 (CEST)
S: X-Is-Generated-Message-Id: true
S: MIME-Version: 1.0
S: Content-Transfer-Encoding: quoted-printable
S: Content-Type: text/plain;
S: charset=UTF-8
S: Subject: <obfuscated>
S: From: Luuk <[email protected]>
S: Date: Thu, 9 Apr 2024 20:14:48 +0200
S: To: [email protected]
S: Message-ID: <[email protected]>
S: X-MailScanner-ID: BB6801EF0A5.A5150
S: X-MailScanner: Found to be clean
S: X-MailScanner-From: [email protected]
S: X-Spam-Status: No
S:
S:
Share
Improve this question
edited Mar 9 at 15:32
Luuk
asked Mar 9 at 13:49
LuukLuuk
15.1k5 gold badges27 silver badges44 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 1Instead of downloading full messages to generate a "message list", use the following code:
public static void CheckIMAP()
{
using (var client = new ImapClient())
{
client.Connect(mailserver, 143, SecureSocketOptions.None);
client.Authenticate(username, password);
IMailFolder folder = client.GetFolder("INBOX.subfolder");
var fo = folder.Open(FolderAccess.ReadOnly);
Console.WriteLine ("Total messages: {0}", folder.Count);
var summaries = folder.Fetch(0, -1, MessageSummaryItems.Envelope | MessageSummaryItems.UniqueId);
for (int i = 0; i < summaries.Count; i++)
{
try
{
Console.WriteLine("Message: {0}", summaries[i].Envelope.Subject);
}
catch (Exception ex)
{
System.Console.WriteLine($"MessageError {i}: {ex.Message}");
break;
}
}
System.Console.WriteLine();
client.Disconnect(true);
}
}
When you want to download the actual message, use summaries[i].UniqueId
like this:
var message = folder.GetMessage(summaries[i].UniqueId);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744869683a4598183.html
new ImapClient()
tonew ImapClient(new ProtocolLogger("imap.log"))
(feel free to use a full path instead of just "imap.log"). Not sure how to get server logs for your server. – jstedfast Commented Mar 9 at 15:18ImapFolder.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope)
instead of downloading full messages just to build the message list. Later, when you click on a message, Thunderbird might be downloading the full message, but if it does, it is using the UID and not the message index. This might explain why Thunderbird works and your code does not. The server might have a bug when asking for a message by index instead of by UID. – jstedfast Commented Mar 9 at 15:26new ProtocolLogger("imap.log")
, and the output of theimap.log
. Strange thing it that the content of the mail message is in the imap.log (It is s short message with only a SUBJECT) – Luuk Commented Mar 9 at 15:34