MailClient constructor

MailClient(
  1. MailAccount account, {
  2. bool isLogEnabled = false,
  3. int? downloadSizeLimit,
  4. EventBus? eventBus,
  5. String? logName,
  6. Duration? defaultWriteTimeout = const Duration(seconds: 2),
  7. Duration? defaultResponseTimeout = const Duration(seconds: 5),
  8. bool onBadCertificate(
    1. X509Certificate
    )?,
  9. Id? clientId,
  10. Future<OauthToken?> refresh(
    1. MailClient client,
    2. OauthToken expiredToken
    )?,
  11. Future onConfigChanged(
    1. MailAccount account
    )?,
})

Creates a new highlevel online mail client for the given account.

Specify the account settings with account.

Set isLogEnabled to true to debug connection issues and the logName to differentiate between mail clients.

Set a defaultWriteTimeout if you do not want to use the default timeout of 2 seconds.

Set a defaultResponseTimeout if you do not want to use the default timeout for waiting for responses to simple commands of 5 seconds.

Specify the optional downloadSizeLimit in bytes to only download messages automatically that are this size or lower.

onBadCertificate is an optional handler for unverifiable certificates. The handler receives the X509Certificate, and can inspect it and decide (or let the user decide) whether to accept the connection or not. The handler should return true to continue the SecureSocket connection.

Set a clientId when the ID should be send automatically after logging in for IMAP servers that supports the IMAP4 ID extension.

Specify the refresh callback in case you support OAuth-based tokens that might expire.

Specify the optional onConfigChanged callback for persisting a changed token in the account, after it has been refreshed.

Implementation

MailClient(
  MailAccount account, {
  bool isLogEnabled = false,
  int? downloadSizeLimit,
  EventBus? eventBus,
  String? logName,
  this.defaultWriteTimeout = const Duration(seconds: 2),
  this.defaultResponseTimeout = const Duration(seconds: 5),
  bool Function(X509Certificate)? onBadCertificate,
  this.clientId,
  Future<OauthToken?> Function(MailClient client, OauthToken expiredToken)?
      refresh,
  Future Function(MailAccount account)? onConfigChanged,
})  : _eventBus = eventBus ?? EventBus(),
      _account = account,
      _isLogEnabled = isLogEnabled,
      _downloadSizeLimit = downloadSizeLimit,
      _refreshOAuthToken = refresh,
      _onConfigChanged = onConfigChanged {
  final config = _account.incoming;
  if (config.serverConfig.type == ServerType.imap) {
    _incomingMailClient = _IncomingImapClient(
      _downloadSizeLimit,
      _eventBus,
      logName,
      defaultWriteTimeout,
      defaultResponseTimeout,
      config,
      this,
      isLogEnabled: _isLogEnabled,
      onBadCertificate: onBadCertificate,
    );
  } else if (config.serverConfig.type == ServerType.pop) {
    _incomingMailClient = _IncomingPopClient(
      _downloadSizeLimit,
      _eventBus,
      logName,
      config,
      this,
      isLogEnabled: _isLogEnabled,
      onBadCertificate: onBadCertificate,
    );
  } else {
    throw InvalidArgumentException(
      'Unsupported incoming'
      'server type [${config.serverConfig.typeName}].',
    );
  }
  final outgoingConfig = _account.outgoing;
  if (outgoingConfig.serverConfig.type != ServerType.smtp) {
    print(
      'Warning: unknown outgoing server '
      'type ${outgoingConfig.serverConfig.typeName}.',
    );
  }
  _outgoingMailClient = _OutgoingSmtpClient(
    this,
    _account.outgoingClientDomain,
    _eventBus,
    'SMTP-$logName',
    outgoingConfig,
    isLogEnabled: _isLogEnabled,
    onBadCertificate: onBadCertificate,
  );
}