title: ERROR: Connection Closed Gracefully
body: <p>I have tried the below code for sending the mail, but it shows connection closed gracefully error. I have tried 587 and 465 port also. Authentication also raise the error for "5.7.0 Must issue a STARTTLS command first. q27sm64805279pfi.83 - gsmtp".</p>
  
  <pre><code>uses
    IdSMTP, IdMessage, IdEMailAddress;
  
  procedure SendSimpleMail;
  var
    IdSMTP: TIdSMTP;
    IdMessage: TIdMessage;
    IdEmailAddressItem: TIdEmailAddressItem;
  begin
    IdSMTP := TIdSMTP.Create(nil);
    try
      IdSMTP.Host := 'smtp.gmail.com';
      IdSMTP.Port := 25;   // or 587    //or 465
      IdSMTP.AuthType := satDefault;
      IdSMTP.Username := 'username@gmail.com';
      IdSMTP.Password := 'password';
      IdSMTP.Connect;
      if IdSMTP.Authenticate then
      begin
        IdMessage := TIdMessage.Create(nil);
        try
          IdMessage.From.Name := 'User Name';
          IdMessage.From.Address := 'username@gmail.com';
          IdMessage.Subject := 'E-mail subject';
          IdMessage.Body.Add('E-mail body.');
  
          IdEmailAddressItem := IdMessage.Recipients.Add;
          IdEmailAddressItem.Address := 'recipient@email.com';
  
          IdSMTP.Send(IdMessage);
        finally
          IdMessage.Free;
        end;
      end;
      IdSMTP.Disconnect;
    finally
      IdSMTP.Free;
    end;
  end;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mtzAp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mtzAp.png" alt="enter image description here"></a></p>