Skip to content

Commit

Permalink
Provide MailboxAddress accessors for LocalPart and Domain.
Browse files Browse the repository at this point in the history
Fixes issue #766
  • Loading branch information
jstedfast committed Feb 14, 2022
1 parent 7691163 commit 35cd96d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions MimeKit/MailboxAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,30 @@ public string Address {
}
}

/// <summary>
/// Get the local-part of the email address.
/// </summary>
/// <remarks>
/// Gets the local-part of the email address, sometimes referred to as the "user" portion of an email address.
/// </remarks>
public string LocalPart {
get {
return at != -1 ? address.Substring (0, at) : address;
}
}

/// <summary>
/// Get the domain of the email address.
/// </summary>
/// <remarks>
/// Gets the domain of the email address.
/// </remarks>
public string Domain {
get {
return at != -1 ? address.Substring (at + 1) : string.Empty;
}
}

/// <summary>
/// Get whether or not the address is an international address.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions UnitTests/MailboxAddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public void ArgumentExceptionTests ()
Assert.DoesNotThrow (() => new SecureMailboxAddress (Encoding.UTF8, "Routed Address", new[] { "route1", "route2", "route3" }, "[email protected]", "ffff"));
}

[Test]
public void TestLocalPartAndDomain ()
{
var mailbox = new MailboxAddress ("User Name", "[email protected]");

Assert.AreEqual ("user", mailbox.LocalPart, "LocalPart");
Assert.AreEqual ("domain.com", mailbox.Domain, "Domain");

mailbox = new MailboxAddress ("User Name", "user");

Assert.AreEqual ("user", mailbox.LocalPart, "Unix LocalPart");
Assert.AreEqual (string.Empty, mailbox.Domain, "Unix non-Domain");
}

[Test]
public void TestSetEmptyAddress ()
{
Expand Down

0 comments on commit 35cd96d

Please sign in to comment.