public void TestEmailAddress() { EmailAddress email = new EmailAddress("*****@*****.**", "Dr. Who"); bool valid = email.Valid; //True string emailValue = email.Value; //[email protected] string domain = email.Domain; //where.com string username = email.User; //who string name = email.Name; //Dr. Who string formatted = email.EmailWithName; //Dr. Who <*****@*****.**> string emailToString = email.ToString(); //[email protected] string emailStringImplicit = (string)email; //[email protected] string link = email.ToLink(); //<a href="mailto:[email protected]">[email protected]</a> object sqlObj = email.ToSql(); //[email protected] or DBNull.Value when empty Assert.IsTrue(valid); Assert.AreEqual(emailValue, "*****@*****.**"); Assert.AreEqual(domain, "where.com"); Assert.AreEqual(username, "who"); Assert.AreEqual(name, "Dr. Who"); Assert.AreEqual(formatted, "Dr. Who <*****@*****.**>"); Assert.AreEqual(emailToString, "*****@*****.**"); Assert.AreEqual(emailStringImplicit, "*****@*****.**"); Assert.AreEqual(link, "<a href=\"mailto:[email protected]\">[email protected]</a>"); Assert.AreEqual(sqlObj, "*****@*****.**"); }
/// <summary> /// Convert a string into a EmailAddress object /// </summary> public static object ToSql(EmailAddress input) { if (input == null) return DBNull.Value; else return input.ToSql(); }