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

Get this error when I append the message to the sent folder using imap client #251

Closed
karthik67 opened this issue Oct 16, 2015 · 27 comments
Closed
Labels
bug Something isn't working

Comments

@karthik67
Copy link

Hi, I get the below error when I append the message to the sent folder. the same code has worked before. But for some reason, it stopped working.
Error is below:
The IMAP server replied to the 'APPEND' command with a 'BAD' response: Additional arguments found after last expected argument

My code is below:

using (var imapClient = new ImapClient(new NullProtocolLogger()))
{
    var imapSecurity = emailConfiguration.IMAPSecurityConfiguration;
    var imapSecurityEnum = imapSecurity.Trim().ToLower().Contains("ssl")
        ? SecureSocketOptions.SslOnConnect
        : SecureSocketOptions.StartTls;

    imapClient.Connect(emailConfiguration.IMAPServer, emailConfiguration.IMAPPort,
        imapSecurityEnum);
    imapClient.AuthenticationMechanisms.Remove("XOAUTH2");
    imapClient.Authenticate(emailConfiguration.UserName,
        emailConfiguration.Password);

    var personalFolders = imapClient.GetFolders(imapClient.PersonalNamespaces[0]);

    var sentFolder = personalFolders.FirstOrDefault(r => r.Name.ToLower().Contains("sent"));
    if (sentFolder != null)
    {
        sentFolder.Open(FolderAccess.ReadWrite);
        sentFolder.Append(message);
        sentFolder.Close();
    }
}
@jstedfast
Copy link
Owner

Can you get me a protocol log? I need to see what the command is so that I can verify that it is correct.

On my IMAP servers, this works as expected so I cannot reproduce the error. I suspect your IMAP server is broken.

@karthik67
Copy link
Author

where can I get the protocol log? BTW, I am trying to connect to yahoo imap server. Weird thing is it worked few minutes back. then it stopped working.

@karthik67
Copy link
Author

Hi, just another piece of information. we are evaluating another product and with that, yahoo works. I just tested Hotmail and Hotmail works fine. so just yahoo with the mailkit. thanks Karthik

@jstedfast
Copy link
Owner

change this line:

using (var imapClient = new ImapClient(new NullProtocolLogger()))

to this:

using (var imapClient = new ImapClient(new ProtocolLogger ("imap.log"))

That will log all of the communications to a file called imap.log

@jstedfast
Copy link
Owner

I happen to have an IMAP account on yahoo.com, so I've tested it with the following MailKit commands and all of them work properly as expected:

client.Inbox.Append (message);
client.Inbox.Append (message, MessageFlags.Seen);
client.Inbox.Append (message, MessageFlags.Seen, DateTimeOffset.Now);
client.Inbox.Append (message, MessageFlags.None, DateTimeOffset.Now);

They result in the following IMAP commands being sent to the server:

A00000005 APPEND Inbox {1174+}
A00000005 APPEND Inbox (\Seen) {1174+}
A00000005 APPEND Inbox (\Seen) "16-Oct-2015 16:01:08 -0400" {1174+}
A00000005 APPEND Inbox "16-Oct-2015 16:01:24 -0400" {1174+}

@karthik67
Copy link
Author

let me try the same with the imap log.

@karthik67
Copy link
Author

hi Jeff,
I have attached the log file. It again worked. but second time onwards, it throws this error. Can you try more than once on your side as well?
Rgds
Karthik

Connected to imaps://imap.mail.yahoo.com:993/
S: * OK [CAPABILITY IMAP4rev1 ID NAMESPACE X-ID-ACLID UIDPLUS LITERAL+ CHILDREN XAPPLEPUSHSERVICE XYMHIGHESTMODSEQ AUTH=PLAIN AUTH=LOGIN AUTH=XOAUTH2 AUTH=XYMCOOKIE AUTH=XYMECOOKIE AUTH=XYMCOOKIEB64 AUTH=XYMPKI] IMAP4rev1 imapgate-1.8.1_01.20166 imap1021.mail.gq1.yahoo.com
C: B00000000 AUTHENTICATE PLAIN
S: +
C: AGthcnRoaWtzdW5kaHVAeWFob28uY29tAHNoeWthcl8xOA==
S: B00000000 OK AUTHENTICATE completed - Mailbox size in bytes is 122896506
C: B00000001 CAPABILITY
S: * CAPABILITY IMAP4rev1 ID NAMESPACE X-ID-ACLID UIDPLUS LITERAL+ XAPPLEPUSHSERVICE XYMHIGHESTMODSEQ
S: B00000001 OK CAPABILITY completed
C: B00000002 NAMESPACE
S: * NAMESPACE (("" "/")) NIL NIL
S: B00000002 OK NAMESPACE completed
C: B00000003 LIST "" "INBOX"
S: * LIST (\HasNoChildren) "/" "Inbox"
S: B00000003 OK LIST completed
C: B00000004 LIST "" "%"
S: * LIST (\HasNoChildren) "/" "Bulk Mail"
S: * LIST (\HasNoChildren) "/" "Archive"
S: * LIST (\HasNoChildren) "/" "Draft"
S: * LIST (\HasNoChildren) "/" "Inbox"
S: * LIST (\HasNoChildren) "/" "Sent"
S: * LIST (\HasNoChildren) "/" "Trash"
S: B00000004 OK LIST completed
C: B00000005 SELECT Sent
S: * 19 EXISTS
S: * 0 RECENT
S: * OK [UIDVALIDITY 1444052409] UIDs valid
S: * OK [UIDNEXT 35] Predicted next UID
S: * FLAGS (\Answered \Deleted \Draft \Flagged \Seen $Forwarded $Junk $NotJunk)
S: * OK [PERMANENTFLAGS (\Answered \Deleted \Draft \Flagged \Seen $Forwarded $Junk $NotJunk)] Permanent flags
S: * OK [HIGHESTMODSEQ 25]
S: B00000005 OK [READ-WRITE] SELECT completed; now in selected state
C: B00000006 APPEND Sent {3151+}
C: Sender: [email protected]
C: From: [email protected]
C: To: [email protected]
C: Subject: test 2
C: MIME-Version: 1.0
C: Content-Type: text/html; charset=us-ascii
C: Content-Transfer-Encoding: quoted-printable
C:
C: <style class=3D'editorStyle'>body{font-family: Arial, 'Hel=
C: vetica', sans-serif;font-size: 12px;color: #333;background-color: #fff=
C: ;margin: 20px;}p, div{font-family: Arial, 'Helvetica', sans-serif;font=
C: -size: 12px;}.cke_editable{font-size: 12px;line-height: 1.6;}blockquot=
C: e{font-style: italic;font-family: Georgia, Times, serif;padding: 2px 0=
C: ;border-style: solid;border-color: #ccc;border-h:0;}.cke_contents_ltr =
C: blockquote{padding-left: 20px;padding-right: 8px;border-left-width: 5p=
C: x;}.cke_contents_rtl blockquote{padding-left: 8px;padding-right: 20px;=
C: border-right-width: 5px;}a{color: #0782C1;}ol,ul,dl{/* IE7: reset rtl =
C: list margin. ) _/_margin-right: 0px;/* preserved spaces for list items=
C: with text direction other than the list. (#6249,#8049)*/padding: 0 40=
C: px;}h1,h2,h3,h4,h5,h6{font-weight: normal;line-height: 1.2;}hr{border:=
C: 0px;border-top: 1px solid #ccc;}img.right{border: 1px solid #ccc;floa=
C: t: right;margin-left: 15px;padding: 5px;}img.left{border: 1px solid #c=
C: cc;float: left;margin-right: 15px;padding: 5px;}pre{white-space: pre-w=
C: rap;word-wrap: break-word;-moz-tab-size: 4;-o--: 4;-webkit-tab-size: 4=
C: ;tab-size: 4;}.marker{background-color: Yellow;}span[lang]{font-style:=
C: italic;}figure{text-align: center;border: solid 1px #ccc;border-radiu=
C: s: 2px;background: rgba(0,0,0,0.05);padding: 10px;margin: 10px 20px;di=
C: splay: ne-;}figure > figcaption{text-align: center;display: block;}a >=
C: img {padding: 1px;margin: 1px;border: none;outline: 1px solid #0782C1=
C: ;}</style>

<div style=3D"font-family:arial; font-size=
C: : 12px;">
C: <p style=3D"text-align: left; "> 


C:  
C:
C:

 


C:
C:

<span style=3D"font-family:courier new,courier,monospace;"><img src=
C: =3D"http://docandimages.blob.core.windows.net/c7653ba5-de72-4d19-a94c-=
C: 260ad0191eff/48b37882-3f4b-43a8-9bf8-51c11b1b3e08" width=3D"180" heigh=
C: t=3D"40"> 


C:
C:

<span style=3D"font-family:courier new,courier,monospace;"><img src=
C: =3D"http://docandimages.blob.core.windows.net/c7653ba5-de72-4d19-a94c-=
C: 260ad0191eff/cd26d455-16e2-4d3f-ba3a-bc5206b9e264" width=3D"180" heigh=
C: t=3D"40"> 


C:
C:

<span style=3D"font-family:courier new,courier,monospace;">Sometime=
C: s this didn't work but I noticed it was because the page of the iframe=
C: wasn't completelly rendered. In this case, and I never noticed any fu=
C: rther problem, put the instruction inside:


C:
C:

<span style=3D"font-family:courier new,courier,monospace;">But insp=
C: ect the size of your 'content' element you adapt to, because often the=
C: element is much greater than the visible content.


C:
C:

<span style=3D"font-family:courier new,courier,monospace;">But insp=
C: ect the size of your 'content' element you adapt to, because often the=
C: element is much greater than the visible content.


C:
C:

 


C:

C:
S: B00000006 BAD [CLIENTBUG] Additional arguments found after last expected argument

@jstedfast
Copy link
Owner

What versions of MimeKit and MailKit are you using? The problem appears to be a miscalculation of the size of the message.

@jstedfast
Copy link
Owner

Actually, that's not the problem. The number of bytes is being calculated correctly after all.

The problem seems to be that the message itself does not end with a CRLF.

My guess is that the Yahoo! mail server appended the message just fine in the first session's APPEND command, but the second session failed because their mailbox file did not end with a newline from the previous APPEND or something like that.

@karthik67
Copy link
Author

Jeff, I will try adding a new line and give it a try. thanks.

@jstedfast
Copy link
Owner

Once I add a newline to the end of your message, it works reliably.

@jstedfast jstedfast added the bug Something isn't working label Oct 16, 2015
@jstedfast
Copy link
Owner

I've landed a fix in MimeKit to make sure that writing a MimeMessage to a stream, the result will always end with a new-line.

Out of curiosity, what other IMAP library were you looking at?

@jstedfast
Copy link
Owner

I've released MimeKit 1.2.14 to NuGet with the fix for this bug.

I've also released MailKit 1.2.13 to NuGet.

@karthik67
Copy link
Author

you are awesome. Thanks for the quick turn around.

Date: Mon, 19 Oct 2015 04:45:07 -0700
From: [email protected]
To: [email protected]
CC: [email protected]
Subject: Re: [MailKit] Get this error when I append the message to the sent folder using imap client (#251)

I've released MimeKit 1.2.14 to NuGet with the fix for this bug.

I've also released MailKit 1.2.13 to NuGet.


Reply to this email directly or view it on GitHub.

@edgiardina
Copy link

I am still seeing a flood of these errors connecting to YAHOO's IMAP; we are on Mailkit 2.0.3

@jstedfast
Copy link
Owner

@edgiardina do you know if your messages end with a newline? That seems to have been what the original problem was.

@edgiardina
Copy link

So we process thousands of messages a day via IMAP. The emails we process are varied and come from a large variety of customers and EMLs. I'm sure some end in newlines, but I doubt 80% of them do, which is what our error rate in IMAP to YAHOO is.

My working theory is that Yahoo's IMAP servers are behind a round robin DNS issue and 4 of their 5 reject messages frequently for some common denominator which I haven't determined. If you want to see a failing EML I can email you or PM you here, but in my testing most of the EMLs we have fail.

@jstedfast
Copy link
Owner

message.WriteTo (stream); should make sure that all messages end in a new-line. Could you verify that for a message that fails?

@edgiardina
Copy link

@jstedfast We are using MimeMessage.Load() ... grabbing a test message, the one I grabbed doesn't appear to have a newline at the end

image

@edgiardina
Copy link

If I manually add a newline to the stream it seems to fix yahoo's issues for our case

memory.WriteByte(0x0A);
memory.Seek(0, SeekOrigin.Begin);		
mimeKitMessage = MimeMessage.Load(memory);

@jstedfast
Copy link
Owner

Okay, then the problem is that when MimeKit parses an existing message, it doesn't enforce the message stream to end with a newline sequence because it wasn't there when the message was parsed.

It only enforces newlines when the message was constructed by code as in making use of new MimeMessage (); ...

jstedfast added a commit to jstedfast/MimeKit that referenced this issue May 7, 2018
jstedfast added a commit that referenced this issue May 7, 2018
@jstedfast
Copy link
Owner

I probably won't get a chance to make another release for another 2 weeks or so, but the above commits should fix the issue.

@edgiardina
Copy link

Thanks Jeffrey. adding it on the Imap append is perfect. I think the reason these emails don't have problems otherwise is that they're typically sent via SMTP not dropped in an IMAP folder. Appreciate the attention to this!

@jstedfast
Copy link
Owner

No problem.

@atiyka
Copy link

atiyka commented Mar 4, 2019

Hi,
I faced with the same issue. When I try to append a message I'm getting this error: The IMAP server replied to the 'APPEND' command with a 'BAD' response: Additional arguments found after last expected argument.
I have the latest version of the library and I tried to append a forwarded message from a gmail account to an aol account. (I think that this cause the problem, because in other cases everything is working fine).
I attached the log file. Do you have any idea how to solve the append problem?

Thanks in advance!
imap.log

@jstedfast
Copy link
Owner

I don't see any problems based on the log. Do you have problems with any other IMAP servers?

@atiyka
Copy link

atiyka commented Mar 5, 2019

I tested now with another imap server (outlook) without any problem. Then probably the problem is something aol specific.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants