Home SMTP Implicit TLS and Explicit TLS using Indy 10.6.2.5341 in Delphi
Reply: 1

SMTP Implicit TLS and Explicit TLS using Indy 10.6.2.5341 in Delphi

Aaron
1#
Aaron Published in 2017-09-14 05:11:51Z

A bit of an odd one here, when setting "UseTLS" to utUseExplicitTLS and then connecting to a mail server on its Implicit TLS port, the first attempt allows the connection and sends the email, subsequent attempts on that port correctly fail.

Just wondering if anyone has any ideas on how to avoid the false positive on the initial connect and send.

The check is to handle that non-standard ports may be getting used for a user's mail server. Pretty much all examples I've seen assume that the correct information will always be provided.

Below is the code portion that handles it (excluding error logging):

function SendTestEmail(EmailAddress: String): Boolean;
var
  EmailMessage: TidMessage;
begin
  IdSMTPEmail.AuthType := satDefault
  IdSMTPEmail.Username := ...;
  IdSMTPEmail.Password := ...;
  IdSMTPEmail.Port := 465;

  IdSMTPEmail.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdSMTPEmail);
  IdSMTPEmail.UseTLS := utUseExplicitTLS;
  TIdSSLIOHandlerSocketOpenSSL(IdSMTPEmail.IOHandler).SSLOptions.Method := sslvTLSv1_2;
  try
    // Connect
    IdSMTPEmail.Connect('smtp.gmail.com');

    try
      // Create
      EmailMessage := TidMessage.Create(nil);

      try
        // Set values
        EmailMessage.Body.Add('Test Email');
        EmailMessage.Subject := 'Test Email';

        // Set sender details
        EmailMessage.From.Address := 'test@test.com';
        EmailMessage.From.Name := 'SSL Test';

        // Set recipient
        EmailMessage.Recipients.Add.Address := EmailAddress;

        try
          // Send message
          IdSMTPEmail.Send(EmailMessage);

        except
          // Exception
          on E: EIdSMTPReplyError do
          begin
            // Result
            Result := False;
          end;
        end;

      finally
        // Free email
        EmailMessage.Free;
      end;

    finally
      // Disconnect
      IdSMTPEmail.Disconnect;
    end;

  except
    // Exception
    on E: Exception do
    begin
      IdSMTPEmail.Disconnect;
      // Result
      Result := False;
    end;
  end;
end;
Aaron
2#
Aaron Reply to 2017-09-15 06:40:57Z

Using the below code, correctly failed on all attempts of trying to email to port 465.

I don't even pretend to understand why this works but my original didn't.

function SendTestEmail(EmailAddress: String): Boolean;
var
  EmailMessage: TidMessage;
  IdSSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  IdSMTPEmail.AuthType := satDefault;

  IdSSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
  IdSSLHandler.SSLOptions.Method := sslvTLSv1_2;

  IdSMTPEmail.IOHandler := IdSSLHandler;
  IdSMTPEmail.UseTLS := utUseExplicitTLS;

  IdSMTPEmail.Username := ...;
  IdSMTPEmail.Password := ...;
  IdSMTPEmail.Port := 465;

  try
    // Connect
    IdSMTPEmail.Connect('smtp.gmail.com');

    try
      // Create
      EmailMessage := TidMessage.Create(nil);

      try
        // Set values
        EmailMessage.Body.Add('Test Email');
        EmailMessage.Subject := 'Test Email';

        // Set sender details
        EmailMessage.From.Address := 'test@test.com';
        EmailMessage.From.Name := 'SSL Test';

        // Set recipient
        EmailMessage.Recipients.Add.Address := EmailAddress;

        try
          // Send message
          IdSMTPEmail.Send(EmailMessage);

        except
          // Exception
          on E: EIdSMTPReplyError do
          begin
            // Result
            Result := False;
          end;
        end;

      finally
        // Free email
        EmailMessage.Free;
      end;

    finally
      // Disconnect
      IdSMTPEmail.Disconnect;
    end;

  except
    // Exception
    on E: Exception do
    begin
      IdSMTPEmail.Disconnect;
      // Result
      Result := False;
    end;
  end;
end;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.259149 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO