title: System.Net.Mail timing out on send
body: <p>I have some older code that works fine for sending emails, but visual studio tells me that the code is obsolete and I should change it over to <code>Net.Mail</code> from <code>Web.Mail</code>. I have rewritten most of it, but I have a few questions.</p>
  
  <p>Here is the original, working code:</p>
  
  <pre><code>public void Send(string from, string to, string subject, string body, bool isHtml, string[] attachments)
  {
  
      var mailMessage = new MailMessage
  
      {
          From = from,
          To = to,
          Subject = subject,
          Body = body,
          BodyFormat = isHtml ? MailFormat.Html : MailFormat.Text
      };
  
  
      // Add attachments
      if (attachments != null)
      {
          foreach (var t in attachments)
          {
              mailMessage.Attachments.Add(new Attachment(t));
          }
      }
      mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
      mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _accountName);
      mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password);
      mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _port.ToString(CultureInfo.InvariantCulture)); 
      mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
  
      SmtpMail.SmtpServer = _smtp;
      SmtpMail.Send(mailMessage);
  }
  </code></pre>
  
  <p>And here is the rewritten portion (well, sort of):</p>
  
  <pre><code>public void Send2(string from, string to, string subject, string body, bool isHtml, string[] attachments)
  {
  var fromObj = new MailAddress(from);
  var toObj = new MailAddress(to);
  
  
  var mailMessage = new System.Net.Mail.MailMessage
                        {
                            From = fromObj,
                            Subject = subject,
                            Body = body,
                            IsBodyHtml = isHtml,
                        };
  
  mailMessage.To.Add(toObj);
  
  if (attachments != null)
  {
      foreach(var t in attachments)
      {
          mailMessage.Attachments.Add(new Attachment(t));
      }
  }
  
  var smtp = new SmtpClient(_smtp) {Credentials = new NetworkCredential(_accountName, _password), Port = _port, EnableSsl = true};
  smtp.Send(mailMessage);
  }
  </code></pre>
  
  <p>In case you were wondering, I have <code>_port</code> and <code>_smtp</code> set higher up in the code, 465 and smtp.gmail.com respectively.</p>
  
  <p>So it seems to work, but then gets down to the sending part and spits out one of these:</p>
  
  <pre><code>System.Net.Mail.SmtpException: The operation has timed out.
  </code></pre>
  
  <p>Is there something I am missing, like from the <code>Fields</code> in the original code, that is causing it to timeout?</p>
  
  <p>Thanks!</p>
  
  <h1>SOLUTION</h1>
  
  <p>Thanks to <a href="https://stackoverflow.com/users/2420979/davidh" title="DavidH">DavidH</a>'s point in the right direction, the port needs to be changed from <strong>465</strong> to <strong>587</strong> (or 25; I am using the former with no problem).</p>