title: MailKit - SmtpClient Connect method freezes
body: <p>I'm developing a Xamarin android app where at some point (after a successful transaction) the app must send an information email to the customer. The whole process of creating the MimeMessage instance as well as the actual sending of it is done through a task. The message (which most of the time contains linked images) is created flawlessly but as soon as I call a method to actually send it the execution freezes at that call and the whole task remains active but frozen at that point.</p>
  
  <p>Here's my <code>SendMail</code> method which, beside of the <code>MimeMessage</code> parameter, receives a MailAccount class with information about the smtp machine, port and sender account. As soon as a call is made to the <code>Connect</code> method of the MailKit's <code>SmtpClient</code> instance I'm having the "frozen" behavior I described. I tried the whole thing many times - not even once the code continued beyond that line.</p>
  
  <pre><code>private static int SendMail(MailAccount account, MimeMessage message)
  {
      using (var client = new SmtpClient())
      {
          try
          {
              client.Connect(account.Server, account.Port, false);
          }
          catch (SmtpCommandException)
          {
              return -1;
          }
          catch (SmtpProtocolException)
          {
              return -2;
          }
  
          client.AuthenticationMechanisms.Remove("XOAUTH2");
  
          if (client.Capabilities.HasFlag(SmtpCapabilities.Authentication))
          {
              try
              {
                  client.Authenticate(account.User, Decrypt(account.Pass));
              }
              catch (AuthenticationException)
              {
                  return -3;
              }
              catch (SmtpCommandException)
              {
                  return -1;
              }
              catch (SmtpProtocolException)
              {
                  return -2;
              }
          }
  
          int status = 1; //assume success
  
          try
          {
              client.Send(message);
          }
          catch (SmtpCommandException ex)
          {
              switch (ex.ErrorCode)
              {
                  case SmtpErrorCode.MessageNotAccepted:
                      status = -6;
                      break;
  
                  case SmtpErrorCode.SenderNotAccepted:
                      status = -5;
                      break;
  
                  case SmtpErrorCode.RecipientNotAccepted:
                      status = -4;
                      break;
  
                  default:
                      status = -7;
                      break;
              }
          }
  
          client.Disconnect(true);
          return status;
      }
  }
  </code></pre>