Jump to content
Ian Branch

smtp client in a console app??

Recommended Posts

Hi Guys,

D10.4.2, latest Indy 10, Win 10.

I have created a console app with my Indy code in it.

The code is exactly the same as I have in my VCL app, same settings/parameters, etc, which works.

But.  When I run it in the cmd prompt I get the following error.

>>EIdTLSClientTLSHandShakeFailed: SSL negotiation failed.<<

Now, I freely acknowledge I probably don't have something set right. 😞

Here is the source I have att..

program DBiPREmail;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  IdMessage, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP, IdBaseComponent, IdComponent,
  IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdAttachment, IdMessageParts, IdEMailAddress, IdAttachmentFile, IdGlobal,
  IdText;  //  These are all in just to start.  To be cleaned up when working..

var
  dmPREmail: TdmPREmail;
  IdSMTP: TIdSMTP;
  IdMessage: TIdMessage;
  IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; // to allow SSL authenticate //

  try
    //
    try
      //
      IdSMTP := TIdSMTP.Create(nil);
      //
      try
        //
        IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(IdSMTP);
        //
        IdMessage := TIdMessage.Create(IdSMTP);
        //
        // IO HANDLER SETTINGS //
        with IdSSLIOHandlerSocketOpenSSL1 do
        begin
          MaxLineAction := maException;
          SSLOptions.Method := sslvTLSv1;
        end;
        //
        IdSMTP.Host := 'my smtp email host';
        //
        IdSMTP.Port := 587;
        //
        IdSMTP.Username := 'my user name';
        IdSMTP.Password := 'my password';
        //
        IdSMTP.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
        IdSMTP.AuthType := satDefault;
        //
        IdSMTP.UseTLS := utUseExplicitTLS;
        //
        // SETTING email MESSAGE DATA //
        IdMessage.Clear;
        //
        IdMessage.From.Address := 'my email address';
        IdMessage.Recipients.EMailAddresses := 'second email address';
        IdMessage.CCList.EMailAddresses := 'third email address';
        //
        IdMessage.Subject := 'Test Email Subject';
        IdMessage.Body.text := 'Test Email Body';
        IdMessage.Priority := mpHigh;
        //
        with TIdText.Create(IdMessage.MessageParts, nil) do
        begin
          //
          Body.text := Body.text +
            '<p style="color: #5e9ca0;">This is a test <strong>message</strong> for <span style="color: #ff0000;"><strong>emailing</strong></span>.</p><h1 style="color: #5e9ca0;"> </h1>';
          ContentType := 'text/html';
        end;
        //
        IdMessage.ContentType := 'multipart/mixed';
        //
        try
          IdSMTP.Connect;
        except
          on E: Exception do
          begin
            Writeln(E.ClassName, ': ', E.Message);
            Exit;
          end;
        end;
        //
        try
          try
            IdSMTP.Send(IdMessage);
          finally
            IdSMTP.Disconnect();
          end;
        except
          on E: Exception do
          begin
            Writeln(E.ClassName, ': ', E.Message);
            Exit;
          end;
        end;
        //
      finally
        //
        IdSMTP.Free;
        //
      end;
      //
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  //
end.

Thoughts, suggestions appreciated..

 

Regards & TIA,

Ian

 

Share this post


Link to post
10 hours ago, Ian Branch said:

The code is exactly the same as I have in my VCL app, same settings/parameters, etc, which works.

But.  When I run it in the cmd prompt I get the following error.

>>EIdTLSClientTLSHandShakeFailed: SSL negotiation failed.<< 

EIdTLSClientTLSHandShakeFailed is raised when another exception is caught internally during the SSL/TLS handshake process.  So the EIdTLSClientTLSHandShakeFailed.InnerException should not be nil.  What are the values of the ClassName and Message of that InnerException?

Edited by Remy Lebeau

Share this post


Link to post

Doh!  Stupid, stupid me!  I forgot to put the two .dll files in the project directory. :-(

All good now.

 

Thanks Guys,

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×