diff --git a/MimeKit/FormatOptions.cs b/MimeKit/FormatOptions.cs index 56d99dd74b..cf590afb53 100644 --- a/MimeKit/FormatOptions.cs +++ b/MimeKit/FormatOptions.cs @@ -73,6 +73,7 @@ public class FormatOptions ParameterEncodingMethod parameterEncodingMethod; bool allowMixedHeaderCharsets; NewLineFormat newLineFormat; + bool ensureNewLine; bool international; /// @@ -118,6 +119,31 @@ public NewLineFormat NewLineFormat { } } + /// + /// Gets or sets whether the formatter should ensure that messages end with a new-line sequence. + /// + /// + /// By default, when writing a to a stream, the serializer attempts to + /// maintain byte-for-byte compatibility with the original stream that the message was parsed from. + /// This means that if the ogirinal message stream did not end with a new-line sequence, then the + /// output of writing the message back to a stream will also not end with a new-line sequence. + /// To override this behavior, you can set this property to true in order to ensure + /// that writing the message back to a stream will always end with a new-line sequence. + /// + /// true in order to ensure that the message will end with a new-line sequence; otherwise, false. + /// + /// cannot be changed. + /// + public bool EnsureNewLine { + get { return ensureNewLine; } + set { + if (this == Default) + throw new InvalidOperationException ("The default formatting options cannot be changed."); + + ensureNewLine = value; + } + } + internal IMimeFilter CreateNewLineFilter (bool ensureNewLine = false) { switch (NewLineFormat) { @@ -255,6 +281,7 @@ public FormatOptions () parameterEncodingMethod = ParameterEncodingMethod.Rfc2231; //maxLineLength = DefaultMaxLineLength; allowMixedHeaderCharsets = false; + ensureNewLine = false; international = false; if (Environment.NewLine.Length == 1) @@ -275,6 +302,7 @@ public FormatOptions Clone () var options = new FormatOptions (); //options.maxLineLength = maxLineLength; options.newLineFormat = newLineFormat; + options.ensureNewLine = ensureNewLine; options.HiddenHeaders = new HashSet (HiddenHeaders); options.allowMixedHeaderCharsets = allowMixedHeaderCharsets; options.parameterEncodingMethod = parameterEncodingMethod; diff --git a/MimeKit/MimeMessage.cs b/MimeKit/MimeMessage.cs index 7922418d33..323934b7ba 100644 --- a/MimeKit/MimeMessage.cs +++ b/MimeKit/MimeMessage.cs @@ -1109,7 +1109,7 @@ public virtual void Prepare (EncodingConstraint constraint, int maxLineLength = if (!headersOnly) { try { - Body.EnsureNewLine = compliance == RfcComplianceMode.Strict; + Body.EnsureNewLine = compliance == RfcComplianceMode.Strict || options.EnsureNewLine; Body.WriteTo (options, stream, true, cancellationToken); } finally { Body.EnsureNewLine = false; @@ -1174,7 +1174,7 @@ public virtual void Prepare (EncodingConstraint constraint, int maxLineLength = if (!headersOnly) { try { - Body.EnsureNewLine = compliance == RfcComplianceMode.Strict; + Body.EnsureNewLine = compliance == RfcComplianceMode.Strict || options.EnsureNewLine; await Body.WriteToAsync (options, stream, true, cancellationToken).ConfigureAwait (false); } finally { Body.EnsureNewLine = false; @@ -1664,7 +1664,7 @@ byte[] DkimHashBody (FormatOptions options, DkimSignatureAlgorithm signatureAlgo if (Body != null) { try { - Body.EnsureNewLine = compliance == RfcComplianceMode.Strict; + Body.EnsureNewLine = compliance == RfcComplianceMode.Strict || options.EnsureNewLine; Body.WriteTo (options, filtered, true, CancellationToken.None); } finally { Body.EnsureNewLine = false;