Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: Access Violation trying to send email (Delphi 5/Indy 10)


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 9 - Last Post: Sep 24, 2015 2:44 PM Last Post By: Remy Lebeau (Te...
Maxime Rouillard

Posts: 5
Registered: 10/23/15
Access Violation trying to send email (Delphi 5/Indy 10)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 8:58 AM
Hi guys, I'm trying to send emails on Delphi using Indy, but when I try it gives me an access violation, I wonder if anyone here knows why cause I'm at a loss. The access violation pops up when it tries to send the message (IdSMTP.Send(Email);), anyway here is the code I currently have.

procedure TForm1.Button3Click(Sender: TObject);
var
    IdSMTP: TIdSMTP;
    Email: TIdMessage;
    SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
    IdSMTP := TIdSMTP.Create(nil);
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    Email := TIdMessage.Create(nil);
    try
      SSLHandler.MaxLineAction := maException;
      SSLHandler.SSLOptions.Method := sslvSSLv23;
      SSLHandler.SSLOptions.Mode := sslmUnassigned;
      SSLHandler.SSLOptions.VerifyMode := [];
      SSLHandler.SSLOptions.VerifyDepth := 0;
 
      IdSMTP.IOHandler := SSLHandler;
      IdSMTP.Host := 'smtp.gmail.com';
      IdSMTP.Port := 587;
      IdSMTP.Username := 'email@gmail.com';
      IdSMTP.Password := 'pass';
      IdSMTP.UseTLS := utUseExplicitTLS;
 
      Email.From.Address := 'email@gmail.com';
      Email.Recipients.EmailAddresses := 'otheremail@gmail.com';
      Email.Subject := 'Test subject';
      Email.Body.Text := 'Test body';
 
      IdSMTP.Connect;
      if IdSMTP.Connected then
      begin
        IdSMTP.Send(Email);
        IdSMTP.Disconnect;
      end;
   finally
     Email.Free;
     SSLHandler.Free;
     IdSMTP.Free;
  end;
 
end;


Edited by: Maxime Rouillard on Sep 23, 2015 10:18 AM
Maxime Rouillard

Posts: 5
Registered: 10/23/15
Re: Access Violation trying to send email (Delphi 5/Indy 10)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 10:47 AM   in response to: Maxime Rouillard in response to: Maxime Rouillard
Well nevermind, I found the problem... All I needed was to add "IdSMTP.Authenticate;" before sending, so...

procedure TForm1.Button3Click(Sender: TObject);
var
    IdSMTP: TIdSMTP;
    Email: TIdMessage;
    SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
    IdSMTP := TIdSMTP.Create(nil);
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    Email := TIdMessage.Create(nil);
    try
      SSLHandler.MaxLineAction := maException;
      SSLHandler.SSLOptions.Method := sslvSSLv23;
      SSLHandler.SSLOptions.Mode := sslmUnassigned;
      SSLHandler.SSLOptions.VerifyMode := [];
      SSLHandler.SSLOptions.VerifyDepth := 0;
 
      IdSMTP.IOHandler := SSLHandler;
      IdSMTP.Host := 'smtp.gmail.com';
      IdSMTP.Port := 587;
      IdSMTP.Username := 'email@gmail.com';
      IdSMTP.Password := 'pass';
      IdSMTP.UseTLS := utUseExplicitTLS;
 
      Email.From.Address := 'email@gmail.com';
      Email.Recipients.EmailAddresses := 'otheremail@gmail.com';
      Email.Subject := 'Test subject';
      Email.Body.Text := 'Test body';
 
      IdSMTP.Connect;
      if IdSMTP.Connected then
      begin
        IdSMTP.Authenticate;  <-----------This line
        IdSMTP.Send(Email);
        IdSMTP.Disconnect;
      end;
   finally
     Email.Free;
     SSLHandler.Free;
     IdSMTP.Free;
  end;
 
end;


Edited by: Maxime Rouillard on Sep 23, 2015 10:48 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 3:04 PM   in response to: Maxime Rouillard in response to: Maxime Rouillard
Maxime,

Well nevermind, I found the problem... All I needed was to
add "IdSMTP.Authenticate;" before sending, so...

Send() calls Authenticate() if it has not already been called. So that cannot
be the fix for your AV, something else had to be causing it.

--
Remy Lebeau (TeamB)
Maxime Rouillard

Posts: 5
Registered: 10/23/15
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2015 5:37 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Maxime,

Well nevermind, I found the problem... All I needed was to
add "IdSMTP.Authenticate;" before sending, so...

Send() calls Authenticate() if it has not already been called. So that cannot
be the fix for your AV, something else had to be causing it.

--
Remy Lebeau (TeamB)

Well before adding that line I was getting an access violation, then when I added it, it worked... So, I don't see what else would cause it.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2015 12:23 PM   in response to: Maxime Rouillard in response to: Maxime Rouillard
Maxime wrote:

Well before adding that line I was getting an access violation,
then when I added it, it worked...

I guarantee you that calling Authenticate() before Send() is not a fix.
Internally, Send() saves the email recipients into a local list and calls
InternalSend(), which calls Authenticate(), then sends the email data to
the recipients.

procedure TIdSMTPBase.Send(AMsg: TIdMessage);
var
  LRecipients: TIdEMailAddressList;
begin
  LRecipients := TIdEMailAddressList.Create(Self);
  try
    ...
    LRecipients.AddItems(AMsg.Recipients);
    LRecipients.AddItems(AMsg.CCList);
    LRecipients.AddItems(AMsg.BccList);
    Send(AMsg, LRecipients);
  finally
    FreeAndNil(LRecipients);
  end;
end;
 
procedure TIdSMTPBase.Send(AMsg: TIdMessage; ARecipients: TIdEMailAddressList);
var
  LSender: string;
begin
  LSender := Trim(AMsg.Sender.Address);
  if LSender = '' then begin
    LSender := Trim(AMsg.From.Address);
  end;
  InternalSend(AMsg, LSender, ARecipients);
end;
 
procedure TIdSMTP.InternalSend(AMsg: TIdMessage; const AFrom: String; ARecipients: 
TIdEMailAddressList);
begin
  Authenticate; // <-- here
  //...
end;


So, I don't see what else would cause it.

That is what the debugger is meant for.

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


Posts: 9,447
Registered: 12/23/01
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2015 2:43 PM   in response to: Maxime Rouillard in response to: Maxime Rouillard
Maxime wrote:

Hi guys, I'm trying to send emails on Delphi using Indy, but when I
try it gives me an access violation, I wonder if anyone here knows
why cause I'm at a loss.

What is the actual message of the exception?

here is the code I currently have.

I see nothing in that code that would cause an AV.

SSLHandler.SSLOptions.Method := sslvSSLv23;

You should be using TLSv1 with Gmail.

--
Remy Lebeau (TeamB)
Maxime Rouillard

Posts: 5
Registered: 10/23/15
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2015 9:44 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

What is the actual message of the exception?

When I remove the line Authenticate... "Project ... faulted with message: 'access violation at 0x00403dc0: read of address 0x0000002b'."

I see nothing in that code that would cause an AV.

And yet...

You should be using TLSv1 with Gmail.

It tells me Undeclared Identifier with TLSv1.

Edited by: Maxime Rouillard on Sep 24, 2015 9:45 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit][Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2015 12:25 PM   in response to: Maxime Rouillard in response to: Maxime Rouillard
Maxime wrote:

When I remove the line Authenticate... "Project ... faulted with
message: 'access violation at 0x00403dc0: read of address
0x0000002b'."

That means a nil pointer is being accessed. Calling Authenticate() would
certainly not fix THAT kind of error. Something is going wrong in your code.
What you showed earlier should not have any nil pointers in it.

It tells me Undeclared Identifier with TLSv1.

The full name is sslvTLSv1.

--
Remy Lebeau (TeamB)
Maxime Rouillard

Posts: 5
Registered: 10/23/15
Re: Access Violation trying to send email (Delphi 5/Indy 10) [Edit][Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2015 1:03 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

That means a nil pointer is being accessed. Calling Authenticate() would
certainly not fix THAT kind of error. Something is going wrong in your code.
What you showed earlier should not have any nil pointers in it.

I don't quite follow you, are you saying I'm lying about my code? Because that's exactly what's written in my Delphi application. Maybe it has something to do with Delphi 5...

The full name is sslvTLSv1.

Very well, it didn't solve the access violation error however.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Access Violation trying to send email (Delphi 5/Indy 10)[Edit][Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2015 2:44 PM   in response to: Maxime Rouillard in response to: Maxime Rouillard
Maxime wrote:

I don't quite follow you, are you saying I'm lying about my code?
Because that's exactly what's written in my Delphi application.
Maybe it has something to do with Delphi 5...

An AV while reading from address 0x0000002b means a data member is being
accessed via a nil object pointer. There are no nil pointers in the code
you showed, so the only possibility would be a nil pointer inside the RTL
itself, which I highly doubt. But, since you are apparently running your
app inside the debugger, you should be able to trace the code line by line,
even trace into Indy/Delphi source code if you have to, and/or jump to addess
0x00403dc0 to see what code is actually being executed at the time the AV
occurs.

The full name is sslvTLSv1.
Very well, it didn't solve the access violation error however.

It wasn't supposed to. It is just a requirement for accessing Gmail in general.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02