Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi SMTP mail using Gmail box



Permlink Replies: 7 - Last Post: Apr 3, 2017 4:26 PM Last Post By: Achim Kalwa
João Paulo Cruz...

Posts: 2
Registered: 4/29/17
Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 5:11 AM
Hello, I have this code, and I have checked everything but I don't know why Am I getting "Connection Closed Gracefully".
This code is to sent a mail, using gmail.
Can somebody help?
Thanks.


   try
      Msg := TIdMessage.Create(nil);
      Msg.From.Address := 'mymail@gmail.com';
      Msg.Recipients.EMailAddresses := 'recepientmail@gmail.com';
      Msg.Subject := 'Test';
      Msg.Body.Text := 'Test';
      SMTP := TIdSMTP.Create(nil);
      SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
      SSLHandler.MaxLineAction := maException;
      SSLHandler.SSLOptions.Method := sslvSSLv23;
      SSLHandler.SSLOptions.Mode := sslmUnassigned;
      SSLHandler.SSLOptions.VerifyMode := [];
      SSLHandler.SSLOptions.VerifyDepth := 0;
      SMTP.IOHandler := SSLHandler;
      SMTP.Host := 'smtp.gmail.com';
      SMTP.Port := 587;
      SMTP.Username := 'mymail@gmail.com';
      SMTP.Password := 'mypassword';
      SMTP.UseTLS := utUseRequireTLS;
      SMTP.Connect;
      SMTP.Send(Msg);
   finally
      if SMTP.Connected then SMTP.Disconnect;
      Msg.Free;
      SSLHandler.Free;
      SMTP.Free;
   end;
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 5:20 AM   in response to: João Paulo Cruz... in response to: João Paulo Cruz...
Maybe I remember it well that you should configure GMail (by webbrowser Google user login) to accept logins from outside the GMail system?!
João Paulo Cruz...

Posts: 2
Registered: 4/29/17
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 6:01 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:
Maybe I remember it well that you should configure GMail (by webbrowser Google user login) to accept logins from outside the GMail system?!

I forgot to say it, I already did it, because it works with C#
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 9:39 AM   in response to: João Paulo Cruz... in response to: João Paulo Cruz...
João Paulo Cruz dos Santos wrote:
Hello, I have this code, and I have checked everything but I don't know why Am I getting "Connection Closed Gracefully".
This code is to sent a mail, using gmail.
Can somebody help?
Thanks.

 
   try
      Msg := TIdMessage.Create(nil);
      Msg.From.Address := 'mymail@gmail.com';
      Msg.Recipients.EMailAddresses := 'recepientmail@gmail.com';
      Msg.Subject := 'Test';
      Msg.Body.Text := 'Test';
      SMTP := TIdSMTP.Create(nil);
      SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
      SSLHandler.MaxLineAction := maException;
      SSLHandler.SSLOptions.Method := sslvSSLv23;
      SSLHandler.SSLOptions.Mode := sslmUnassigned;
      SSLHandler.SSLOptions.VerifyMode := [];
      SSLHandler.SSLOptions.VerifyDepth := 0;
      SMTP.IOHandler := SSLHandler;
      SMTP.Host := 'smtp.gmail.com';
      SMTP.Port := 587;
      SMTP.Username := 'mymail@gmail.com';
      SMTP.Password := 'mypassword';
      SMTP.UseTLS := utUseRequireTLS;
      SMTP.Connect;
      SMTP.Send(Msg);
   finally
      if SMTP.Connected then SMTP.Disconnect;
      Msg.Free;
      SSLHandler.Free;
      SMTP.Free;
   end;

Only differences with code I have that is working is the IO Handler's Method is
set to the default sslvTLSv1 and I am setting the port for the handler to 587 too.

     // Create the SSL handler
     id1 := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
     with id1 do
     begin
       Name := 'id1';
       Destination := 'smtp.gmail.com:587';
       Host := 'smtp.gmail.com';
       MaxLineAction := maException;
       Port := 587;
       DefaultPort := 0;
     end;
 
     // Create the SMTP sender
     smtp1 := TIdSMTP.Create(nil);
     with smtp1 do
     begin
       Name := 'smtp1';
       IOHandler := id1;
       Host := 'smtp.gmail.com';
       Password := 'xxxxxxxxxx';
       Port := 587;
       UseTLS := utUseExplicitTLS;
       Username := 'xxxxxx@gmail.com';
     end;

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 4:26 PM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Jeff wrote:

Destination := 'smtp.gmail.com:587';
Host := 'smtp.gmail.com';
Port := 587;
DefaultPort := 0;

Don't set those IOHandler properties manually. Connect() does that internally
for you. Just set the Host/Port properties on TIdSMTP and let it delegate
internally as needed.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 4:23 PM   in response to: João Paulo Cruz... in response to: João Paulo Cruz...
João wrote:

I have this code, and I have checked everything but I don't
know why Am I getting "Connection Closed Gracefully".

That means the server is closing the connection on its end. The most likely
culprit is a rejected SSL/TLS handshake.

SMTP.Port := 587;
SMTP.UseTLS := utUseRequireTLS;

On port 587, you should be using UseTLS=utUseExplicitTLS:

SMTP.UseTLS := utUseExplicitTLS;


Also, note that setting the UseTLS property may change the Port property,
so make sure you are actually connecting to the expected Port. You should
generally set the Port property after setting the UseTLS property:

SMTP.UseTLS := utUseExplicitTLS;
SMTP.Port := 587;


SSLHandler.SSLOptions.Method := sslvSSLv23;

Since you are setting the SSLOptions.Method property directly to sslvSSLv23,
that is going to enable SSLv2 and SSLv3, which Gmail does not support. You
should be using the SSLOptions.TLSVersions property instead:

SSLHandler.SSLOptions.TLSVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];


Or, at least disable SSLv2 and SSLv3:

SSLHandler.SSLOptions.Method := sslvSSLv23;
SSLHandler.SSLOptions.TLSVersions := SSLHandler.SSLOptions.TLSVersions - 
[sslvSSLv2,sslvSSLv3];


SSLHandler.SSLOptions.Mode := sslmUnassigned;

You should be using sslmClient instead.

SSLHandler.SSLOptions.Mode := sslmClient;


try
...
SMTP.Connect;
SMTP.Send(Msg);
finally
if SMTP.Connected then SMTP.Disconnect;
Msg.Free;
SSLHandler.Free;
SMTP.Free;
end;

Disconnect(), and all of those Free() calls, should be in their own try/finally
blocks:

Msg := TIdMessage.Create(nil);
try
  ...
  SMTP := TIdSMTP.Create(nil);
  try
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
      ...
      SMTP.Connect;
      try
        ...
      finally
        SMTP.Disconnect;
      end;
    finally
      SSLHandler.Free;
    end;
  finally
    SMTP.Free;
  end;
finally
  Msg.Free;
end;


Which can be greatly simplified using Owner semantics instead:

SMTP := TIdSMTP.Create(nil);
try
  SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
  ...
  Msg := TIdMessage.Create(SMTP);
  ...
  SMTP.Connect;
  try
    ...
  finally
    SMTP.Disconnect;
  end;
finally
  SMTP.Free;
end;


--
Remy Lebeau (TeamB)
Achim Kalwa

Posts: 39
Registered: 9/28/99
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 4, 2017 7:16 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello Remy,

since I also need to send SMTP email via GMail, your information are
very valuable to me. So I've tried your code fragments:

SSLHandler.SSLOptions.Method := sslvSSLv23;

Since you are setting the SSLOptions.Method property directly to sslvSSLv23,
that is going to enable SSLv2 and SSLv3, which Gmail does not support. You
should be using the SSLOptions.TLSVersions property instead:

SSLHandler.SSLOptions.TLSVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];

TLSVersions is unknown on my system (Delphi 10.1 Berlin). Which Indy
version is required for your code examples?

TIA
Achim
Achim Kalwa

Posts: 39
Registered: 9/28/99
Re: Delphi SMTP mail using Gmail box
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 4, 2017 7:18 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello Remy,

since I also need to send SMTP email via GMail, your information are
very valuable to me. So I've tried your code fragments:

SSLHandler.SSLOptions.Method := sslvSSLv23;

Since you are setting the SSLOptions.Method property directly to sslvSSLv23,
that is going to enable SSLv2 and SSLv3, which Gmail does not support. You
should be using the SSLOptions.TLSVersions property instead:

SSLHandler.SSLOptions.TLSVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];

TLSVersions is unknown on my system (Delphi 10.1 Berlin). Which Indy
version is required for your code examples?

TIA
Achim
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02