/// <summary> /// Gets the best encoding given the specified constraints. /// </summary> /// <remarks> /// Gets the best encoding given the specified constraints. /// </remarks> /// <returns>The best encoding.</returns> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable line length (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public ContentEncoding GetBestEncoding (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < 72 || maxLineLength > 998) throw new ArgumentOutOfRangeException ("maxLineLength"); switch (constraint) { case EncodingConstraint.SevenBit: if (count0 > 0) return ContentEncoding.Base64; if (count8 > 0) { if (count8 >= (int) (total * (17.0 / 100.0))) return ContentEncoding.Base64; return ContentEncoding.QuotedPrintable; } if (maxline > maxLineLength) return ContentEncoding.QuotedPrintable; break; case EncodingConstraint.EightBit: if (count0 > 0) return ContentEncoding.Base64; if (maxline > maxLineLength) return ContentEncoding.QuotedPrintable; if (count8 > 0) return ContentEncoding.EightBit; break; case EncodingConstraint.None: if (count0 > 0) return ContentEncoding.Binary; if (maxline > maxLineLength) return ContentEncoding.QuotedPrintable; if (count8 > 0) return ContentEncoding.EightBit; break; default: throw new ArgumentOutOfRangeException ("constraint"); } if (hasMarker) return ContentEncoding.QuotedPrintable; return ContentEncoding.SevenBit; }
/// <summary> /// Gets the best encoding given the specified constraints. /// </summary> /// <remarks> /// Gets the best encoding given the specified constraints. /// </remarks> /// <returns>The best encoding.</returns> /// <param name="constraint">The encoding constraint.</param> public ContentEncoding GetBestEncoding (EncodingConstraint constraint) { switch (constraint) { case EncodingConstraint.SevenBit: if (count0 > 0) return ContentEncoding.Base64; if (count8 > 0) { if (count8 >= (int) (total * (17.0 / 100.0))) return ContentEncoding.Base64; return ContentEncoding.QuotedPrintable; } if (maxline > 998) return ContentEncoding.QuotedPrintable; break; case EncodingConstraint.EightBit: if (count0 > 0) return ContentEncoding.Base64; if (maxline > 998) return ContentEncoding.QuotedPrintable; if (count8 > 0) return ContentEncoding.EightBit; break; case EncodingConstraint.None: if (count0 > 0) return ContentEncoding.Binary; if (count8 > 0) return ContentEncoding.EightBit; break; } if (hasMarker) return ContentEncoding.QuotedPrintable; return ContentEncoding.SevenBit; }
/// <summary> /// Prepare the message for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the message for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public virtual void Prepare (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < 72 || maxLineLength > 998) throw new ArgumentOutOfRangeException ("maxLineLength"); if (Body != null) Body.Prepare (constraint, maxLineLength); }
/// <summary> /// Prepare the MIME entity for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the MIME entity for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum number of octets allowed per line (not counting the CRLF). Must be between <c>60</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>60</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public override void Prepare (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < FormatOptions.MinimumLineLength || maxLineLength > FormatOptions.MaximumLineLength) throw new ArgumentOutOfRangeException ("maxLineLength"); if (Message != null) Message.Prepare (constraint, maxLineLength); }
/// <summary> /// Prepare the MIME entity for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the MIME entity for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum number of octets allowed per line (not counting the CRLF). Must be between <c>60</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>60</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public override void Prepare (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < FormatOptions.MinimumLineLength || maxLineLength > FormatOptions.MaximumLineLength) throw new ArgumentOutOfRangeException ("maxLineLength"); switch (ContentTransferEncoding) { case ContentEncoding.QuotedPrintable: case ContentEncoding.UUEncode: case ContentEncoding.Base64: // these are all safe no matter what the constraints are return; case ContentEncoding.Binary: if (constraint == EncodingConstraint.None) { // no need to re-encode anything return; } break; } var best = GetBestEncoding (constraint, maxLineLength); if (ContentTransferEncoding == ContentEncoding.Default && best == ContentEncoding.SevenBit) return; ContentTransferEncoding = best; }
/// <summary> /// Calculates the most efficient content encoding given the specified constraint. /// </summary> /// <remarks> /// If no <see cref="ContentObject"/> is set, <see cref="ContentEncoding.SevenBit"/> will be returned. /// </remarks> /// <returns>The most efficient content encoding.</returns> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param> /// <param name="cancellationToken">A cancellation token.</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public ContentEncoding GetBestEncoding (EncodingConstraint constraint, int maxLineLength, CancellationToken cancellationToken = default (CancellationToken)) { if (ContentObject == null) return ContentEncoding.SevenBit; using (var measure = new MeasuringStream ()) { using (var filtered = new FilteredStream (measure)) { var filter = new BestEncodingFilter (); filtered.Add (filter); ContentObject.DecodeTo (filtered, cancellationToken); filtered.Flush (); return filter.GetBestEncoding (constraint, maxLineLength); } } }
/// <summary> /// Calculates the most efficient content encoding given the specified constraint. /// </summary> /// <remarks> /// If no <see cref="ContentObject"/> is set, <see cref="ContentEncoding.SevenBit"/> will be returned. /// </remarks> /// <returns>The most efficient content encoding.</returns> /// <param name="constraint">The encoding constraint.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="constraint"/> is not a valid value. /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public ContentEncoding GetBestEncoding (EncodingConstraint constraint, CancellationToken cancellationToken = default (CancellationToken)) { return GetBestEncoding (constraint, 78, cancellationToken); }
/// <summary> /// Prepare the MIME entity for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the MIME entity for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum number of octets allowed per line (not counting the CRLF). Must be between <c>60</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>60</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public override void Prepare (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < FormatOptions.MinimumLineLength || maxLineLength > FormatOptions.MaximumLineLength) throw new ArgumentOutOfRangeException ("maxLineLength"); // Note: we do not iterate over our children because they are already signed // and changing them would break the signature. They should already be // properly prepared, anyway. }
/// <summary> /// Gets the best encoding given the specified constraints. /// </summary> /// <remarks> /// Gets the best encoding given the specified constraints. /// </remarks> /// <returns>The best encoding.</returns> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable line length (not counting the CRLF). Must be between <c>60</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>60</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public ContentEncoding GetBestEncoding(EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < FormatOptions.MinimumLineLength || maxLineLength > FormatOptions.MaximumLineLength) { throw new ArgumentOutOfRangeException("maxLineLength"); } switch (constraint) { case EncodingConstraint.SevenBit: if (count0 > 0) { return(ContentEncoding.Base64); } if (count8 > 0) { if (count8 >= (int)(total * (17.0 / 100.0))) { return(ContentEncoding.Base64); } return(ContentEncoding.QuotedPrintable); } if (maxline > maxLineLength) { return(ContentEncoding.QuotedPrintable); } break; case EncodingConstraint.EightBit: if (count0 > 0) { return(ContentEncoding.Base64); } if (maxline > maxLineLength) { return(ContentEncoding.QuotedPrintable); } if (count8 > 0) { return(ContentEncoding.EightBit); } break; case EncodingConstraint.None: if (count0 > 0) { return(ContentEncoding.Binary); } if (maxline > maxLineLength) { return(ContentEncoding.QuotedPrintable); } if (count8 > 0) { return(ContentEncoding.EightBit); } break; default: throw new ArgumentOutOfRangeException("constraint"); } if (hasMarker) { return(ContentEncoding.QuotedPrintable); } return(ContentEncoding.SevenBit); }
/// <summary> /// Calculates the most efficient content encoding given the specified constraint. /// </summary> /// <remarks> /// If no <see cref="Content"/> is set, <see cref="ContentEncoding.SevenBit"/> will be returned. /// </remarks> /// <returns>The most efficient content encoding.</returns> /// <param name="constraint">The encoding constraint.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="constraint"/> is not a valid value. /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public ContentEncoding GetBestEncoding(EncodingConstraint constraint, CancellationToken cancellationToken = default(CancellationToken)) { return(GetBestEncoding(constraint, 78, cancellationToken)); }
/// <summary> /// Prepare the MIME entity for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the MIME entity for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public abstract void Prepare (EncodingConstraint constraint, int maxLineLength = 78);
/// <summary> /// Prepare the MIME entity for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the MIME entity for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public override void Prepare (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < 72 || maxLineLength > 998) throw new ArgumentOutOfRangeException ("maxLineLength"); if (Message != null) Message.Prepare (constraint, maxLineLength); }
/// <summary> /// Prepare the message for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the message for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>60</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>60</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public virtual void Prepare (EncodingConstraint constraint, int maxLineLength = 78) { if (maxLineLength < FormatOptions.MinimumLineLength || maxLineLength > FormatOptions.MaximumLineLength) throw new ArgumentOutOfRangeException ("maxLineLength"); if (Body != null) Body.Prepare (constraint, maxLineLength); }
/// <summary> /// Prepare the MIME entity for transport using the specified encoding constraints. /// </summary> /// <remarks> /// Prepares the MIME entity for transport using the specified encoding constraints. /// </remarks> /// <param name="constraint">The encoding constraint.</param> /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para> /// <para>-or-</para> /// <para><paramref name="constraint"/> is not a valid value.</para> /// </exception> public abstract void Prepare(EncodingConstraint constraint, int maxLineLength = 78);
/// <summary> /// Gets the best encoding given the specified constraints. /// </summary> /// <remarks> /// Gets the best encoding given the specified constraints. /// </remarks> /// <returns>The best encoding.</returns> /// <param name="constraint">The encoding constraint.</param> public ContentEncoding GetBestEncoding(EncodingConstraint constraint) { switch (constraint) { case EncodingConstraint.SevenBit: if (count0 > 0) { return(ContentEncoding.Base64); } if (count8 > 0) { if (count8 >= (int)(total * (17.0 / 100.0))) { return(ContentEncoding.Base64); } return(ContentEncoding.QuotedPrintable); } if (maxline > 998) { return(ContentEncoding.QuotedPrintable); } break; case EncodingConstraint.EightBit: if (count0 > 0) { return(ContentEncoding.Base64); } if (maxline > 998) { return(ContentEncoding.QuotedPrintable); } if (count8 > 0) { return(ContentEncoding.EightBit); } break; case EncodingConstraint.None: if (count0 > 0) { return(ContentEncoding.Binary); } if (count8 > 0) { return(ContentEncoding.EightBit); } break; } if (hasMarker) { return(ContentEncoding.QuotedPrintable); } return(ContentEncoding.SevenBit); }
/// <summary> /// Calculates the most efficient content encoding given the specified constraint. /// </summary> /// <remarks> /// If no <see cref="ContentObject"/> is set, <see cref="ContentEncoding.SevenBit"/> will be returned. /// </remarks> /// <returns>The most efficient content encoding.</returns> /// <param name="constraint">The encoding constraint.</param> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public ContentEncoding GetBestEncoding(EncodingConstraint constraint) { return GetBestEncoding (constraint, CancellationToken.None); }