function SendItViaGMailImap: Cardinal;
const
google_client_id = '1111111111-xxxxxxxxxxxxxx.apps.googleusercontent.com';
google_client_secret = 'xxxxxxxxxxxxxxxxxxxxxxx';
var
gOAuth2: TGoogleOAuth2;
Scopes: GoogleScopeSet;
var
Imap: TmyIMAPSend;
ts: TStringList;
Uid: int64;
UidStr: string;
Ok: Boolean;
Match: TMatch;
Url: String;
begin
Result := MAPI_E_FAILURE;
ts := TStringList.Create;
Imap := TmyIMAPSend.Create;
gOAuth2 := TGoogleOAuth2.Create(google_client_id, google_client_secret);
try
Scopes := [GoogleScope.goMail];
gOAuth2.refresh_token := CipherString(ReadRegistry('GMail', 'refresh_token', ''), 'my_app_name', false);
gOAuth2.access_token := CipherString(ReadRegistry('GMail', 'access_token', ''), 'my_app_name', false);
gOAuth2.GetAccess(Scopes, false); // <- DO NOT get from file
if gOAuth2.email <> '' then
begin
DeleteRegistry('GMail', 'Gebruikersnaam');
DeleteRegistry('GMail', 'Wachtwoord');
WriteRegistry('GMail', 'refresh_token', CipherString(gOAuth2.refresh_token, 'my_app_name', true));
WriteRegistry('GMail', 'access_token', CipherString(gOAuth2.access_token, 'my_app_name', true));
end
else
begin
Result := MAPI_E_INVALID_SESSION;
Exit;
end;
// https://developers.google.com/gmail/imap_extensions?csw=1
Imap.AutoTLS := false;
// https://www.limilabs.com/blog/oauth2-gmail-imap-service-account
Imap.Username := '';
Imap.Password := '';
Imap.OAuth2 := gOAuth2.GetXOAuth2Base64;
Imap.TargetHost := 'imap.gmail.com';
Imap.TargetPort := '993';
Imap.FullSSL := true;
Imap.Sock.SSL.SSLType := LT_all;
// Imap.Sock.SSLDoConnect();
if not Imap.Login() then
begin
ShowMessage('Fout: ' + Imap.ChallengeError);
Result := MAPI_E_LOGIN_FAILURE;
Exit;
end;
(*
Imap.List('*', ts);
ShowMessage(ts.Text);
ShowMessage(Imap.SelectedFolder);
*)
Ok := Imap.SelectFolder('[Gmail]/Concepten');
if not Ok then Ok := Imap.SelectFolder('[Gmail]/Drafts');
if not Ok then
begin
Imap.Logout();
Result := MAPI_E_INVALID_SESSION;
Exit;
end;
ts.Text := CreatePlainEML;
Imap.AppendMess(Imap.SelectedFolder, ts);
// S5 OK [APPENDUID 628624526 53] (Success)
Match := TRegEx.Match(Imap.ResultString, '\[APPENDUID (.*?) (.*?)\]');
if Match.Success and (Match.Groups.Count = 3) then
begin
UidStr := Match.Groups[2].Value;
if Imap.IMAPcommand('UID FETCH ' + UidStr + ' (X-GM-MSGID)') = 'OK' then
begin
// * 13 FETCH (X-GM-MSGID 1590941195342179459 UID 61)
Match := TRegEx.Match(Imap.FullResult.Text, '\(X-GM-MSGID (.*?) UID (.*?)\)');
if Match.Success and (Match.Groups.Count = 3) then
begin
UidStr := Match.Groups[1].Value;
Uid := StrToInt64(UidStr);
Url := 'https://mail.google.com/mail/#drafts?compose=' + lowercase(IntToHex(Uid, 16));
Url := 'https://mail.google.com/mail?authuser=' + gOAuth2.email + '#drafts/' + lowercase(IntToHex(Uid, 16));
// Url := 'https://mail.google.com/mail/#drafts/' + lowercase(IntToHex(Uid, 16));
// https://mail.google.com/mail/u/0/#drafts/%23thread-a%3ammiai-xxxx
// https://mail.google.com/mail/u/0/#drafts?compose=xxx
BrowseURL(Url);
Result := SUCCESS_SUCCESS;
end;
end;
end;
Imap.Logout();
finally
Imap.Free;
ts.Free;
gOAuth2.Free;
end;
end;