Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when trying to save a message to a folder with Apple mail. #852

Closed
chitoiur opened this issue Apr 29, 2019 · 5 comments
Closed

Issue when trying to save a message to a folder with Apple mail. #852

chitoiur opened this issue Apr 29, 2019 · 5 comments
Labels
compatibility Compatibility with existing software server/icloud Apple iCloud IMAP server server-bug The bug appears to be in the server

Comments

@chitoiur
Copy link

I have an issue when trying to save a message to a folder with Apple mail.

Here is the output:

Connected to imaps://imap.mail.me.com:993/
S: * OK [CAPABILITY st43p00im-tygg10060201 1906B30 XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 SASL-IR AUTH=ATOKEN AUTH=PLAIN] iSCREAM ready to rumble (1906B30-10000-1906B-0-daa0eea8754c:12385) st43p00im-tygg10060201 [15:21604:15:25:00:CF]
C: A00000000 AUTHENTICATE PLAIN XXXXXXXXXXXXXXXXXXX==
S: A00000000 NO [AUTHENTICATIONFAILED] Authentication failed
C: A00000001 LOGIN [email protected] XXXXXXXXXXXXXX
S: A00000001 OK User XXXXXXXXXXXXXX logged in
C: A00000002 CAPABILITY
S: * CAPABILITY XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 ACL RIGHTS=tekx CONDSTORE ENABLE QRESYNC QUOTA NAMESPACE UIDPLUS CHILDREN BINARY UNSELECT SORT CATENATE URLAUTH LANGUAGE ESEARCH ESORT THREAD=ORDEREDSUBJECT THREAD=REFERENCES CONTEXT=SEARCH CONTEXT=SORT WITHIN SASL-IR SEARCHRES METADATA ID XMSEARCH X-SUN-SORT ANNOTATE-EXPERIMENT-1 X-UNAUTHENTICATE X-SUN-IMAP XUM1 MULTISEARCH IDLE
S: A00000002 OK !!
C: A00000003 NAMESPACE
S: * NAMESPACE (("" "/")) NIL NIL
S: A00000003 OK NAMESPACE completed (took 0 ms)
C: A00000004 LIST "" "INBOX"
S: * LIST (\Noinferiors) "/" "INBOX"
S: A00000004 OK LIST completed (took 17 ms)
C: A00000005 LIST "" "%"
S: * LIST (\Noinferiors) "/" "INBOX"
S: * LIST () "/" "Sent Items"
S: A00000005 OK LIST completed (took 17 ms)
C: A00000006 APPEND "Sent Items" {268}
S: + 270
C: From: xxx <[email protected]>
C: Date: Mon, 29 Apr 2019 11:25:00 -0400
C: Subject: Test
C: Message-Id: <[email protected]>
C: To: xxx<[email protected]>
C: MIME-Version: 1.0
C: Content-Type: text/plain; charset=utf-8
C:
C: Test
C:
S: + 10
System.NullReferenceException: Object reference not set to an instance of an object.
   at MailKit.Net.Imap.ImapCommand.StepAsync(Boolean doAsync)
   at MailKit.Net.Imap.ImapEngine.IterateAsync(Boolean doAsync)
   at MailKit.Net.Imap.ImapEngine.RunAsync(ImapCommand ic, Boolean doAsync)
   at MailKit.Net.Imap.ImapFolder.AppendAsync(FormatOptions options, MimeMessage message, MessageFlags flags, Boolean doAsync, CancellationToken cancellationToken, ITransferProgress progress)
   at MailKit.Net.Imap.ImapFolder.Append(FormatOptions options, MimeMessage message, MessageFlags flags, CancellationToken cancellationToken, ITransferProgress progress)
   at MailKit.MailFolder.Append(MimeMessage message, MessageFlags flags, CancellationToken cancellationToken, ITransferProgress progress)
   at ConsoleApp12.Program.Main(String[] args) in C:\Users\xxx\source\repos\ConsoleApp12\ConsoleApp12\Program.cs:line 41

Here is my code:

using System;
using System.Linq;
using MailKit;
using MailKit.Net.Imap;
using MimeKit;

namespace ConsoleApp12
{
    class Program
    {
        private static string[] CommonSentFolderNames = { "Sent Messages", "Sent Items", "Sent Mail", "Sent" };

        static void Main(string[] args)
        {
            using (var client = new ImapClient(new ProtocolLogger(Console.OpenStandardError())))
            {
                try
                {
                    client.Connect("imap.mail.me.com", 993, true);

                    client.Authenticate("[email protected]", "xxx");

                    var personal = client.GetFolder(client.PersonalNamespaces[0]);
                    var subfolders = personal.GetSubfolders(false);
                    // first try to see if any of the common sent folders exist
                    var sentFolder = subfolders.FirstOrDefault(x => x.Name == "Sent Items");

                    if (sentFolder == null)
                    {
                        // then see if the sent folder in the policy exists,
                        // but only if the policy folder is not already in the common folder list (since we already checked there)
                        if (!CommonSentFolderNames.Contains("Sent Items"))
                            sentFolder = subfolders.FirstOrDefault(folder => folder.Name == "Sent Items");

                        // if not, create the folder
                        if (sentFolder == null)
                            sentFolder = personal.Create("Sent Items", true);
                    }

                    sentFolder.Append(GetMimeMessage());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }

        private static MimeMessage GetMimeMessage()
        {
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("[email protected]", "[email protected]"));
            message.To.Add(new MailboxAddress("[email protected]", "[email protected]"));
            message.Subject = "Test";
            message.Body = new TextPart("plain")
            {
                Text = "Test"
            };
            return message;
        }
    }
}

I am using the latest MailKit.

@chitoiur
Copy link
Author

chitoiur commented Apr 29, 2019

I tried with my personal server and that works fine. I noticed it has LITERAL+ capabilities, whereas Apple's does not. I think it has something to do with it.

Connected to imaps://mail.webfaction.com:993/
S: * OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN] Dovecot ready.
C: A00000000 AUTHENTICATE PLAIN xxxxxxxxxxx
S: A00000000 OK [CAPABILITY IMAP4rev1 LITERAL+ 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] 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 (\HasNoChildren) "." INBOX
S: A00000002 OK List completed (0.001 + 0.000 secs).
C: A00000003 LIST "" "%" RETURN (SUBSCRIBED CHILDREN)
S: * LIST (\HasNoChildren) "." Trash
S: * LIST (\Subscribed \HasNoChildren) "." "Sent Items"
S: * LIST (\HasNoChildren) "." INBOX
S: A00000003 OK List completed (0.001 + 0.000 secs).
C: A00000004 APPEND "Sent Items" {268+}
C: From: xxx <[email protected]>
C: Date: Mon, 29 Apr 2019 13:08:14 -0400
C: Subject: Test
C: Message-Id: <[email protected]>
C: To: xxx <[email protected]>
C: MIME-Version: 1.0
C: Content-Type: text/plain; charset=utf-8
C:
C: Test
C:
S: A00000004 OK [APPENDUID 1539188892 2] Append completed (0.034 + 0.083 + 0.001 secs).

@jstedfast
Copy link
Owner

The problem seems to be that after MailKit uploads the message, the server responds with a + meaning that the server expects MailKit to send another literal token yet MailKit has no other literal tokens to send and has no idea what the server expects (and neither do I).

I could make MailKit send an extra CRLF if it gets a + and doesn't have anything else to send, but I'm not even sure if that will work.

@jstedfast
Copy link
Owner

Try using a build from https://www.myget.org/feed/mimekit/package/nuget/MailKit with a version >= 2.1.4.9 (should be up in the next half hour or so).

@chitoiur
Copy link
Author

This is so funny, because I tried exactly that as a band-aid and it works (I was building the source and using it directly)! Thanks for the quick turnaround!

@jstedfast
Copy link
Owner

Oh, cool. No problem :-)

@jstedfast jstedfast added server/icloud Apple iCloud IMAP server compatibility Compatibility with existing software server-bug The bug appears to be in the server labels Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility Compatibility with existing software server/icloud Apple iCloud IMAP server server-bug The bug appears to be in the server
Projects
None yet
Development

No branches or pull requests

2 participants