0
votes

I'm trying to send an email using Indy 10 on Delphi 5, what happens is that it doesn't want to authenticate as it shows me the "Nooo" message. So far I have this code here (which I got from another Stack Overflow question). I'm not sure why it doesn't work for me since it apparently did for the other person... Maybe I missed something, I replaced the senderemail and receiveremail with 2 of my actual mails and the password of course.

EDIT

So that's where I am right now, it pops a "Must issue a STARTTLS command first." error now however... I'm really confused with this...

EDIT 2

So I managed to make it work, here's the final result. Feel free to tell me if something isn't done properly... I had an Access Violation until I added the "IdSMTP.Authenticate;" line before sending the message!

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 protected]';
        IdSMTP.Password := 'password';
        IdSMTP.UseTLS := utUseExplicitTLS;

        Email.From.Address := '[email protected]';
        Email.Recipients.EmailAddresses := '[email protected]';
        Email.Subject := 'Test subject';
        Email.Body.Text := 'Test body';

        IdSMTP.Connect;
        if IdSMTP.Connected then
        begin
            IdSMTP.Authenticate;
            IdSMTP.Send(Email);
            IdSMTP.Disconnect;
        end;
    finally
        Email.Free;
        SSLHandler.Free;
        IdSMTP.Free;
    end;
end;
1
Note that AuthType=satDefault does not work with all servers. It uses the AUTH LOGIN command, which is not secure, and some servers have dropped support for it. You should consider using AuthType=satSASL with the TIdSMTP.SASLMechanisms collection instead. You do not need to call Authenticate() manuallyRoman Marusyk
Gmail uses 25, 465 and 587 for SMTP, but since almost all isps block 25, 465 is the preferred SMTP with SSL for gmail, and 587 is the preferred SMTP with TLS port.Roman Marusyk
GMail can use settings which does not allow access to your program - try to find "Access for less secure apps" in your settings.smooty86
You do not need to enable "Access for less secure apps" if you have 2-factor authentication enabled and have created an app-specific password.Remy Lebeau

1 Answers

2
votes

Try to use this code:

  IdSMTP := TIdSMTP.Create(nil);
  try
    IdSMTP.UseTLS := utNoTLSSupport;
    IdSMTP.Host := 'smtp.gmail.com';
    IdSMTP.Port := 25;
    IdSMTP.AuthType := satDefault;
    IdSMTP.Username := '[email protected]';
    IdSMTP.Password := 'Password';        

    IdMessage := TIdMessage.Create(nil);
    try
        IdMessage.From.Name := 'My Name';
        IdMessage.From.Address := '[email protected]';
        IdMessage.Subject := 'E-mail subject';
        IdMessage.Body.Add('E-mail body.');
        IdMessage.Recipients.EMailAddresses := '[email protected]';           
        IdMessage.ReceiptRecipient.Name := 'Name';

        if not IdSMTP.Connected then
          IdSMTP.Connect;

        IdSMTP.Send(IdMessage);

     finally
          IdMessage.Free;
          IdSMTP.Disconnect;
     end;

  finally
    IdSMTP.Free;
    IdSSLIOHandlerSocketOpenSSL1.Free;