Home > database >  Username and Password not accepted when I try to send a email using Indy
Username and Password not accepted when I try to send a email using Indy

Time:09-21

I have been struggling to solve this error for the last 3 days and can't find a way to fix it.

First chance exception at $779ACC12. Exception class EIdSMTPReplyError with message
'Username and Password not accepted.

Before this problem I had a problem with SSL library that couldn't load but fixed it when i got the correct .dll files. Now I'm stuck on this.

The code I used is the following.

var
  FormMain: TFormMain;
  mail_username: String;
  mail_password: String;
  mail_to: String;
  mail_subject: String;
  mail_body: String;

implementation

{$R *.dfm}

procedure Gmail(UserName, Password, ToTarget, Subject, Body : String);
var
  DATA : TIdMessage;
  SMTP : TIdSMTP;
  SSL : TIdSSLIOHandlerSocketOpenSSL;
begin
  SMTP := TIdSMTP.Create(nil);
  DAta := TIdMessage.Create(nil);
  SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

  SSL.SSLOptions.Method := sslvTLSv1;
  SSL.SSLOptions.Mode := sslmUnassigned;
  SSL.SSLOptions.VerifyMode := [];
  SSL.SSLOptions.VerifyDepth := 0;

  DATA.From.Address := UserName;
  DATA.Recipients.EMailAddresses := ToTarget;
  DATA.Subject := Subject;
  DATA.Body.Text := Body;

  SMTP.IOHandler := SSL;
  SMTP.Host := 'smtp.gmail.com';
  SMTP.Port := 587;  // also used the other 2 ports and their respective UseTLS
  SMTP.Username := UserName;
  SMTP.Password := Password;
  SMTP.UseTLS := utUseExplicitTLS;

  SMTP.Connect;
  SMTP.Send(DATA);
  SMTP.Disconnect;

  SMTP.Free;
  DATA.Free;
  SSL.Free;
end;

procedure TFormMain.Button1Click(Sender: TObject);
begin
  mail_username := EditUserName.Text;
  mail_password := EditPassword.Text;
  mail_to := EditTo.Text;
  mail_subject := EditSubject.Text;
  mail_body := MemoBody.Text;

  try
    begin
      Gmail(mail_username, mail_password, mail_to, mail_subject, mail_body);
    end;
  except
    ShowMessage('Didn''''t send.');
  end;
end;

I looked everywhere and couldn't find an answer for this problem. I'm working with Delphi 10.2 Tokyo.

CodePudding user response:

While I was still searching for an answer, I kept finding that people talked about the new security changes Google made. In the end, I enabled 2-Step Verification and made an application-specific password. When I used the app password, the email went through fine.

Here is the way I made the app password:

Sign in with App Passwords

  • Related