コード例 #1
1
        public void TestEncodingMailboxWithArabicName()
        {
            var mailbox = new MailboxAddress ("هل تتكلم اللغة الإنجليزية /العربية؟", "[email protected]");
            var list = new InternetAddressList ();
            list.Add (mailbox);

            var expected = "=?utf-8?b?2YfZhCDYqtiq2YPZhNmFINin2YTZhNi62Kk=?=\n =?utf-8?b?INin2YTYpdmG2KzZhNmK2LLZitipIC/Yp9mE2LnYsdio2YrYqdif?=\n\t<[email protected]>";
            var actual = list.ToString (UnixFormatOptions, true);

            Assert.AreEqual (expected, actual, "Encoding arabic mailbox did not match expected result: {0}", expected);
            Assert.IsTrue (InternetAddressList.TryParse (actual, out list), "Failed to parse arabic mailbox");
            Assert.AreEqual (mailbox.Name, list[0].Name);
        }
コード例 #2
0
		internal MimeMessage (ParserOptions options, IEnumerable<Header> headers)
		{
			addresses = new Dictionary<string, InternetAddressList> (icase);
			Headers = new HeaderList (options);

			// initialize our address lists
			foreach (var name in StandardAddressHeaders) {
				var list = new InternetAddressList ();
				list.Changed += InternetAddressListChanged;
				addresses.Add (name, list);
			}

			references = new MessageIdList ();
			references.Changed += ReferencesChanged;
			inreplyto = null;

			Headers.Changed += HeadersChanged;

			// add all of our message headers...
			foreach (var header in headers) {
				if (header.Field.StartsWith ("Content-", StringComparison.OrdinalIgnoreCase))
					continue;

				Headers.Add (header);
			}
		}
コード例 #3
0
		public void TestSimpleAddrSpec ()
		{
			var expected = new InternetAddressList ();
			var mailbox = new MailboxAddress ("", "");
			InternetAddressList result;
			string text;

			expected.Add (mailbox);

			text = "[email protected]";
			mailbox.Address = "[email protected]";
			Assert.IsTrue (InternetAddressList.TryParse (text, out result), "Failed to parse: {0}", text);
			AssertInternetAddressListsEqual (text, expected, result);

			result = InternetAddressList.Parse (text);
			AssertInternetAddressListsEqual (text, expected, result);

			text = "fejj";
			mailbox.Address = "fejj";
			Assert.IsTrue (InternetAddressList.TryParse (text, out result), "Failed to parse: {0}", text);
			AssertInternetAddressListsEqual (text, expected, result);

			result = InternetAddressList.Parse (text);
			AssertInternetAddressListsEqual (text, expected, result);
		}
コード例 #4
0
		static void AssertParse (string text, string encoded, InternetAddressList expected)
		{
			var buffer = Encoding.UTF8.GetBytes (text);
			InternetAddressList result = null;

			try {
				result = InternetAddressList.Parse (text);
			} catch {
				Assert.Fail ("Parse(string): {0}", text);
			}
			AssertInternetAddressListsEqual (encoded, expected, result);

			try {
				result = InternetAddressList.Parse (buffer);
			} catch {
				Assert.Fail ("Parse(byte[]): {0}", text);
			}
			AssertInternetAddressListsEqual (encoded, expected, result);

			try {
				result = InternetAddressList.Parse (buffer, 0);
			} catch {
				Assert.Fail ("Parse(byte[], int): {0}", text);
			}
			AssertInternetAddressListsEqual (encoded, expected, result);

			try {
				result = InternetAddressList.Parse (buffer, 0, buffer.Length);
			} catch {
				Assert.Fail ("Parse(byte[], int, int): {0}", text);
			}
			AssertInternetAddressListsEqual (encoded, expected, result);
		}
コード例 #5
0
ファイル: Envelope.cs プロジェクト: jstedfast/MailKit
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Envelope"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="Envelope"/>.
		/// </remarks>
		public Envelope ()
		{
			From = new InternetAddressList ();
			Sender = new InternetAddressList ();
			ReplyTo = new InternetAddressList ();
			To = new InternetAddressList ();
			Cc = new InternetAddressList ();
			Bcc = new InternetAddressList ();
		}
コード例 #6
0
		static void AssertInternetAddressListsEqual (string text, InternetAddressList expected, InternetAddressList result)
		{
			Assert.AreEqual (expected.Count, result.Count, "Unexpected number of addresses: {0}", text);

			for (int i = 0; i < expected.Count; i++) {
				Assert.AreEqual (expected.GetType (), result.GetType (),
				                 "Address #{0} differs in type: {1}", i, text);

				Assert.AreEqual (expected[i].ToString (), result[i].ToString (), "Display strings differ for {0}", text);
			}
		}
コード例 #7
0
        public void TestDecodedMailboxHasCorrectCharsetEncoding()
        {
            var latin1 = Encoding.GetEncoding ("iso-8859-1");
            var mailbox = new MailboxAddress (latin1, "Kristoffer Brånemyr", "[email protected]");
            var list = new InternetAddressList ();
            list.Add (mailbox);

            var encoded = list.ToString (UnixFormatOptions, true);

            InternetAddressList parsed;
            Assert.IsTrue (InternetAddressList.TryParse (encoded, out parsed), "Failed to parse address");
            Assert.AreEqual (latin1.HeaderName, parsed[0].Encoding.HeaderName, "Parsed charset does not match");
        }
コード例 #8
0
		static void AssertTryParse (string text, string encoded, InternetAddressList expected)
		{
			var buffer = Encoding.UTF8.GetBytes (text);
			InternetAddressList result;

			Assert.IsTrue (InternetAddressList.TryParse (text, out result), "TryParse(string): {0}", text);
			AssertInternetAddressListsEqual (encoded, expected, result);

			Assert.IsTrue (InternetAddressList.TryParse (buffer, out result), "TryParse(byte[]): {0}", text);
			AssertInternetAddressListsEqual (encoded, expected, result);

			Assert.IsTrue (InternetAddressList.TryParse (buffer, 0, out result), "TryParse(byte[], int): {0}", text);
			AssertInternetAddressListsEqual (encoded, expected, result);

			Assert.IsTrue (InternetAddressList.TryParse (buffer, 0, buffer.Length, out result), "TryParse(byte[] int, int): {0}", text);
			AssertInternetAddressListsEqual (encoded, expected, result);
		}
コード例 #9
0
		public void TestArgumentExceptions ()
		{
			var mailbox = new MailboxAddress ("MimeKit Unit Tests", "[email protected]");
			var list = new InternetAddressList ();

			list.Add (new MailboxAddress ("Example User", "[email protected]"));

			Assert.Throws<ArgumentNullException> (() => new InternetAddressList (null));
			Assert.Throws<ArgumentNullException> (() => list.Add (null));
			Assert.Throws<ArgumentNullException> (() => list.AddRange (null));
			Assert.Throws<ArgumentNullException> (() => list.CompareTo (null));
			Assert.Throws<ArgumentNullException> (() => list.Contains (null));
			Assert.Throws<ArgumentNullException> (() => list.CopyTo (null, 0));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.CopyTo (new InternetAddress[0], -1));
			Assert.Throws<ArgumentNullException> (() => list.IndexOf (null));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, mailbox));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, null));
			Assert.Throws<ArgumentNullException> (() => list.Remove (null));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAt (-1));
			Assert.Throws<ArgumentOutOfRangeException> (() => list[-1] = mailbox);
			Assert.Throws<ArgumentNullException> (() => list[0] = null);
		}
コード例 #10
0
		public void TestEquality ()
		{
			var list1 = new InternetAddressList ();

			list1.Add (new GroupAddress ("Local recipients", new InternetAddress[] {
				new MailboxAddress ("", "phil"),
				new MailboxAddress ("", "joe"),
				new MailboxAddress ("", "alex"),
				new MailboxAddress ("", "bob"),
			}));
			list1.Add (new MailboxAddress ("Joey", "[email protected]"));
			list1.Add (new MailboxAddress ("Chandler", "[email protected]"));

			var list2 = new InternetAddressList ();

			list2.Add (new MailboxAddress ("Chandler", "[email protected]"));
			list2.Add (new GroupAddress ("Local recipients", new InternetAddress[] {
				new MailboxAddress ("", "phil"),
				new MailboxAddress ("", "joe"),
				new MailboxAddress ("", "alex"),
				new MailboxAddress ("", "bob"),
			}));
			list2.Add (new MailboxAddress ("Joey", "[email protected]"));

			Assert.IsTrue (list1.Equals (list2), "The 2 lists should be equal.");
		}
コード例 #11
0
		public void TestUnsupportedCharsetExceptionNotThrown ()
		{
			var mailbox = new MailboxAddress (Encoding.UTF8, "狂ったこの世で狂うなら気は確かだ。", "[email protected]");
			var list = new InternetAddressList ();
			list.Add (mailbox);

			var encoded = list.ToString (true);

			encoded = encoded.Replace ("utf-8", "x-unknown");

			InternetAddressList parsed;

			try {
				Assert.IsTrue (InternetAddressList.TryParse (encoded, out parsed), "Failed to parse address");
			} catch (Exception ex) {
				Assert.Fail ("Exception thrown parsing address with unsupported charset: {0}", ex);
			}
		}
コード例 #12
0
		public void TestEncodingSimpleAddressList ()
		{
			const string expectedEncoded = "Kristoffer =?iso-8859-1?q?Br=E5nemyr?= <[email protected]>, Jeffrey Stedfast\n\t<[email protected]>";
			const string expectedDisplay = "\"Kristoffer Brånemyr\" <[email protected]>, \"Jeffrey Stedfast\" <[email protected]>";
			var latin1 = Encoding.GetEncoding ("iso-8859-1");
			var options = FormatOptions.Default.Clone ();
			var list = new InternetAddressList ();

			list.Add (new MailboxAddress (latin1, "Kristoffer Brånemyr", "[email protected]"));
			list.Add (new MailboxAddress ("Jeffrey Stedfast", "[email protected]"));

			options.NewLineFormat = NewLineFormat.Unix;

			var display = list.ToString (options, false);
			Assert.AreEqual (expectedDisplay, display, "Display value does not match the expected result: {0}", display);

			var encoded = list.ToString (options, true);
			Assert.AreEqual (expectedEncoded, encoded, "Encoded value does not match the expected result: {0}", display);
		}
コード例 #13
0
		public void TestEncodingMailboxWithJapaneseName ()
		{
			const string expected = "=?utf-8?b?54uC44Gj44Gf44GT44Gu5LiW44Gn54uC44GG44Gq44KJ5rCX44Gv56K644GL44Gg?=\n =?utf-8?b?44CC?= <[email protected]>";
			var mailbox = new MailboxAddress ("狂ったこの世で狂うなら気は確かだ。", "[email protected]");
			var list = new InternetAddressList ();
			list.Add (mailbox);

			var actual = list.ToString (UnixFormatOptions, true);

			Assert.AreEqual (expected, actual, "Encoding japanese mailbox did not match expected result: {0}", expected);
			Assert.IsTrue (InternetAddressList.TryParse (actual, out list), "Failed to parse japanese mailbox");
			Assert.AreEqual (mailbox.Name, list[0].Name);
		}
コード例 #14
0
		public void TestEncodingMailboxWithReallyLongWord ()
		{
			const string expected = "=?us-ascii?q?reeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaallllllllllll?=\n =?us-ascii?q?llllllllllllllllllllllllllllllllllllllllllly?= long word\n\t<[email protected]>";
			const string name = "reeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaallllllllllllllllllllllllllllllllllllllllllllllllllllllly long word";
			var mailbox = new MailboxAddress (name, "[email protected]");
			var list = new InternetAddressList ();
			list.Add (mailbox);

			var actual = list.ToString (UnixFormatOptions, true);

			Assert.AreEqual (expected, actual, "Encoding really long mailbox did not match expected result: {0}", expected);
			Assert.IsTrue (InternetAddressList.TryParse (actual, out list), "Failed to parse really long mailbox");
			Assert.AreEqual (mailbox.Name, list[0].Name);
		}
コード例 #15
0
		public void TestEncodingSimpleMailboxWithLatin1Name ()
		{
			var latin1 = Encoding.GetEncoding ("iso-8859-1");
			var mailbox = new MailboxAddress (latin1, "Kristoffer Brånemyr", "[email protected]");
			var list = new InternetAddressList ();
			list.Add (mailbox);

			var expected = "Kristoffer =?iso-8859-1?q?Br=E5nemyr?= <[email protected]>";
			var actual = list.ToString (UnixFormatOptions, true);

			Assert.AreEqual (expected, actual, "Encoding latin1 mailbox did not match expected result: {0}", expected);

			mailbox = new MailboxAddress (latin1, "Tõivo Leedjärv", "[email protected]");
			list = new InternetAddressList ();
			list.Add (mailbox);

			expected = "=?iso-8859-1?b?VIH1aXZvIExlZWRqgeRydg==?= <[email protected]>";
			actual = list.ToString (UnixFormatOptions, true);

			Assert.AreEqual (expected, actual, "Encoding latin1 mailbox did not match expected result: {0}", expected);
		}
コード例 #16
0
		public void TestListWithGroupAndAddrspec ()
		{
			const string text = "GNOME Hackers: Miguel de Icaza <[email protected]>, Havoc Pennington <[email protected]>;, [email protected]";
			const string encoded = "GNOME Hackers: Miguel de Icaza <[email protected]>, Havoc Pennington\n\t<[email protected]>;, [email protected]";
			var expected = new InternetAddressList ();

			expected.Add (new GroupAddress ("GNOME Hackers", new InternetAddress[] {
				new MailboxAddress ("Miguel de Icaza", "[email protected]"),
				new MailboxAddress ("Havoc Pennington", "[email protected]")
			}));
			expected.Add (new MailboxAddress ("", "[email protected]"));

			AssertParseAndTryParse (text, encoded, expected);
		}
コード例 #17
0
		public void TestEncodingSimpleMailboxWithQuotedName ()
		{
			const string expected = "\"Stedfast, Jeffrey\" <[email protected]>";
			var mailbox = new MailboxAddress ("Stedfast, Jeffrey", "[email protected]");
			var list = new InternetAddressList ();
			list.Add (mailbox);

			var actual = list.ToString (UnixFormatOptions, true);

			Assert.AreEqual (expected, actual, "Encoding quoted mailbox did not match expected result: {0}", expected);
		}
コード例 #18
0
		public void TestSimpleMailboxes ()
		{
			var expected = new InternetAddressList ();
			var mailbox = new MailboxAddress ("", "");
			string text;

			expected.Add (mailbox);

			mailbox.Name = "Jeffrey Stedfast";
			mailbox.Address = "[email protected]";
			text = "Jeffrey Stedfast <[email protected]>";
			AssertParseAndTryParse (text, text, expected);

			mailbox.Name = "this is a folded name";
			mailbox.Address = "[email protected]";
			text = "this is\n\ta folded name <[email protected]>";
			AssertParseAndTryParse (text, "this is a folded name <[email protected]>", expected);

			mailbox.Name = "Jeffrey fejj Stedfast";
			mailbox.Address = "[email protected]";
			text = "Jeffrey \"fejj\" Stedfast <[email protected]>";
			AssertParseAndTryParse (text, "Jeffrey fejj Stedfast <[email protected]>", expected);

			mailbox.Name = "Jeffrey \"fejj\" Stedfast";
			mailbox.Address = "[email protected]";
			text = "\"Jeffrey \\\"fejj\\\" Stedfast\" <[email protected]>";
			AssertParseAndTryParse (text, text, expected);

			mailbox.Name = "Stedfast, Jeffrey";
			mailbox.Address = "[email protected]";
			text = "\"Stedfast, Jeffrey\" <[email protected]>";
			AssertParseAndTryParse (text, text, expected);

			mailbox.Name = "Jeffrey Stedfast";
			mailbox.Address = "[email protected]";
			text = "[email protected] (Jeffrey Stedfast)";
			AssertParseAndTryParse (text, "Jeffrey Stedfast <[email protected]>", expected);

			mailbox.Name = "Jeffrey Stedfast";
			mailbox.Address = "[email protected]";
			text = "Jeffrey Stedfast <fejj(recursive (comment) block)@helixcode.(and a comment here)com>";
			AssertParseAndTryParse (text, "Jeffrey Stedfast <[email protected]>", expected);

			mailbox.Name = "Jeffrey Stedfast";
			mailbox.Address = "[email protected]";
			text = "Jeffrey Stedfast <[email protected]>";
			AssertParseAndTryParse (text, "Jeffrey Stedfast <[email protected]>", expected);
		}
コード例 #19
0
ファイル: MimeMessage.cs プロジェクト: yukine/MimeKit
		void AddAddresses (Header header, InternetAddressList list)
		{
			int length = header.RawValue.Length;
			List<InternetAddress> parsed;
			int index = 0;

			// parse the addresses in the new header and add them to our address list
			if (!InternetAddressList.TryParse (Headers.Options, header.RawValue, ref index, length, false, false, out parsed))
				return;

			list.Changed -= InternetAddressListChanged;
			list.AddRange (parsed);
			list.Changed += InternetAddressListChanged;
		}
コード例 #20
0
ファイル: MimeMessage.cs プロジェクト: yukine/MimeKit
		internal MimeMessage (ParserOptions options)
		{
			addresses = new Dictionary<string, InternetAddressList> (StringComparer.OrdinalIgnoreCase);
			Headers = new HeaderList (options);

			// initialize our address lists
			foreach (var name in StandardAddressHeaders) {
				var list = new InternetAddressList ();
				list.Changed += InternetAddressListChanged;
				addresses.Add (name, list);
			}

			references = new MessageIdList ();
			references.Changed += ReferencesChanged;
			inreplyto = null;

			Headers.Changed += HeadersChanged;
		}
コード例 #21
0
		public void TestMailboxWithDotsInTheName ()
		{
			const string encoded = "\"Nathaniel S. Borenstein\" <[email protected]>";
			const string text = "Nathaniel S. Borenstein <[email protected]>";
			var expected = new InternetAddressList ();

			expected.Add (new MailboxAddress ("Nathaniel S. Borenstein", "[email protected]"));

			AssertParseAndTryParse (text, encoded, expected);
		}
コード例 #22
0
		public void TestExampleGroupWithCommentsFromRfc5322 ()
		{
			const string text = "A Group(Some people):Chris Jones <[email protected](Chris's host.)public.example>, [email protected], John <[email protected]> (my dear friend); (the end of the group)";
			const string encoded = "A Group: Chris Jones <[email protected]>, [email protected], John <[email protected]>;";
			var expected = new InternetAddressList ();

			expected.Add (new GroupAddress ("A Group", new InternetAddress[] {
				new MailboxAddress ("Chris Jones", "[email protected]"),
				new MailboxAddress ("", "[email protected]"),
				new MailboxAddress ("John", "[email protected]")
			}));

			AssertParseAndTryParse (text, encoded, expected);
		}
コード例 #23
0
		public void TestLocalGroupWithoutSemicolon ()
		{
			const string text = "Local recipients: phil, joe, alex, bob";
			const string encoded = "Local recipients: phil, joe, alex, bob;";
			var expected = new InternetAddressList ();

			expected.Add (new GroupAddress ("Local recipients", new InternetAddress[] {
				new MailboxAddress ("", "phil"),
				new MailboxAddress ("", "joe"),
				new MailboxAddress ("", "alex"),
				new MailboxAddress ("", "bob"),
			}));

			AssertTryParse (text, encoded, expected);

			//Assert.Throws<ParseException> (() => InternetAddressList.Parse (text), "Parsing should have failed.");
		}
コード例 #24
0
		public void TestCompareTo ()
		{
			var list1 = new InternetAddressList ();

			list1.Add (new GroupAddress ("Local recipients", new InternetAddress[] {
				new MailboxAddress ("", "phil"),
				new MailboxAddress ("", "joe"),
				new MailboxAddress ("", "alex"),
				new MailboxAddress ("", "bob"),
			}));
			list1.Add (new MailboxAddress ("Joey", "[email protected]"));
			list1.Add (new MailboxAddress ("Chandler", "[email protected]"));

			var list2 = new InternetAddressList ();

			list2.Add (new MailboxAddress ("Chandler", "[email protected]"));
			list2.Add (new GroupAddress ("Local recipients", new InternetAddress[] {
				new MailboxAddress ("", "phil"),
				new MailboxAddress ("", "joe"),
				new MailboxAddress ("", "alex"),
				new MailboxAddress ("", "bob"),
			}));
			list2.Add (new MailboxAddress ("Joey", "[email protected]"));

			Assert.IsTrue (list1.CompareTo (list2) > 0, "CompareTo() should return < 0.");
			Assert.IsTrue (list2.CompareTo (list1) < 0, "CompareTo() should return > 0.");
		}
コード例 #25
0
		public void TestMailboxWith8bitName ()
		{
			const string encoded = "Patrik =?iso-8859-1?b?RqVkbHRzdHKldm0=?= <[email protected]>";
			const string text = "Patrik F¥dltstr¥vm <[email protected]>";
			var expected = new InternetAddressList ();

			expected.Add (new MailboxAddress ("Patrik F¥dltstr¥vm", "[email protected]"));

			AssertParseAndTryParse (text, encoded, expected);
		}
コード例 #26
0
		public void TestMailboxesWithRfc2047EncodedNames ()
		{
			var expected = new InternetAddressList ();
			var mailbox = new MailboxAddress ("", "");
			string text;

			expected.Add (mailbox);

			mailbox.Name = "Kristoffer Brånemyr";
			mailbox.Address = "[email protected]";
			text = "=?iso-8859-1?q?Kristoffer_Br=E5nemyr?= <[email protected]>";
			AssertParseAndTryParse (text, "Kristoffer =?iso-8859-1?q?Br=E5nemyr?= <[email protected]>", expected);

			mailbox.Name = "François Pons";
			mailbox.Address = "[email protected]";
			text = "=?iso-8859-1?q?Fran=E7ois?= Pons <[email protected]>";
			AssertParseAndTryParse (text, text, expected);
		}
コード例 #27
0
ファイル: MimeMessage.cs プロジェクト: yukine/MimeKit
		void SerializeAddressList (string field, InternetAddressList list)
		{
			var builder = new StringBuilder (" ");
			var options = FormatOptions.Default;
			int lineLength = field.Length + 2;

			list.Encode (options, builder, ref lineLength);
			builder.Append (options.NewLine);

			var raw = Encoding.UTF8.GetBytes (builder.ToString ());

			ReplaceHeader (field.ToHeaderId (), field, raw);
		}
コード例 #28
0
		public void TestObsoleteMailboxRoutingSyntax ()
		{
			const string text = "Routed Address <@route:[email protected]>";
			var expected = new InternetAddressList ();

			expected.Add (new MailboxAddress ("Routed Address", new [] { "route" }, "[email protected]"));

			AssertParseAndTryParse (text, text, expected);
		}
コード例 #29
0
ファイル: MimeMessage.cs プロジェクト: yukine/MimeKit
		void ReloadAddressList (HeaderId id, InternetAddressList list)
		{
			// clear the address list and reload
			list.Changed -= InternetAddressListChanged;
			list.Clear ();

			foreach (var header in Headers) {
				if (header.Id != id)
					continue;

				int length = header.RawValue.Length;
				List<InternetAddress> parsed;
				int index = 0;

				if (!InternetAddressList.TryParse (Headers.Options, header.RawValue, ref index, length, false, false, out parsed))
					continue;

				list.AddRange (parsed);
			}

			list.Changed += InternetAddressListChanged;
		}
コード例 #30
0
		public void TestObsoleteMailboxRoutingSyntaxWithEmptyDomains ()
		{
			const string text = "Routed Address <@route1,,@route2,,,@route3:[email protected]>";
			const string encoded = "Routed Address <@route1,@route2,@route3:[email protected]>";
			var expected = new InternetAddressList ();

			expected.Add (new MailboxAddress ("Routed Address", new [] { "route1", "route2", "route3" }, "[email protected]"));

			AssertParseAndTryParse (text, encoded, expected);
		}