void OnFieldsChanged(object sender, HeaderListChangedEventArgs e)
        {
            var stream  = new MemoryBlockStream();
            var options = FormatOptions.Default;

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

            Content = new MimeContent(stream);
        }
Beispiel #2
0
		/// <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="TextPart.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 (nameof (encoding));

			if (text == null)
				throw new ArgumentNullException (nameof (text));

			ContentType.Parameters["charset"] = CharsetUtils.GetMimeCharset (encoding);
			var content = new MemoryStream (encoding.GetBytes (text));
			Content = new MimeContent (content);
		}
Beispiel #3
0
        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;

            Content = new MimeContent(stream);
        }
Beispiel #4
0
        /// <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.IMimeContent"/> 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(nameof(args));
            }

            IMimeContent content = null;

            foreach (object obj in args)
            {
                if (obj == null || TryInit(obj))
                {
                    continue;
                }

                var co = obj as IMimeContent;
                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 MimeContent(stream);
                    continue;
                }

                throw new ArgumentException("Unknown initialization parameter: " + obj.GetType());
            }

            if (content != null)
            {
                Content = content;
            }
        }
Beispiel #5
0
        /// <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="TextPart.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(nameof(encoding));
            }

            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            var content = new MemoryStream(encoding.GetBytes(text));

            ContentType.CharsetEncoding = encoding;
            Content = new MimeContent(content);
        }
        private static void PrepareAttachments(MimeMessage emailMessage, List <Stream> streams,
                                               List <EmailAttachment> attachments, BodyBuilder bodyBuilder)
        {
            var multipart = new Multipart("mixed")
            {
                bodyBuilder.ToMessageBody()
            };

            try
            {
                foreach (var attachment in attachments)
                {
                    Stream stream        = new MemoryStream(attachment.Content);
                    var    contentObject = new MimeKit.MimeContent(stream);
                    streams.Add(stream);
                    var contentDisposition = new ContentDisposition(ContentDisposition.Attachment);
                    var fileType           = stream.GetFileTypeFromStream();
                    attachment.AttachmentName = attachment.AttachmentName.EndsWith(fileType.Extension)
                        ? attachment.AttachmentName
                        : $"{attachment.AttachmentName}.{fileType.Extension}";
                    var attachmentPart = new MimePart(MimeTypes.GetMimeType(attachment.AttachmentName))
                    {
                        Content                 = contentObject,
                        ContentDisposition      = contentDisposition,
                        ContentTransferEncoding = ContentEncoding.Base64,
                        FileName                = attachment.AttachmentName
                    };
                    multipart.Add(attachmentPart);
                }
                emailMessage.Body = multipart;
            }
            catch
            {
                // ignored
            }
        }