A multipart/alternative MIME entity.
A multipart/alternative MIME entity contains, as one might expect, is used to offer a list of alternative formats for the main body of the message (usually they will be "text/plain" and "text/html"). These alternatives are in order of increasing faithfulness to the original document (in other words, the last entity will be in a format that, when rendered, will most closely match what the sending client's WYSISYG editor produced).
Inheritance: Multipart
コード例 #1
0
 protected override void VisitMultipartAlternative(MultipartAlternative alternative)
 {
     // walk the multipart/alternative children backwards from greatest level of faithfulness to the least faithful
     for (int i = alternative.Count - 1; i >= 0 && _body == null; i--)
     {
         alternative[i].Accept(this);
     }
 }
コード例 #2
0
ファイル: MimeVisitor.cs プロジェクト: ywscr/MimeKit
 /// <summary>
 /// Visit the multipart/alternative MIME entity.
 /// </summary>
 /// <remarks>
 /// Visits the multipart/alternative MIME entity.
 /// </remarks>
 /// <example>
 /// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
 /// </example>
 /// <param name="alternative">The multipart/alternative MIME entity.</param>
 protected internal virtual void VisitMultipartAlternative(MultipartAlternative alternative)
 {
     VisitMultipart(alternative);
 }
コード例 #3
0
ファイル: BodyBuilder.cs プロジェクト: surekqomi/MimeKit
        /// <summary>
        /// Constructs the message body based on the text-based bodies, the linked resources, and the attachments.
        /// </summary>
        /// <remarks>
        /// Combines the <see cref="Attachments"/>, <see cref="LinkedResources"/>, <see cref="TextBody"/>,
        /// and <see cref="HtmlBody"/> into the proper MIME structure suitable for display in many common
        /// mail clients.
        /// </remarks>
        /// <returns>The message body.</returns>
        public MimeEntity ToMessageBody()
        {
            MultipartAlternative alternative = null;
            MimeEntity           body        = null;

            if (!string.IsNullOrEmpty(TextBody))
            {
                var text = new TextPart("plain");
                text.Text = TextBody;

                if (!string.IsNullOrEmpty(HtmlBody))
                {
                    alternative = new MultipartAlternative();
                    alternative.Add(text);
                    body = alternative;
                }
                else
                {
                    body = text;
                }
            }

            if (!string.IsNullOrEmpty(HtmlBody))
            {
                var        text = new TextPart("html");
                MimeEntity html;

                text.ContentId = MimeUtils.GenerateMessageId();
                text.Text      = HtmlBody;

                if (LinkedResources.Count > 0)
                {
                    var related = new MultipartRelated {
                        Root = text
                    };

                    foreach (var resource in LinkedResources)
                    {
                        related.Add(resource);
                    }

                    html = related;
                }
                else
                {
                    html = text;
                }

                if (alternative != null)
                {
                    alternative.Add(html);
                }
                else
                {
                    body = html;
                }
            }

            if (Attachments.Count > 0)
            {
                var mixed = new Multipart("mixed");

                if (body != null)
                {
                    mixed.Add(body);
                }

                foreach (var attachment in Attachments)
                {
                    mixed.Add(attachment);
                }

                body = mixed;
            }

            return(body ?? new TextPart("plain")
            {
                Text = string.Empty
            });
        }
コード例 #4
0
ファイル: MimeVisitor.cs プロジェクト: cybercircuits/MimeKit
		/// <summary>
		/// Visit the multipart/alternative MIME entity.
		/// </summary>
		/// <remarks>
		/// Visits the multipart/alternative MIME entity.
		/// </remarks>
		/// <example>
		/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
		/// </example>
		/// <param name="alternative">The multipart/alternative MIME entity.</param>
		protected internal virtual void VisitMultipartAlternative (MultipartAlternative alternative)
		{
			VisitMultipart (alternative);
		}
コード例 #5
0
ファイル: BodyBuilder.cs プロジェクト: ruffin--/MimeKit
		/// <summary>
		/// Constructs the message body based on the text-based bodies, the linked resources, and the attachments.
		/// </summary>
		/// <remarks>
		/// Combines the <see cref="Attachments"/>, <see cref="LinkedResources"/>, <see cref="TextBody"/>,
		/// and <see cref="HtmlBody"/> into the proper MIME structure suitable for display in many common
		/// mail clients.
		/// </remarks>
		/// <returns>The message body.</returns>
		public MimeEntity ToMessageBody ()
		{
			MultipartAlternative alternative = null;
			MimeEntity body = null;

			if (!string.IsNullOrEmpty (TextBody)) {
				var text = new TextPart ("plain");
				text.Text = TextBody;

				if (!string.IsNullOrEmpty (HtmlBody)) {
					alternative = new MultipartAlternative ();
					alternative.Add (text);
					body = alternative;
				} else {
					body = text;
				}
			}

			if (!string.IsNullOrEmpty (HtmlBody)) {
				var text = new TextPart ("html");
				MimeEntity html;

				text.ContentId = MimeUtils.GenerateMessageId ();
				text.Text = HtmlBody;

				if (LinkedResources.Count > 0) {
					var related = new MultipartRelated {
						Root = text
					};

					foreach (var resource in LinkedResources)
						related.Add (resource);

					html = related;
				} else {
					html = text;
				}

				if (alternative != null)
					alternative.Add (html);
				else
					body = html;
			}

			if (Attachments.Count > 0) {
				var mixed = new Multipart ("mixed");

				if (body != null)
					mixed.Add (body);

				foreach (var attachment in Attachments)
					mixed.Add (attachment);

				body = mixed;
			}

			return body ?? new TextPart ("plain") { Text = string.Empty };
		}
コード例 #6
0
		protected override void VisitMultipartAlternative (MultipartAlternative alternative)
		{
			var multipart = new MultipartAlternative ();

			Push (multipart);

			for (int i = 0; i < alternative.Count; i++)
				alternative[i].Accept (this);

			Pop ();
		}
コード例 #7
0
ファイル: MimeMessage.cs プロジェクト: nachocove/MimeKit
		/// <summary>
		/// Creates a new <see cref="MimeMessage"/> from a <see cref="System.Net.Mail.MailMessage"/>.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MimeMessage"/> from a <see cref="System.Net.Mail.MailMessage"/>.
		/// </remarks>
		/// <returns>The equivalent <see cref="MimeMessage"/>.</returns>
		/// <param name="message">The message.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="message"/> is <c>null</c>.
		/// </exception>
		public static MimeMessage CreateFromMailMessage (MailMessage message)
		{
			if (message == null)
				throw new ArgumentNullException ("message");

			var headers = new List<Header> ();
			foreach (var field in message.Headers.AllKeys) {
				foreach (var value in message.Headers.GetValues (field))
					headers.Add (new Header (field, value));
			}

			var msg = new MimeMessage (ParserOptions.Default, headers);
			MimeEntity body = null;

			// Note: If the user has already sent their MailMessage via System.Net.Mail.SmtpClient,
			// then the following MailMessage properties will have been merged into the Headers, so
			// check to make sure our MimeMessage properties are empty before adding them.
			if (msg.Sender == null && message.Sender != null)
				msg.Sender = (MailboxAddress) message.Sender;
			if (msg.From.Count == 0 && message.From != null)
				msg.From.Add ((MailboxAddress) message.From);
#if NET_3_5
			if (msg.ReplyTo.Count == 0 && message.ReplyTo != null)
				msg.ReplyTo.Add ((MailboxAddress) message.ReplyTo);
#else
			if (msg.ReplyTo.Count == 0 && message.ReplyToList.Count > 0)
				msg.ReplyTo.AddRange ((InternetAddressList) message.ReplyToList);
#endif
			if (msg.To.Count == 0 && message.To.Count > 0)
				msg.To.AddRange ((InternetAddressList) message.To);
			if (msg.Cc.Count == 0 && message.CC.Count > 0)
				msg.Cc.AddRange ((InternetAddressList) message.CC);
			if (msg.Bcc.Count == 0 && message.Bcc.Count > 0)
				msg.Bcc.AddRange ((InternetAddressList) message.Bcc);
			if (string.IsNullOrEmpty (msg.Subject))
				msg.Subject = message.Subject ?? string.Empty;

			switch (message.Priority) {
			case MailPriority.Normal:
				msg.Headers.RemoveAll (HeaderId.XMSMailPriority);
				msg.Headers.RemoveAll (HeaderId.Importance);
				msg.Headers.RemoveAll (HeaderId.XPriority);
				msg.Headers.RemoveAll (HeaderId.Priority);
				break;
			case MailPriority.High:
				msg.Headers.Replace (HeaderId.Priority, "urgent");
				msg.Headers.Replace (HeaderId.Importance, "high");
				msg.Headers.Replace (HeaderId.XPriority, "2 (High)");
				break;
			case MailPriority.Low:
				msg.Headers.Replace (HeaderId.Priority, "non-urgent");
				msg.Headers.Replace (HeaderId.Importance, "low");
				msg.Headers.Replace (HeaderId.XPriority, "4 (Low)");
				break;
			}

			if (!string.IsNullOrEmpty (message.Body)) {
				var text = new TextPart (message.IsBodyHtml ? "html" : "plain");
				text.SetText (message.BodyEncoding ?? Encoding.UTF8, message.Body);
				body = text;
			}

			if (message.AlternateViews.Count > 0) {
				var alternative = new MultipartAlternative ();

				if (body != null)
					alternative.Add (body);

				foreach (var view in message.AlternateViews) {
					var part = GetMimePart (view);

					if (view.BaseUri != null)
						part.ContentLocation = view.BaseUri;

					if (view.LinkedResources.Count > 0) {
						var type = part.ContentType.MediaType + "/" + part.ContentType.MediaSubtype;
						var related = new MultipartRelated ();

						related.ContentType.Parameters.Add ("type", type);

						if (view.BaseUri != null)
							related.ContentLocation = view.BaseUri;

						related.Add (part);

						foreach (var resource in view.LinkedResources) {
							part = GetMimePart (resource);

							if (resource.ContentLink != null)
								part.ContentLocation = resource.ContentLink;

							related.Add (part);
						}

						alternative.Add (related);
					} else {
						alternative.Add (part);
					}
				}

				body = alternative;
			}

			if (body == null)
				body = new TextPart (message.IsBodyHtml ? "html" : "plain");

			if (message.Attachments.Count > 0) {
				var mixed = new Multipart ("mixed");

				if (body != null)
					mixed.Add (body);

				foreach (var attachment in message.Attachments)
					mixed.Add (GetMimePart (attachment));

				body = mixed;
			}

			msg.Body = body;

			return msg;
		}