question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SmtpClient.DataAsync Adds Extra CRLF

See original GitHub issue

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 mail data are terminated by a line containing only a period, that is, the character sequence “<CRLF>.<CRLF>”, where the first <CRLF> is actually the terminator of the previous line (see Section 4.5.2). This is the end of mail data indication. The first <CRLF> of this terminating sequence is also the <CRLF> that ends the final line of the data (message text) or, if there was no mail data, ends the DATA command itself (the “no mail data” case does not conform to this specification since it would require that neither the trace header fields required by this specification nor the message header section required by RFC 5322 [4] be transmitted). https://tools.ietf.org/html/rfc5321#section-4.1.1.4

The EndData parameter in SmtpClient.cs (https://github.com/jstedfast/MailKit/blob/master/MailKit/Net/Smtp/SmtpClient.cs#L77) is currently:

static readonly byte[] EndData = Encoding.ASCII.GetBytes ("\r\n.\r\n");

It should be:

static readonly byte[] EndData = Encoding.ASCII.GetBytes (".\r\n");

The first CLRF should be from the end of the message itself.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jstedfastcommented, Aug 21, 2019

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.

0reactions
jstedfastcommented, Aug 25, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SmtpClient.send(MailMessage) is adding an extra period ...
I'm using System.Net.Mail.SmtpClient. I build my MailMessage. IsBodyHtml is set to true. The Body is some html with some links.
Read more >
Why is sendmail adding an extra carriage return in headers?
A user in Japan is sending a message and when it gets processed by the server, SendMail is adding an extra carriage return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found