procedure TForm1.SendSMTP(ptToken:
String);
Var lcSMTP:TIdSMTP;
lcMail:TIdMessage;
lcRec:TIdEMailAddressItem;
ltAuth:
String;
lcAuthSASL:TIdSASLListEntry;
lcIdSSLIOHandlerSocketSMTP:TidSSLIOHandlerSocketOpenSSL;
lcUserPass:TIdUserPassProvider;
lcDebug:TIdLogDebug;
begin
lcSMTP:=TIdSMTP.Create;
Try
lcSMTP.Host:='
smtp.office365.com';
lcSMTP.Port:=587;
lcMail:=TIdMessage.Create(lcSMTP);
Try
lcMail.Encoding:=meMime;
lcRec:=lcMail.Recipients.Add;
lcRec.Address:='
XXX@YYY.de';
lcRec.
Name:='
XXX@YYY.de';
lcMail.Subject:='
Test';
lcMail.Body.Text:='
Testnachricht via OAuth';
lcIdSSLIOHandlerSocketSMTP:=TidSSLIOHandlerSocketOpenSSL.Create(lcSMTP);
lcIdSSLIOHandlerSocketSMTP.SSLOptions.SSLVersions:=[sslvTLSv1_2];
lcUserPass:=TIdUserPassProvider.Create(lcSMTP);
lcUserPass.Username:='
myname@test.onmicrosoft.com';
lcUserPass.Password:=ptToken;
lcAuthSASL:=lcSMTP.SASLMechanisms.Add;
lcAuthSASL.SASL:=TIdSASLXOAuth2.Create(lcSMTP);
TIdSASLXOAuth2(lcAuthSASL.SASL).UserPassProvider:=lcUserPass;
lcDebug:=TIdLogDebug.Create(lcSMTP);
lcDebug.OnReceive:=DoAddLogReceive;
lcDebug.OnSend:=DoAddLogSend;
lcSMTP.Intercept:=lcDebug;
lcSMTP.AuthType:=satSASL;
lcSMTP.Connect;
--------------------------------------
- hier wird eine
Exception ausgelöst -
--------------------------------------
lcSMTP.Authenticate;
lcSMTP.Send(lcMail);
Finally
lcMail.Free;
End;
lcSMTP.Disconnect;
Finally
lcSMTP.Free;
End;
end;