-
-
Notifications
You must be signed in to change notification settings - Fork 818
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
SmtpClient.DataAsync Adds Extra CRLF #895
Comments
This was a solution to fix messages that do not end with a new-line. |
Doesn't |
FYI, you always currently force So, removing the extra CRLF on EndData would not seem to have any other impact than preventing the extra CRLF from being sent? |
Yea, the EnsureNewLine thing on FormatOptions was added later and I didn't update the SmtpClient logic. Not sure if I just forgot or if I just wasn't 100% confident at the time that there weren't any corner cases where it didn't work. Pretty sure it's a solved problem when you write to a Dos2UnixFilter or a Unix2DosFilter because the Flush() logic tracks what the last character written was. and makes sure to write a new-line sequence when flushed if it hasn't already ended with one. I can probably remove it from SmtpClient, but I need time to verify that it won't break anything. |
Changing EndData to ".\r\n" seems to work based on the unit tests I've got currently but I'm not 100% confident that there are no corner cases where MimeMessage.WriteTo() will not end with a newline sequence (even when FormatOptions.EnsureNewLine is enabled). |
…pported Partial fix for issue #895
Now that SmtpClient.Send*() will always use the BDAT command if the CHUNKING extension is supported, I've updated the Send*() code to not force-enable EnsureNewLine in that case. In other words, this is now a non-issue when the SMTP server supports the CHUNKING extension. |
I think changing EndData to ".\r\n", is the right way to go. With the forced CRLF on DATA commands, it really should be a non-issue. However, defaulting EnsureNewLine to true is probably a good thing for most users. We'd prefer to have control and the option to disable it because we're controlling it in other ways and it would help us catch problems, but it should default to true. Most people wouldn't disable it and WriteTo would be consistent with what gets sent via SmtpClient.Send. Most mail servers will still treat CHUNKING/BDAT like DATA in terms of requirements of ending messages in CRLF, so BDAT won't save many of those errors. It will ensure however that they'll at least get an error message back rather than a timeout waiting for a never coming CRLF that happens with DATA or the mail server may just add the CRLF itself. For example, here are some interesting tidbits surrounding BDAT CRLF from my notes:
Interesting bug surrounding CRLF BDAT a while ago in Exim: From the CHUNKING RFC:
|
Good point about the SMTP server just adding the ending newline itself if it doesn't exist anyway. I've re-added for EnsureNewLine. |
I didn't add this change to the v2.3.0 release that I just made because this change is still somewhat risky but I wanted to get v2.3.0 released with all of the fixes I've done so far over the past 2 months. The corner cases you found in MimeKit that didn't properly write a newline character have been fixed so I'm guessing it's probably safe now but I gotta see if I can figure out a way to add MimeKit unit tests that cover more potential cases to feel confident enough to make this change. |
Makes sense. If you put it into the post-release MyGet builds, I'm comfortable putting it into use and reporting back on any issues as well. |
…Data to be ".\r\n" This ensures that we do not add an extraneous new-line sequence to the end of messages being sent. Fixes issue #895
Not sure why I didn't think of this simple solution yesterday... but the above commit modifies the SmtpDataFilter to ensure that the output ends with a CRLF sequence, and if not, adds it in the Flush(). Now that the SmtpDataFilter ensures the data ends with CRLF, we can change EndData from |
That works as well. Taking a look at the changes, I don't see |
On further review, I notice that since it also runs through Unix2DosFilter filter before hitting |
Correct. No need to track CRLF vs LF in the SmtpDataFilter because all content that reaches the SmtpDataFilter has already been converted to CRLF from LF. |
SmtpClient currently adds an extra CRLF when sending messages to email servers.
Because of this bug, the delivered message contains an extra CRLF compared to the actual message when you do MimeMessage.WriteToAsync.
As per the RFC:
The EndData parameter in SmtpClient.cs (https://github.com/jstedfast/MailKit/blob/master/MailKit/Net/Smtp/SmtpClient.cs#L77) is currently:
It should be:
The first CLRF should be from the end of the message itself.
The text was updated successfully, but these errors were encountered: