gmailRelaySaslXoauth2 function

SmtpServer gmailRelaySaslXoauth2(
  1. String userEmail,
  2. String accessToken
)

Send through GSuite gmail relay with SASL XOAUTH2 authentication.

This requires that the G Suite SMTP relay service is enabled by the GSuite administrator. For more information see: Send email from a printer, scanner, or app.

This requires an accessToken for userEmail with the OAuth2 scope: https://mail.google.com/. This can be obtained in many differnet ways, one could add an application to the GSuite account and have users grant access, or one could use domain-wide delegation to obtain a service-account that can impersonate any GSuite user for the given domain with the https://mail.google.com/, and then use said service account to obtain an accessToken impersonating a GSuite user.

Implementation

SmtpServer gmailRelaySaslXoauth2(
  String userEmail,
  String accessToken,
) =>
    SmtpServer(
      'smtp-relay.gmail.com',
      xoauth2Token: _formatXoauth2Token(userEmail, accessToken),
      ssl: true,
      port: 465,
    );