Exemplo n.º 1
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>
		private static Bodypart BodypartFromMIME(MIMEPart mimePart) {
			NameValueCollection contentType = ParseMIMEField(
				mimePart.header["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.header["Content-Transfer-Encoding"]);
			p.Id = mimePart.header["Content-Id"];
			foreach (string k in contentType.AllKeys)
				p.Parameters.Add(k, contentType[k]);
			p.Size = mimePart.body.Length;
			if (mimePart.header["Content-Disposition"] != null) {
				NameValueCollection disposition = ParseMIMEField(
					mimePart.header["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;
		}
Exemplo n.º 2
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>
		private static Attachment CreateAttachment(Bodypart part, byte[] bytes) {
			MemoryStream stream = new MemoryStream(bytes);
			string name = part.Disposition.Filename ?? 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();
			}
			return attachment;
		}
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>
		private static 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>
		/// 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>
		private static void AddBodypart(this MailMessage message, Bodypart part, string content) {
			Encoding encoding = part.Parameters.ContainsKey("Charset") ?
				Util.GetEncoding(part.Parameters["Charset"]) : Encoding.ASCII;
			// decode content if it was 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 MailMessage's Body fields haven't been initialized yet, put it there.
			// Some weird (i.e. spam) mails like to omit content-types so don't check for
			// that here and just assume it's text.
			if (message.Body == string.Empty) {
				message.Body = encoding.GetString(bytes);
				message.BodyEncoding = encoding;
				message.IsBodyHtml = part.Subtype.ToLower() == "html";
				return;
			}

			if (part.Disposition.Type == ContentDispositionType.Attachment)
				message.Attachments.Add(CreateAttachment(part, bytes));
			else
				message.AlternateViews.Add(CreateAlternateView(part, bytes));
		}