procedure SendSMTP(ptToken:
String);
Var lcSMTP:TIdSMTP;
lcMail:TIdMessage;
lcRec:TIdEMailAddressItem;
lcAuthSASL:TIdSASLListEntry;
lcProvider:TProviderInfo;
lcIdSSLIOHandlerSocketSMTP:TidSSLIOHandlerSocketOpenSSL;
lcUserPass:TIdUserPassProvider;
lcDebug:TIdLogDebug;
begin
ctToken:=ptToken;
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:='
MyMail@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.IOHandler:=lcIdSSLIOHandlerSocketSMTP;
lcSMTP.UseTLS:=utUseExplicitTLS;
lcSMTP.AuthType:=satSASL;
lcSMTP.Connect;
lcSMTP.Authenticate;
lcSMTP.Send(lcMail);
Finally
lcMail.Free;
End;
lcSMTP.Disconnect;
Finally
lcSMTP.Free;
End;
end;