Skip to content

Commit

Permalink
IPGlobalProperties.GetIPGlobalProperties() can throw NotSupported on …
Browse files Browse the repository at this point in the history
…WASM

Wrap SmtpClient static ctor logic in a try/catch and default to "localhost".

Fixes issue #1381
  • Loading branch information
jstedfast committed May 28, 2022
1 parent f0ef2b6 commit 185ffc7
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions MailKit/Net/Smtp/SmtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,29 @@ enum SmtpCommand {

static SmtpClient ()
{
string hostName = IPGlobalProperties.GetIPGlobalProperties ().HostName;
var idn = new IdnMapping ();
string hostName;

if (!string.IsNullOrEmpty (hostName)) {
try {
hostName = idn.GetAscii (hostName);
} catch (ArgumentException) {
// This can happen if the hostName contains illegal unicode characters.
var ascii = new StringBuilder ();
for (int i = 0; i < hostName.Length; i++) {
if (hostName[i] <= 0x7F)
ascii.Append (hostName[i]);
}
try {
hostName = IPGlobalProperties.GetIPGlobalProperties ().HostName;
var idn = new IdnMapping ();

if (!string.IsNullOrEmpty (hostName)) {
try {
hostName = idn.GetAscii (hostName);
} catch (ArgumentException) {
// This can happen if the hostName contains illegal unicode characters.
var ascii = new StringBuilder ();
for (int i = 0; i < hostName.Length; i++) {
if (hostName[i] <= 0x7F)
ascii.Append (hostName[i]);
}

hostName = ascii.Length > 0 ? ascii.ToString () : null;
hostName = ascii.Length > 0 ? ascii.ToString () : null;
}
} else {
hostName = null;
}
} else {
} catch {
hostName = null;
}

Expand Down

0 comments on commit 185ffc7

Please sign in to comment.