diff --git a/MimeKit/MessagePart.cs b/MimeKit/MessagePart.cs index 3a845f0d40..e20aec2f13 100644 --- a/MimeKit/MessagePart.cs +++ b/MimeKit/MessagePart.cs @@ -184,6 +184,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = /// /// The formatting options. /// The output stream. + /// true if only the content should be written; otherwise, false. /// A cancellation token. /// /// is null. @@ -196,9 +197,9 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = /// /// An I/O error occurred. /// - public override void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken)) + public override void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) { - base.WriteTo (options, stream, cancellationToken); + base.WriteTo (options, stream, contentOnly, cancellationToken); if (Message != null) Message.WriteTo (options, stream, cancellationToken); diff --git a/MimeKit/MimeEntity.cs b/MimeKit/MimeEntity.cs index a1588a5c7f..3bf1fe6c53 100644 --- a/MimeKit/MimeEntity.cs +++ b/MimeKit/MimeEntity.cs @@ -397,6 +397,7 @@ public virtual void Accept (MimeVisitor visitor) /// /// The formatting options. /// The output stream. + /// true if only the content should be written; otherwise, false. /// A cancellation token. /// /// is null. @@ -409,7 +410,7 @@ public virtual void Accept (MimeVisitor visitor) /// /// An I/O error occurred. /// - public virtual void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken)) + public virtual void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) { if (options == null) throw new ArgumentNullException ("options"); @@ -417,7 +418,57 @@ public virtual void WriteTo (FormatOptions options, Stream stream, CancellationT if (stream == null) throw new ArgumentNullException ("stream"); - Headers.WriteTo (options, stream, cancellationToken); + if (!contentOnly) + Headers.WriteTo (options, stream, cancellationToken); + } + + /// + /// Writes the to the specified output stream. + /// + /// + /// Writes the headers to the output stream, followed by a blank line. + /// Subclasses should override this method to write the content of the entity. + /// + /// The formatting options. + /// The output stream. + /// A cancellation token. + /// + /// is null. + /// -or- + /// is null. + /// + /// + /// The operation was canceled via the cancellation token. + /// + /// + /// An I/O error occurred. + /// + public void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken)) + { + WriteTo (options, stream, false, cancellationToken); + } + + /// + /// Writes the to the specified output stream. + /// + /// + /// Writes the entity to the output stream. + /// + /// The output stream. + /// true if only the content should be written; otherwise, false. + /// A cancellation token. + /// + /// is null. + /// + /// + /// The operation was canceled via the cancellation token. + /// + /// + /// An I/O error occurred. + /// + public void WriteTo (Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) + { + WriteTo (FormatOptions.Default, stream, contentOnly, cancellationToken); } /// @@ -439,10 +490,57 @@ public virtual void WriteTo (FormatOptions options, Stream stream, CancellationT /// public void WriteTo (Stream stream, CancellationToken cancellationToken = default (CancellationToken)) { - WriteTo (FormatOptions.Default, stream, cancellationToken); + WriteTo (FormatOptions.Default, stream, false, cancellationToken); } #if !PORTABLE && !COREFX + /// + /// Writes the to the specified file. + /// + /// + /// Writes the to the specified file using the provided formatting options. + /// + /// The formatting options. + /// The file. + /// true if only the content should be written; otherwise, false. + /// A cancellation token. + /// + /// is null. + /// -or- + /// is null. + /// + /// + /// is a zero-length string, contains only white space, or + /// contains one or more invalid characters as defined by + /// . + /// + /// + /// The operation was canceled via the cancellation token. + /// + /// + /// is an invalid file path. + /// + /// + /// The specified file path could not be found. + /// + /// + /// The user does not have access to write to the specified file. + /// + /// + /// An I/O error occurred. + /// + public void WriteTo (FormatOptions options, string fileName, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) + { + if (options == null) + throw new ArgumentNullException ("options"); + + if (fileName == null) + throw new ArgumentNullException ("fileName"); + + using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write)) + WriteTo (options, stream, contentOnly, cancellationToken); + } + /// /// Writes the to the specified file. /// @@ -486,7 +584,48 @@ public void WriteTo (FormatOptions options, string fileName, CancellationToken c throw new ArgumentNullException ("fileName"); using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write)) - WriteTo (options, stream, cancellationToken); + WriteTo (options, stream, false, cancellationToken); + } + + /// + /// Writes the to the specified file. + /// + /// + /// Writes the to the specified file using the default formatting options. + /// + /// The file. + /// true if only the content should be written; otherwise, false. + /// A cancellation token. + /// + /// is null. + /// + /// + /// is a zero-length string, contains only white space, or + /// contains one or more invalid characters as defined by + /// . + /// + /// + /// The operation was canceled via the cancellation token. + /// + /// + /// is an invalid file path. + /// + /// + /// The specified file path could not be found. + /// + /// + /// The user does not have access to write to the specified file. + /// + /// + /// An I/O error occurred. + /// + public void WriteTo (string fileName, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) + { + if (fileName == null) + throw new ArgumentNullException ("fileName"); + + using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write)) + WriteTo (FormatOptions.Default, stream, contentOnly, cancellationToken); } /// @@ -526,7 +665,7 @@ public void WriteTo (string fileName, CancellationToken cancellationToken = defa throw new ArgumentNullException ("fileName"); using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write)) - WriteTo (FormatOptions.Default, stream, cancellationToken); + WriteTo (FormatOptions.Default, stream, false, cancellationToken); } #endif diff --git a/MimeKit/MimePart.cs b/MimeKit/MimePart.cs index bcebe4ca1c..0835b88df9 100644 --- a/MimeKit/MimePart.cs +++ b/MimeKit/MimePart.cs @@ -531,6 +531,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = /// /// The formatting options. /// The output stream. + /// true if only the content should be written; otherwise, false. /// A cancellation token. /// /// is null. @@ -543,9 +544,9 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = /// /// An I/O error occurred. /// - public override void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken)) + public override void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) { - base.WriteTo (options, stream, cancellationToken); + base.WriteTo (options, stream, contentOnly, cancellationToken); if (ContentObject == null) return; diff --git a/MimeKit/Multipart.cs b/MimeKit/Multipart.cs index ef7a7bf2b6..3d6224a144 100644 --- a/MimeKit/Multipart.cs +++ b/MimeKit/Multipart.cs @@ -385,6 +385,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = /// /// The formatting options. /// The output stream. + /// true if only the content should be written; otherwise, false. /// A cancellation token. /// /// is null. @@ -397,12 +398,12 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = /// /// An I/O error occurred. /// - public override void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken)) + public override void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken)) { if (Boundary == null) Boundary = GenerateBoundary (); - base.WriteTo (options, stream, cancellationToken); + base.WriteTo (options, stream, contentOnly, cancellationToken); if (ContentType.IsMimeType ("multipart", "signed")) { // don't reformat the headers or content of any children of a multipart/signed @@ -424,7 +425,7 @@ public override void WriteTo (FormatOptions options, Stream stream, Cancellation for (int i = 0; i < children.Count; i++) { cancellable.Write (boundary, 0, boundary.Length - 2, cancellationToken); cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken); - children[i].WriteTo (options, stream, cancellationToken); + children[i].WriteTo (options, stream, false, cancellationToken); cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken); } @@ -437,7 +438,7 @@ public override void WriteTo (FormatOptions options, Stream stream, Cancellation cancellationToken.ThrowIfCancellationRequested (); stream.Write (boundary, 0, boundary.Length - 2); stream.Write (options.NewLineBytes, 0, options.NewLineBytes.Length); - children[i].WriteTo (options, stream, cancellationToken); + children[i].WriteTo (options, stream, false, cancellationToken); stream.Write (options.NewLineBytes, 0, options.NewLineBytes.Length); }