Some remarks while I'm testing:
In your unitoauth2.pas you still have aGetXOAuth2Base64 define as a function while in setxauth2 you call it as a procedure. I think it returns the code64 correctly but your unitoauth2.pas is out of date. I suppose you did changed it in your original xoauth2.dll because I do get a code64 back.
At first the geTAUHT2 didn't create a token file but that was because I didn't have libeay32.dll and ssleay32.dll in my project directory (or anywhere else in my path). After copying them in the geTAUHT2 worked and produced the tokens.dat and a correct code64.
I do have the feeling you might end up in doing the GetAccess 2 times. One time in the CheckTokenFile if there is a tokens.dat and then the second time in GettheAccess in geTAUHT2.
You're missing something vital in your TForm1.Button1Click. You receive a code64 (access_token) from your geTAUHT2 but you don't use it anywhere. If you look in my frmmain.pas you'll see I added a helper for TSMTPSend to be able to send the 'AUTH XOAUTH2 code64' right after the smtp.StartTLS. If you don't do that Google won't accept the connection.
Then... because I couldn't trace a DLL I made a small geTAUHT3 procedure to not use the DLL but use the functions in unitoauth2 directly. Just to test if something goes wrong there too. But it didn't.
function geTAUHT3(var EMAIL, FULLNAME, code64: string; tokenfile_name: string): boolean;
type
TCreateit = procedure(client_id, client_secret: string); stdcall;
TGettheAccess = procedure(var email, fullname: string; UseTokenFile: boolean = False);
stdcall;
TDestroyit = procedure; stdcall;
TCheckTokenFile = function: boolean; stdcall;
TGetXOAuth2Base64=procedure (var val: string);stdcall;
//TGetXOAuth2Base64 = function: string; stdcall;
Tgettoken_filename = procedure(Value: string); stdcall;
var
myObj: TObject;
_EC: function: Pointer;
Handle: cardinal;
_Createit: TCreateit;
_GettheAccess: TGettheAccess;
_Destroyit: TDestroyit;
_CheckTokenFile: TCheckTokenFile;
_GetXOAuth2Base64: TGetXOAuth2Base64;
_gettoken_filename: Tgettoken_filename;
msg: string;
const
client_id = '896304839415-nnl5e0smrtakhr9r2l3bno0tes2mrtgk.apps.googleusercontent.com';
client_secret = 'dUahHDn3IMyhCIk3qD4tf8E_';
begin
Result := False;
email := '';
_Createit := @unitoauth2.Createit;
if Assigned(_Createit) then
begin
_Createit(client_id, client_secret);
_gettoken_filename := @unitoauth2.gettoken_filename;
if Assigned(_gettoken_filename) then
_gettoken_filename(tokenfile_name);
_CheckTokenFile := @unitoauth2.CheckTokenFile;
if Assigned(_CheckTokenFile) then
begin
if boolean(_CheckTokenFile) <> True then
msg := 'Could not get Authentication';
end
else
msg := 'CheckTokenFile not found';
_GettheAccess := @unitoauth2.GettheAccess;
if Assigned(_GettheAccess) then
_GettheAccess(email, fullname, True);
_GetXOAuth2Base64 := @unitoauth2.aGetXOAuth2Base64;
if Assigned(_GetXOAuth2Base64) then
_GetXOAuth2Base64(code64);
_Destroyit := @unitoauth2.Destroyit;
if Assigned (_Destroyit) then
_Destroyit;
end;
Result := email <> '';
end;
Everything went great (getting access and getting the code64) and the testmail is send.
When I change it back to geTAUHT2 everything went great until StartTLS. So the correct code64 is received so the xoauth2.dll should work correctly. I get this error:
SMTP ERROR: StartTLS:Success-Other undefined Status
After that the program crashes with a SIGSEGV. So the cleanup of the dll is not done correctly.
But I'm puzzled as to why that would mess up the TSMTPSend because that is not even done in the dll.Edit: I think it has something to do with the fact that the DLL loads the openssl dll's and after it unloads, it also unloads the openssl dll's while your program still needs those to send the mail. Maybe because your DLL is still part of the same process the DLL's can get unloaded (which is wrong).
For instance if you uncheck the FreeLibrary of your own dll it all works:
end;
// FreeLibrary(Handle);
end;
So as a workaround, you could free the DLL at the end of your program and not right after geTAUHT2. Or you need to find a way to "reload" the openssl libraries again for your own .exe. Or you need to find a way for your DLL
not to unload the openssl libraries.
B.T.W. I had no problems compiling the DLL. In Laz1.4.2 or trunk (=Lazarus SVN development version)
So no need for hell to freez over for me