Mailbox AddressTry Parse(Parser Options, String, Mailbox Address) Method
Try to parse the given text into a new MailboxAddress instance.
Definition
Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
if the address was successfully parsed; otherwise, .
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public static bool TryParse(
ParserOptions? options,
string? text,
out MailboxAddress?? mailbox
)Parameters
- options ParserOptions
- The parser options to use.
- text String
- The text.
- mailbox MailboxAddress
- The parsed mailbox address.
Return Value
Booleanif the address was successfully parsed; otherwise, .
Remarks
Parses a single MailboxAddress. If the address is not a mailbox address or
there is more than a single mailbox address, then parsing will fail.
Example
C#
public static bool IsValidMailbox (string text)
{
// TryParse() is the best way to validate user input since it does not throw.
if (!MailboxAddress.TryParse (text, out var mailbox))
return false;
// The MimeKit address parser is fairly lenient by default. For example, it will
// accept addresses that do not have a domain, such as "postmaster".
return !string.IsNullOrEmpty (mailbox.Domain);
}
public static bool IsValidMailboxStrict (string text)
{
// For stricter validation, use ParserOptions with an RfcComplianceMode of Strict and
// disallow addresses without a domain.
var options = new ParserOptions {
AddressParserComplianceMode = RfcComplianceMode.Strict,
AllowAddressesWithoutDomain = false,
AllowUnquotedCommasInAddresses = false
};
return MailboxAddress.TryParse (options, text, out _);
}