Home General

TppRESTMailOutlook365 Using without a Report

Hi,

Will this still work after 22nd October when connecting to a Microsoft email server?

If so, how do I retrieve and set the token?

If not, what should I use?

Thanks,

Ken

Comments

  • Hi Ken,

    I am not aware of any major changes to the way Microsoft handles OAuth2 verification in the near future. Is there something I am missing?
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • I thought they were stopping using Rest.
  • Hi Ken,

    Do you have a link to this information?
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Sorry Nico, looks like it is just basic authentication is being retired on 22nd Oct.

    So back to my other question, how can the token be accessed?
  • Hi Ken,

    ReportBuilder fully supports REST API access to Gmail and Outlook.com using their OAuth2 authentication process. Once you register your application with the mail service, ReportBuilder handles the retrieval of the access token internally and then sends the email.

    See the following articles on web mail overview with RB and background on the OAuth2 protocol.

    https://rbwiki.digital-metaphors.com/output/email/web-mail-overview/

    https://rbwiki.digital-metaphors.com/output/cloud-drive/oauth2-overview/

    Then see the following articles on how to register your app and send emails using these plugins.

    https://rbwiki.digital-metaphors.com/output/email/web-mail-setup-outlook-com/

    https://rbwiki.digital-metaphors.com/output/email/web-mail-setup-gmail/
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Thanks Nico. Is there a sample project available for doing this without using a report?
  • edited September 2022
    Starting with RB 21, it is possible to easily send stand-alone emails without the need of a report. This is one of the many features of RB that really could be its own product :). Below is some simple pseudo code
    uses
    ppEmail,
    ppRESTMailOutlook365;

    ...

    procedure TForm2.Button1Click(Sender: TObject);
    var
    lEmail: TppEmail;
    begin

    lEmail := TppEmail.Create;

    try
    // define email settings
    lEmail.EmailSettings.Recipients.Add('name@myemailserver.com');
    lEmail.EmailSettings.Subject := 'Important Subject';
    lEmail.EmailSettings.Body.Add('Hello!');

    //REST API Settings
    lEmail.EmailSettings.ConnectionSettings.WebMail.Outlook365Settings.OAuth2.ClientID := [ClientID];
    lEmail.EmailSettings.ConnectionSettings.WebMail.Outlook365Settings.OAuth2.ClientSecret := [ClientSecret];
    lEmail.EmailSettings.ConnectionSettings.WebMail.Outlook365Settings.OAuth2.RedirectURI := 'http://localhost';

    lEmail.SendEmail; // send stand-alone email

    finally
    lEmail.Free;

    end;

    end;
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Thanks Nico. Would be nice if you made it a component.
  • I tried this. It connects ok but fails to get the token. If I debug it the error message is:

    {"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter:
    ''code''.\r\nTrace ID: a7eacbf8-2b08-420d-8835-8556bbe35500\r\nCorrelation ID: 22936f2d-49de-4d48-9feb-a50f2ea3039e\r\nTimestamp: 2022-09-23 08:03:33Z","error_codes":[900144],"timestamp":"2022-09-23 08:03:33Z","trace_id":"a7eacbf8-2b08-420d-8835-8556bbe35500","correlation_id":"22936f2d-49de-4d48-9feb-a50f2ea3039e","error_uri":"https://login.microsoftonline.com/error?code=900144"}
  • Hi Ken,

    In my testing with new credentials, the plugin is working correctly. I went ahead and created a new app registration to see if anything has changed.

    1. It appears the authentication code is not being received. Be sure you select "Web" application during the registration process. (This is a new setting so I will update our articles).

    2. Be sure you set the ClientID and ClientSecret correctly. Also be sure you defined the proper Redirect URI in your app registration and report property. This should be "http://localhost".

    3. During your app registration, be sure you gave your application proper permissions to access outside accounts. (i.e. "Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts").
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Hi Nico,

    That now works ok except it then opens an Outlook web page with the details of the email saying [Draft] This message hasn't been sent.

    Thanks,

    Ken

  • Set EmailSettings.PreviewInEmailClient to False to automatically send.
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Thanks Nico. That works but every time I run it I need to select the account to use. How do I avoid that and where is the token stored?
  • Currently ReportBuilder does not save the access token once the application has been closed. If you would like to save and reuse the token it will need to be saved manually when your application is closed, and reloaded when opened. You will also need to save/load the expiration datetime.

    Note that access tokens are generally only valid for 1 hour.

    It is on our list of possible enhancements to handle this internally as well as provide refresh token support for a later release of ReportBuilder

    Uses
    ppEmail, ppRESTMailCustom;

    ...

    if lEmail.SMTP is TppRESTMailCustom then
    begin
    lsToken := TppRESTMailCustom(lEmail.SMTP).Authenticator.AccessToken;
    lExpire := TppRESTMailCustom(lEmail.SMTP).Authenticator.AccessTokenExpiry;
    //save to file
    end;
    if lEmail.SMTP is TppRESTMailCustom then
    begin
    //load from file
    TppRESTMailCustom(lEmail.SMTP).Authenticator.AccessToken := lsToken;
    TppRESTMailCustom(lEmail.SMTP).Authenticator.AccessTokenExpiry := lExpire;
    end;
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
Sign In or Register to comment.