thanks foir your reply.
the following is based on your existing code for getting token
function TgmailFrm.Sendemail(FromName, emailFrom, emailRecip, emailSubject,
emailBody, emailAttach: string; Quietly: Boolean): Boolean;
var
HTTP: TIdHTTP;
Response: TStringList;
Url, s: STring;
Base64: TBase64Encoding;
IdMessage: TIdMessage;
MailBuilder: TIdMessageBuilderPlain;
xoauthSASL: TIdSASLListEntry;
MS: TStringStream;
fIdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
begin
result := False;
if not SetupAuthenticator or not HasSavedToken
then
begin
/// Authenticate; this is done in setup authenticator
IdHTTPServer1.Active := False;
Exit;
end;
// if we only have refresh_token or access token has expired
// request new access_token to use with request
OAuth2_Enhanced.RefreshAccessTokenIfRequired;
if OAuth2_Enhanced.AccessToken.Length = 0
then
begin
raise Exception.Create('Failed to authenticate properly');
// Exit; //need to free the stuff
end;
// xoauthSASL := IdSMTP1.SASLMechanisms.Add;
// xoauthSASL.SASL := TIdOAuth2Bearer.Create(nil);
// TIdOAuth2Bearer(xoauthSASL.SASL).Token := OAuth2_Enhanced.AccessToken;
// TIdOAuth2Bearer(xoauthSASL.SASL).Host := IdSMTP1.Host;
// TIdOAuth2Bearer(xoauthSASL.SASL).Port := IdSMTP1.Port;
// TIdOAuth2Bearer(xoauthSASL.SASL).User := clientaccount;
try
Url := 'https://gmail.googleapis.com/gmail/v1/users/' + emailFrom +
'/messages/send';
try
HTTP := TIdHTTP.Create;
fIdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
with fIdSSLIOHandlerSocketOpenSSL.SSLOptions do
begin
Method := sslvTLSv1_2;
Mode := sslmClient;
SSLVersions := [sslvTLSv1_2];
end;
HTTP.IOHandler := fIdSSLIOHandlerSocketOpenSSL;
// HTTP.Request.CharSet := 'utf-8';
IdMessage := TIdMessage.Create(Application);
MailBuilder := TIdMessageBuilderPlain.Create;
MailBuilder.PlainText.Text := emailBody;
MailBuilder.PlainTextCharSet := 'iso-8859-1';
// MailBuilder.PlainTextContentTransfer := 'base64';
if (emailAttach <> '') and (FileExists(emailAttach))
then
begin
MailBuilder.Attachments.Add(emailAttach);
IdMessage.ContentType := 'multipart/mixed';
end
else
IdMessage.ContentType := 'text/plain';
IdMessage := MailBuilder.NewMessage();
// if UseHTML then
// 'text/html';
// else
IdMessage.ContentType := 'text/plain';
IdMessage.Encoding := meMIME;
IdMessage.From.Address := emailFrom;
IdMessage.From.Name := FromName;
IdMessage.ReplyTo.EMailAddresses := IdMessage.From.Address;
IdMessage.Recipients.Add.Text := emailRecip;
IdMessage.Subject := emailSubject;
IdMessage.Body.Text := emailBody;
IdMessage.NoEncode := False;
MS := TStringStream.Create('', TEncoding.UTF8); // this enables me to show progress
try
if not Quietly
then
begin
IdMessage.SaveToStream(MS);
Sz := MS.Size;
HTTP.onWork := EvHandler.DoWork;
IdSMTP1.onWork := EvHandler.DoWork;
HTTP.BeginWork(wmRead); // this enables me to show progress
end; // if not quietly then
with HTTP do
begin
Request.CustomHeaders.Add(Format('Authorization: Bearer %s',
[OAuth2_Enhanced.AccessToken]));
HandleRedirects := True;
Response.KeepAlive := False;
AllowCookies := True;
end;
MS.Position:=0;
HTTP.Post(Url, MS);
if not Quietly
then
ShowWait(0, 0, TimeToStr(Time) + ' email sent to ' +
emailRecip, True);
if not Quietly
then
HTTP.EndWork(wmRead);
result := True;
except
On E: Exception do
begin
Add2Log('Error while sending email to ' + emailRecip + ': ' +
E.Message);
result := False;
if not Quietly
then
ShowTimedMsg('Error while sending email to ' + emailRecip + ': ' +
E.Message, 10);
end;
end;
finally
IdSMTP1.Disconnect;
HTTP.Free;
// fIdSSLIOHandlerSocketOpenSSL.Free;
MS.Free;
IdMessage.Free;
MailBuilder.Free;
end;
finally
IdHTTPServer1.Active := False;
end;
end;