Beispiel #1
0
        /// <summary>
        /// Serializes the <see cref="MimeKit.ContentDisposition"/> to a string,
        /// optionally encoding the parameters.
        /// </summary>
        /// <remarks>
        /// Creates a string-representation of the <see cref="ContentDisposition"/>,
        /// optionally encoding the parameters as they would be encoded for trabsport.
        /// </remarks>
        /// <returns>The serialized string.</returns>
        /// <param name="charset">The charset to be used when encoding the parameter values.</param>
        /// <param name="encode">If set to <c>true</c>, the parameter values will be encoded.</param>
        public string ToString(Encoding charset, bool encode)
        {
            if (charset == null)
            {
                throw new ArgumentNullException("charset");
            }

            var value = new StringBuilder("Content-Disposition: ");

            value.Append(disposition);

            if (encode)
            {
                var options    = FormatOptions.GetDefault();
                int lineLength = value.Length;

                Parameters.Encode(options, value, ref lineLength, charset);
            }
            else
            {
                value.Append(Parameters.ToString());
            }

            return(value.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Returns a string representation of the <see cref="GroupAddress"/>,
        /// optionally encoding it for transport.
        /// </summary>
        /// <remarks>
        /// Returns a string containing the formatted group of addresses. If the <paramref name="encode"/>
        /// parameter is <c>true</c>, then the name of the group and all member addresses will be encoded
        /// according to the rules defined in rfc2047, otherwise the names will not be encoded at all and
        /// will therefor only be suitable for display purposes.
        /// </remarks>
        /// <returns>A string representing the <see cref="GroupAddress"/>.</returns>
        /// <param name="encode">If set to <c>true</c>, the <see cref="GroupAddress"/> will be encoded.</param>
        public override string ToString(bool encode)
        {
            var builder = new StringBuilder();

            if (encode)
            {
                var options    = FormatOptions.GetDefault();
                int lineLength = 0;

                Encode(options, builder, ref lineLength);
            }
            else
            {
                builder.Append(Name);
                builder.Append(':');
                builder.Append(' ');

                for (int i = 0; i < Members.Count; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }

                    builder.Append(Members[i]);
                }

                builder.Append(';');
            }

            return(builder.ToString());
        }
Beispiel #3
0
        void SerializeContentDisposition()
        {
            var text = disposition.Encode(FormatOptions.GetDefault(), Encoding.UTF8);
            var raw  = Encoding.UTF8.GetBytes(text);

            SetHeader("Content-Disposition", raw);
        }
Beispiel #4
0
        void SerializeContentType()
        {
            var text = ContentType.Encode(FormatOptions.GetDefault(), Encoding.UTF8);
            var raw  = Encoding.UTF8.GetBytes(text);

            SetHeader("Content-Type", raw);
        }
Beispiel #5
0
        /// <summary>
        /// Load a <see cref="MimeEntity"/> from the specified content stream.
        /// </summary>
        /// <remarks>
        /// This method is mostly meant for use with APIs such as <see cref="System.Net.HttpWebResponse"/>
        /// where the headers are parsed separately from the content.
        /// </remarks>
        /// <returns>The parsed MIME entity.</returns>
        /// <param name="options">The parser options.</param>
        /// <param name="contentType">The Content-Type of the stream.</param>
        /// <param name="content">The content stream.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="contentType"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="content"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.FormatException">
        /// There was an error parsing the entity.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public static MimeEntity Load(ParserOptions options, ContentType contentType, Stream content, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (contentType == null)
            {
                throw new ArgumentNullException("contentType");
            }

            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            var format = FormatOptions.GetDefault();

            format.NewLineFormat = NewLineFormat.Dos;

            var encoded = contentType.Encode(format, Encoding.UTF8);
            var header  = string.Format("Content-Type:{0}\r\n", encoded);
            var chained = new ChainedStream();

            chained.Add(new MemoryStream(Encoding.UTF8.GetBytes(header), false));
            chained.Add(content);

            return(Load(options, chained, cancellationToken));
        }
        /// <summary>
        /// Returns a string representation of the <see cref="MailboxAddress"/>,
        /// optionally encoding it for transport.
        /// </summary>
        /// <remarks>
        /// Returns a string containing the formatted mailbox address. If the <paramref name="encode"/>
        /// parameter is <c>true</c>, then the mailbox name will be encoded according to the rules defined
        /// in rfc2047, otherwise the name will not be encoded at all and will therefor only be suitable
        /// for display purposes.
        /// </remarks>
        /// <returns>A string representing the <see cref="MailboxAddress"/>.</returns>
        /// <param name="encode">If set to <c>true</c>, the <see cref="MailboxAddress"/> will be encoded.</param>
        public override string ToString(bool encode)
        {
            if (encode)
            {
                var builder    = new StringBuilder();
                int lineLength = 0;

                Encode(FormatOptions.GetDefault(), builder, ref lineLength);

                return(builder.ToString());
            }

            string route = Route.ToString();

            if (!string.IsNullOrEmpty(route))
            {
                route += ":";
            }

            if (!string.IsNullOrEmpty(Name))
            {
                return(MimeUtils.Quote(Name) + " <" + route + Address + ">");
            }

            if (!string.IsNullOrEmpty(route))
            {
                return("<" + route + Address + ">");
            }

            return(Address);
        }
Beispiel #7
0
        /// <summary>
        /// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file.
        /// </summary>
        /// <remarks>
        /// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file using the default formatting options.
        /// </remarks>
        /// <param name="fileName">The file.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="fileName"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// <paramref name="fileName"/> is a zero-length string, contains only white space, or
        /// contains one or more invalid characters as defined by
        /// <see cref="System.IO.Path.InvalidPathChars"/>.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.DirectoryNotFoundException">
        /// <paramref name="fileName"/> is an invalid file path.
        /// </exception>
        /// <exception cref="System.IO.FileNotFoundException">
        /// The specified file path could not be found.
        /// </exception>
        /// <exception cref="System.UnauthorizedAccessException">
        /// The user does not have access to write to the specified file.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public void WriteTo(string fileName, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            using (var stream = File.OpenWrite(fileName))
                WriteTo(FormatOptions.GetDefault(), stream, cancellationToken);
        }
        void OnFieldsChanged(object sender, HeaderListChangedEventArgs e)
        {
            var options = FormatOptions.GetDefault();
            var stream  = new MemoryBlockStream();

            fields.WriteTo(options, stream);
            stream.Write(options.NewLineBytes, 0, options.NewLineBytes.Length);
            stream.Position = 0;

            ContentObject = new ContentObject(stream);
        }
Beispiel #9
0
 /// <summary>
 /// Returns a string representation of the email addresses in the <see cref="InternetAddressList"/>.
 /// </summary>
 /// <remarks>
 /// If there are multiple addresses in the list, they will be separated by a comma.
 /// </remarks>
 /// <returns>A string representing the <see cref="InternetAddressList"/>.</returns>
 public override string ToString()
 {
     return(ToString(FormatOptions.GetDefault(), false));
 }
Beispiel #10
0
 /// <summary>
 /// Returns a string representation of the email addresses in the <see cref="InternetAddressList"/>,
 /// optionally encoding them for transport.
 /// </summary>
 /// <remarks>
 /// <para>If <paramref name="encode"/> is <c>true</c>, each address in the list will be encoded
 /// according to the rules defined in rfc2047.</para>
 /// <para>If there are multiple addresses in the list, they will be separated by a comma.</para>
 /// </remarks>
 /// <returns>A string representing the <see cref="InternetAddressList"/>.</returns>
 /// <param name="encode">If set to <c>true</c>, each <see cref="InternetAddress"/> in the list will be encoded.</param>
 public string ToString(bool encode)
 {
     return(ToString(FormatOptions.GetDefault(), encode));
 }
Beispiel #11
0
 /// <summary>
 /// Writes the <see cref="MimeKit.MimeEntity"/> to the specified output stream.
 /// </summary>
 /// <remarks>
 /// Writes the entity to the output stream.
 /// </remarks>
 /// <param name="stream">The output stream.</param>
 /// <param name="cancellationToken">A cancellation token.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="stream"/> is <c>null</c>.
 /// </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 void WriteTo(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
 {
     WriteTo(FormatOptions.GetDefault(), stream, cancellationToken);
 }