/// <summary> /// Gets the decoded text content using the provided charset encoding to /// override the charset specified in the Content-Type parameters. /// </summary> /// <remarks> /// Uses the provided charset encoding to convert the raw text content /// into a unicode string, overriding any charset specified in the /// Content-Type header. /// </remarks> /// <returns>The decoded text.</returns> /// <param name="encoding">The charset encoding to use.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="encoding"/> is <c>null</c>. /// </exception> public string GetText(Encoding encoding) { if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (ContentObject == null) { return(string.Empty); } using (var memory = new MemoryStream()) { ContentObject.DecodeTo(memory); #if !PORTABLE && !COREFX var buffer = memory.GetBuffer(); #else var buffer = memory.ToArray(); #endif return(encoding.GetString(buffer, 0, (int)memory.Length)); } }
/// <summary> /// Sets the text content and the charset parameter in the Content-Type header. /// </summary> /// <remarks> /// This method is similar to setting the <see cref="Text"/> property, but allows /// specifying a charset encoding to use. Also updates the /// <see cref="ContentType.Charset"/> property. /// </remarks> /// <param name="encoding">The charset encoding.</param> /// <param name="text">The text content.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="encoding"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="text"/> is <c>null</c>.</para> /// </exception> public void SetText (Encoding encoding, string text) { if (encoding == null) throw new ArgumentNullException ("encoding"); if (text == null) throw new ArgumentNullException ("text"); ContentType.Parameters["charset"] = CharsetUtils.GetMimeCharset (encoding); var content = new MemoryStream (encoding.GetBytes (text)); ContentObject = new ContentObject (content); }
/// <summary> /// Writes the <see cref="MimeKit.MimePart"/> to the specified output stream. /// </summary> /// <remarks> /// Writes the MIME part to the output stream. /// </remarks> /// <param name="options">The formatting options.</param> /// <param name="stream">The output 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="stream"/> is <c>null</c>.</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 override void WriteTo(FormatOptions options, Stream stream, CancellationToken cancellationToken = default(CancellationToken)) { base.WriteTo(options, stream, cancellationToken); if (ContentObject == null) { return; } var cancellable = stream as ICancellableStream; if (ContentObject.Encoding != encoding) { if (encoding == ContentEncoding.UUEncode) { var begin = string.Format("begin 0644 {0}", FileName ?? "unknown"); var buffer = Encoding.UTF8.GetBytes(begin); if (cancellable != null) { cancellable.Write(buffer, 0, buffer.Length, cancellationToken); cancellable.Write(options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken); } else { cancellationToken.ThrowIfCancellationRequested(); stream.Write(buffer, 0, buffer.Length); stream.Write(options.NewLineBytes, 0, options.NewLineBytes.Length); } } // transcode the content into the desired Content-Transfer-Encoding using (var filtered = new FilteredStream(stream)) { filtered.Add(EncoderFilter.Create(encoding)); if (encoding != ContentEncoding.Binary) { filtered.Add(options.CreateNewLineFilter()); } ContentObject.DecodeTo(filtered, cancellationToken); filtered.Flush(cancellationToken); } if (encoding == ContentEncoding.UUEncode) { var buffer = Encoding.ASCII.GetBytes("end"); if (cancellable != null) { cancellable.Write(buffer, 0, buffer.Length, cancellationToken); cancellable.Write(options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken); } else { cancellationToken.ThrowIfCancellationRequested(); stream.Write(buffer, 0, buffer.Length); stream.Write(options.NewLineBytes, 0, options.NewLineBytes.Length); } } } else if (encoding != ContentEncoding.Binary) { using (var filtered = new FilteredStream(stream)) { filtered.Add(options.CreateNewLineFilter()); ContentObject.WriteTo(filtered, cancellationToken); filtered.Flush(cancellationToken); } } else { ContentObject.WriteTo(stream, cancellationToken); } }
/// <summary> /// Initializes a new instance of the <see cref="MimeKit.MimePart"/> class /// with the specified media type and subtype. /// </summary> /// <remarks> /// Creates a new <see cref="MimePart"/> with the specified media type and subtype. /// </remarks> /// <param name="mediaType">The media type.</param> /// <param name="mediaSubtype">The media subtype.</param> /// <param name="args">An array of initialization parameters: headers and part content.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="mediaType"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="mediaSubtype"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="args"/> is <c>null</c>.</para> /// </exception> /// <exception cref="System.ArgumentException"> /// <para><paramref name="args"/> contains more than one <see cref="MimeKit.IContentObject"/> or /// <see cref="System.IO.Stream"/>.</para> /// <para>-or-</para> /// <para><paramref name="args"/> contains one or more arguments of an unknown type.</para> /// </exception> public MimePart (string mediaType, string mediaSubtype, params object[] args) : this (mediaType, mediaSubtype) { if (args == null) throw new ArgumentNullException ("args"); IContentObject content = null; foreach (object obj in args) { if (obj == null || TryInit (obj)) continue; var co = obj as IContentObject; if (co != null) { if (content != null) throw new ArgumentException ("ContentObject should not be specified more than once."); content = co; continue; } var stream = obj as Stream; if (stream != null) { if (content != null) throw new ArgumentException ("Stream (used as content) should not be specified more than once."); // Use default as specified by ContentObject ctor when building a new MimePart. content = new ContentObject (stream); continue; } throw new ArgumentException ("Unknown initialization parameter: " + obj.GetType ()); } if (content != null) ContentObject = content; }
void OnGroupsChanged (object sender, EventArgs e) { var stream = new MemoryBlockStream (); var options = FormatOptions.Default; for (int i = 0; i < groups.Count; i++) groups[i].WriteTo (options, stream); stream.Position = 0; ContentObject = new ContentObject (stream); }
void OnFieldsChanged (object sender, HeaderListChangedEventArgs e) { var stream = new MemoryBlockStream (); var options = FormatOptions.Default; fields.WriteTo (options, stream); stream.Position = 0; ContentObject = new ContentObject (stream); }
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); }