Skip to content

Commit

Permalink
Added FormatOptions.EnsureNewLine property
Browse files Browse the repository at this point in the history
Partial fix for jstedfast/MailKit#251
  • Loading branch information
jstedfast committed May 7, 2018
1 parent e481f5e commit c188903
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 28 additions & 0 deletions MimeKit/FormatOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class FormatOptions
ParameterEncodingMethod parameterEncodingMethod;
bool allowMixedHeaderCharsets;
NewLineFormat newLineFormat;
bool ensureNewLine;
bool international;

/// <summary>
Expand Down Expand Up @@ -118,6 +119,31 @@ public NewLineFormat NewLineFormat {
}
}

/// <summary>
/// Gets or sets whether the formatter should ensure that messages end with a new-line sequence.
/// </summary>
/// <remarks>
/// <para>By default, when writing a <see cref="MimeMessage"/> 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.</para>
/// <para>To override this behavior, you can set this property to <c>true</c> in order to ensure
/// that writing the message back to a stream will always end with a new-line sequence.</para>
/// </remarks>
/// <value><c>true</c> in order to ensure that the message will end with a new-line sequence; otherwise, <c>false</c>.</value>
/// <exception cref="System.InvalidOperationException">
/// <see cref="Default"/> cannot be changed.
/// </exception>
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) {
Expand Down Expand Up @@ -255,6 +281,7 @@ public FormatOptions ()
parameterEncodingMethod = ParameterEncodingMethod.Rfc2231;
//maxLineLength = DefaultMaxLineLength;
allowMixedHeaderCharsets = false;
ensureNewLine = false;
international = false;

if (Environment.NewLine.Length == 1)
Expand All @@ -275,6 +302,7 @@ public FormatOptions Clone ()
var options = new FormatOptions ();
//options.maxLineLength = maxLineLength;
options.newLineFormat = newLineFormat;
options.ensureNewLine = ensureNewLine;
options.HiddenHeaders = new HashSet<HeaderId> (HiddenHeaders);
options.allowMixedHeaderCharsets = allowMixedHeaderCharsets;
options.parameterEncodingMethod = parameterEncodingMethod;
Expand Down
6 changes: 3 additions & 3 deletions MimeKit/MimeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c188903

Please sign in to comment.