[System] Make SmtpClient fallback to it's old behaviour if the hostname cannot be...
authorSebastien Pouliot <sebastien@xamarin.com>
Tue, 26 Jan 2016 18:11:46 +0000 (13:11 -0500)
committerSebastien Pouliot <sebastien@xamarin.com>
Tue, 26 Jan 2016 18:11:46 +0000 (13:11 -0500)
Warning: System.Net.Mail is broken by design and sends incorrect emails.

This is not to fix SmtpClient itself but to ensure it remains compatible
(even if incorrect/broken) with older Mono releases.

The commit associated with bug# 33551 [1] can cause issues on computer
and devices that cannot resolve their hostname using the Dns API (which
have their own issues).

This commit catch the exception that occurs when resolution fails and
continue to send an email without a fully qualified domain name - just
like earlier Mono versions did in the past.

This solve bug #32746 [2] where some, but not all, AppleTV devices fails
to execute some of the System.dll unit tests.

Note: Whenever possible stay clear of SmtpClient (and friends) and use
something else like MimeKit/MailKit for your email needs

[1] https://bugzilla.xamarin.com/show_bug.cgi?id=33551
[2] https://bugzilla.xamarin.com/show_bug.cgi?id=37246

mcs/class/System/System.Net.Mail/SmtpClient.cs

index f156c7277ae752d38f462e1bb46631dfbb07bf06..0e89b187d1a97a216d0677c1cfb612e88cfb97eb 100644 (file)
@@ -592,8 +592,16 @@ namespace System.Net.Mail {
                        // FIXME: parse the list of extensions so we don't bother wasting
                        // our time trying commands if they aren't supported.
                        
-                       // Get the FQDN of the local machine
-                       string fqdn = Dns.GetHostEntry (Dns.GetHostName ()).HostName;
+                       // get the host name (not fully qualified)
+                       string fqdn = Dns.GetHostName ();
+                       try {
+                               // we'll try for the fully qualified name - ref: bug #33551
+                               fqdn = Dns.GetHostEntry (fqdn).HostName;
+                       }
+                       catch (SocketException) {
+                               // we could not resolve our name but will continue with the partial name
+                               // IOW we won't fail to send email because of this - ref: bug #37246
+                       }
                        status = SendCommand ("EHLO " + fqdn);
                        
                        if (IsError (status)) {