We're using the AESendMail a few years now without problems.
Now gmail is changing their conditions and you can only send mail with OAuth2.
I've test it with the example project and works fine. But in my own project it will not work.
For the user I've created a form to get the accesstoken and refresktoken. This is saved in a database.
Then I create a message and send it to gmail:
procedure Tfo3EmailOpstellen.SendGEmail;
const ConnectSSLAuto = 1;
var body_html : string;
begin
Oauth := TOauthWrapper.Create;
oSmtp := TMail.Create(nil);
try
Oauth.InitGoogleSmtpProvider;
oSmtp.LicenseCode := 'xxxxxxxx';
// Set Asynchronous mode
oSmtp.Asynchronous := 1;
Oauth.accesstoken := email.EmailAccountNil.GMail_AccessToken; // from database
Oauth.refreshtoken := email.EmailAccountNil.GMail_RefreshToken; // from database
if not DoOauth() then
begin
showmessage('Failed to request/refresh access token!');
exit;
end;
oSmtp.Charset := 'utf-8';
oSmtp.FromAddr := email.EmailAccountNil.SMTP_Inlognaam;
oSmtp.ReplyTo := email.EmailAccountNil.SMTP_Inlognaam;
// Add recipient's
oSmtp.ClearRecipient();
oSmtp.AddRecipientEx(trim(email.EmailAdresNaarOverflow), 0);
// Set subject
oSmtp.Subject := email.Onderwerp;
// Using HTML FORMAT to send mail
oSmtp.BodyFormat := 1;
body_html := email.AntwoordHtmlOverflow2;
oSmtp.ImportHtml( body_html, GetCurrentDir());
oSmtp.ServerAddr := email.EmailAccountNil.SMTP_Servernaam;
oSmtp.ServerPort := 587; //ports[lstPort.ItemIndex];
oSmtp.Protocol := 0; //SMTP protocol
oSmtp.UserName := Oauth.UserEmail;
oSmtp.Password := Oauth.AccessToken;
oSmtp.AuthType := 5; // XOAUTH2;
// Use SSL/TLS based on server port.
oSmtp.ConnectType := ConnectSSLAuto;
oSmtp.SendMail();
if oSmtp.GetLastErrDescription() <>'' then
ShowNBMessage('Fout : ' + oSmtp.GetLastErrDescription())
else
ShowNBMessage('Mail verstuurd');
finally
Oauth.free;
oSMTP.free;
end;
end;
Everytime a mail will be sent, the SMTP will be created. The mail is sent but is not recieved ever. Sendmail() gives no error back.
If I use this piece of code with geeting an accesscode the mail is sent and received succesfully. But then I have to login to gmail everytime when sending mail.
Is it possible to send a mail with a accesskey without logged in gmail?
I created my project in Delphi 10.4
with kind regards,
John Kuiper.