Exemplo n.º 1
0
        /// <summary>
		/// Creates an instance of the Attachment class used by the MailMessage class to store mail
		/// message attachments.
		/// </summary>
		/// <param name="part">The MIME body part to create the attachment from.</param>
		/// <param name="bytes">An array of bytes composing the content of the attachment.</param>
		/// <returns>An initialized instance of the Attachment class.</returns>
		Attachment CreateAttachment(Bodypart part, byte[] bytes)
        {
            MemoryStream stream = new MemoryStream(bytes);
            string name = part.Disposition.Filename;
            // Many MUAs put the file name in the name parameter of the content-type header instead of
            // the filename parameter of the content-disposition header.
            if (String.IsNullOrEmpty(name) && part.Parameters.ContainsKey("name"))
                name = part.Parameters["name"];
            if (String.IsNullOrEmpty(name))
                name = Path.GetRandomFileName();
            Attachment attachment = new Attachment(stream, name);
            try
            {
                attachment.ContentId = ParseMessageId(part.Id);
            }
            catch { }
            try
            {
                attachment.ContentType = new System.Net.Mime.ContentType(
                    part.Type.ToString().ToLower() + "/" + part.Subtype.ToLower());
            }
            catch
            {
                attachment.ContentType = new System.Net.Mime.ContentType();
            }
            // Workaround: filename from Attachment constructor is ignored with Mono.
            attachment.Name = name;
            attachment.ContentDisposition.FileName = name;
            return attachment;
        }
Exemplo n.º 2
0
        /// <summary>
		/// Adds a body part to an existing MailMessage instance.
		/// </summary>
		/// <param name="message">Extension method for the MailMessage class.</param>
		/// <param name="part">The body part to add to the MailMessage instance.</param>
		/// <param name="content">The content of the body part.</param>
		internal void AddBodypart(MailMessage message, Bodypart part, string content)
        {
            Encoding encoding = part.Parameters.ContainsKey("Charset") ?
                Util.GetEncoding(part.Parameters["Charset"]) : Encoding.ASCII;
            // Decode the content if it is encoded.
            byte[] bytes;
            try
            {
                switch (part.Encoding)
                {
                    case ContentTransferEncoding.QuotedPrintable:
                        bytes = encoding.GetBytes(Util.QPDecode(content, encoding));
                        break;
                    case ContentTransferEncoding.Base64:
                        bytes = Util.Base64Decode(content);
                        break;
                    default:
                        bytes = Encoding.ASCII.GetBytes(content);
                        break;
                }
            }
            catch
            {
                // If it's not a valid Base64 or quoted-printable encoded string just leave the data as is.
                bytes = Encoding.ASCII.GetBytes(content);
            }

            // If the part has a name it most likely is an attachment and it should go into the
            // Attachments collection.
            bool hasName = part.Parameters.ContainsKey("name");

            // If the MailMessage's Body fields haven't been initialized yet, put it there. Some weird
            // (i.e. spam) mails like to omit content-types so we don't check for that here and just
            // assume it's text.
            if (String.IsNullOrEmpty(message.Body) &&
                part.Disposition.Type != ContentDispositionType.Attachment)
            {
                message.Body = encoding.GetString(bytes);
                message.BodyEncoding = encoding;
                message.IsBodyHtml = part.Subtype.ToLower() == "html";
                return;
            }

            // Check for alternative view.
            string ContentType = ParseMIMEField(message.Headers["Content-Type"])["value"];
            bool preferAlternative = string.Compare(ContentType, "multipart/alternative", true) == 0;

            // Many attachments are missing the disposition-type. If it's not defined as alternative
            // and it has a name attribute, assume it is Attachment rather than an AlternateView.
            if (part.Disposition.Type == ContentDispositionType.Attachment ||
                (part.Disposition.Type == ContentDispositionType.Unknown &&
                preferAlternative == false && hasName))
                message.Attachments.Add(CreateAttachment(part, bytes));
            else
                message.AlternateViews.Add(CreateAlternateView(part, bytes));
        }
Exemplo n.º 3
0
        /// <summary>
		/// Creates an instance of the AlternateView class used by the MailMessage class to store
		/// alternate views of the mail message's content.
		/// </summary>
		/// <param name="part">The MIME body part to create the alternate view from.</param>
		/// <param name="bytes">An array of bytes composing the content of the alternate view.</param>
		/// <returns>An initialized instance of the AlternateView class.</returns>
		AlternateView CreateAlternateView(Bodypart part, byte[] bytes)
        {
            MemoryStream stream = new MemoryStream(bytes);
            System.Net.Mime.ContentType contentType;
            try
            {
                contentType = new System.Net.Mime.ContentType(
                    part.Type.ToString().ToLower() + "/" + part.Subtype.ToLower());
            }
            catch
            {
                contentType = new System.Net.Mime.ContentType();
            }
            AlternateView view = new AlternateView(stream, contentType);
            try
            {
                view.ContentId = ParseMessageId(part.Id);
            }
            catch { }
            return view;
        }
Exemplo n.º 4
0
        /// <summary>
		/// Glue method to create a bodypart from a MIMEPart instance.
		/// </summary>
		/// <param name="mimePart">The MIMEPart instance to create the bodypart instance from.</param>
		/// <returns>An initialized instance of the Bodypart class.</returns>
		Bodypart BodypartFromMIME(MIMEPart mimePart)
        {
            NameValueCollection contentType = ParseMIMEField(
                mimePart.Headers["Content-Type"]);
            Bodypart p = new Bodypart(null);
            Match m = Regex.Match(contentType["value"], "(.+)/(.+)");
            if (m.Success)
            {
                p.Type = ContentTypeMap.fromString(m.Groups[1].Value);
                p.Subtype = m.Groups[2].Value;
            }
            p.Encoding = ContentTransferEncodingMap.fromString(
                mimePart.Headers["Content-Transfer-Encoding"]);
            p.Id = mimePart.Headers["Content-Id"];
            foreach (string k in contentType.AllKeys)
                p.Parameters.Add(k, contentType[k]);
            p.Size = mimePart.MessageText.Length;
            if (mimePart.Headers["Content-Disposition"] != null)
            {
                NameValueCollection disposition = ParseMIMEField(
                    mimePart.Headers["Content-Disposition"]);
                p.Disposition.Type = ContentDispositionTypeMap.fromString(
                    disposition["value"]);
                p.Disposition.Filename = disposition["Filename"];
                foreach (string k in disposition.AllKeys)
                    p.Disposition.Attributes.Add(k, disposition[k]);
            }
            return p;
        }