public SetText ( Portable.Text.Encoding encoding, string text ) : void | ||
encoding | Portable.Text.Encoding | The charset encoding. |
text | string | The text content. |
Résultat | void |
public void TestArgumentExceptions () { var text = new TextPart (TextFormat.Plain); Assert.Throws<ArgumentNullException> (() => new TextPart ("plain", (object[]) null)); Assert.Throws<ArgumentException> (() => new TextPart ("plain", Encoding.UTF8, "blah blah blah", Encoding.UTF8)); Assert.Throws<ArgumentException> (() => new TextPart ("plain", Encoding.UTF8, "blah blah blah", "blah blah")); Assert.Throws<ArgumentException> (() => new TextPart ("plain", 5)); Assert.Throws<ArgumentOutOfRangeException> (() => new TextPart ((TextFormat) 500)); Assert.Throws<ArgumentNullException> (() => text.Accept (null)); Assert.Throws<ArgumentNullException> (() => text.GetText ((string) null)); Assert.Throws<ArgumentNullException> (() => text.GetText ((Encoding) null)); Assert.Throws<ArgumentNullException> (() => text.SetText ((string) null, "text")); Assert.Throws<ArgumentNullException> (() => text.SetText ((Encoding) null, "text")); Assert.Throws<ArgumentNullException> (() => text.SetText ("iso-8859-1", null)); Assert.Throws<ArgumentNullException> (() => text.SetText (Encoding.UTF8, null)); }
/// <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.High: msg.Headers.Replace (HeaderId.Priority, "urgent"); msg.Headers.Replace (HeaderId.Importance, "high"); msg.Headers.Replace ("X-Priority", "1"); break; case MailPriority.Low: msg.Headers.Replace (HeaderId.Priority, "non-urgent"); msg.Headers.Replace (HeaderId.Importance, "low"); msg.Headers.Replace ("X-Priority", "5"); 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 Multipart ("alternative"); 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 Multipart ("related"); 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; }
/// <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; if (message.Sender != null) msg.Sender = (MailboxAddress) message.Sender; if (message.From != null) msg.From.Add ((MailboxAddress) message.From); msg.ReplyTo.AddRange ((InternetAddressList) message.ReplyToList); msg.To.AddRange ((InternetAddressList) message.To); msg.Cc.AddRange ((InternetAddressList) message.CC); msg.Bcc.AddRange ((InternetAddressList) message.Bcc); msg.Subject = message.Subject ?? string.Empty; switch (message.Priority) { case MailPriority.High: msg.Headers.Add (HeaderId.Priority, "urgent"); msg.Headers.Add (HeaderId.Importance, "high"); msg.Headers.Add ("X-Priority", "1"); break; case MailPriority.Low: msg.Headers.Add (HeaderId.Priority, "non-urgent"); msg.Headers.Add (HeaderId.Importance, "low"); msg.Headers.Add ("X-Priority", "5"); break; } if (message.Body != null) { 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 Multipart ("alternative"); 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 Multipart ("related"); 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 (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; }